Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public ResponseEntity<DocumentDTO> uploadDocument(UUID id, MultipartFile file, S
.year(docYear)
.contentType(file.getContentType())
.stream(file.getInputStream())
.size(file.getSize())
.build();

DocumentDTO documentDTO = this.documentService.putDocument(args);
Expand Down Expand Up @@ -116,6 +117,7 @@ public ResponseEntity<DocumentWithUrlResponseDTO> replaceDocument(UUID id, UUID
.year(target.year())
.contentType(file.getContentType())
.stream(file.getInputStream())
.size(file.getSize())
.build()
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,16 @@ public DocumentDTO putDocument(PutDocumentArgsDTO dto)
dto.category(),
dto.type(),
dto.owner(),
dto.year());
dto.year(),
dto.size());

try {
makeBucketIfNotExists(dto.owner());

client.putObject(
PutObjectArgs.builder()
.bucket(dto.owner())
.stream(dto.stream(), dto.stream().available(), -1)
.stream(dto.stream(), dto.size(), -1)
.contentType(dto.contentType())
.userMetadata(metadata)
.object(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@ public static Map<String, String> from(UUID id,
DocumentCategory category,
DocumentType type,
String owner,
Year year) {
Year year,
long size) {
return Map.of(
"owner", owner,
"id", id.toString(),
"category", category.toString(),
"type", type.toString(),
"year", year.toString());
}

public static Map<String, String> from(DocumentDTO dto) {
return from(
dto.id(),
dto.category(),
dto.type(),
dto.owner(),
dto.year());
"year", year.toString(),
"size", Long.toString(size));
}

private static String minioMetadataKey(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public record PutDocumentArgsDTO(
InputStream stream,
long size,
DocumentCategory category,
DocumentType type,
String contentType,
Expand All @@ -23,6 +24,7 @@ public static PutDocumentArgsDTO.Builder builder() {

public static class Builder {
private InputStream stream;
private Long size;
private DocumentCategory category;
private DocumentType type;
private String contentType;
Expand All @@ -34,6 +36,11 @@ public Builder stream(InputStream stream) {
return this;
}

public Builder size(long size) {
this.size = size;
return this;
}

public Builder category(DocumentCategory category) {
this.category = category;
return this;
Expand Down Expand Up @@ -62,6 +69,7 @@ public Builder year(Year year) {
public PutDocumentArgsDTO build() {
return new PutDocumentArgsDTO(
Optional.ofNullable(stream).orElseThrow(InsufficientDataException::new),
Optional.ofNullable(size).orElseThrow(InsufficientDataException::new),
Optional.ofNullable(category).orElseThrow(InsufficientDataException::new),
Optional.ofNullable(type).orElseThrow(InsufficientDataException::new),
Optional.ofNullable(contentType).orElseThrow(InsufficientDataException::new),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private void storePatientDocument(
.type(type)
.contentType(file.getContentType())
.stream(file.getInputStream())
.size(file.getSize())
.build());

} catch (Exception error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public void uploadProfessionalPhoto(UUID id, MultipartFile file) {
DocumentDTO document = documentService.putDocument(
PutDocumentArgsDTO.builder()
.stream(file.getInputStream())
.size(file.getSize())
.category(DocumentCategory.PROFESSIONAL)
.type(DocumentType.PHOTO)
.contentType(file.getContentType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private void storeDocument(HealthProfessional professional, DocumentType type, M
.type(type)
.contentType(file.getContentType())
.stream(file.getInputStream())
.size(file.getSize())
.build()
);
} catch (Exception error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DocumentMetadataMapperTest {
private static final UUID DOCUMENT_ID = UUID.fromString("1f2e3d4c-5b6a-7988-9a0b-1c2d3e4f5061");
private static final String OWNER = "MARIA SOUZA";
private static final int YEAR_VALUE = 2026;
private static final long SIZE_VALUE = 12_345L;
private static final String MINIO_PREFIX = "X-Amz-Meta-";

private static Map<String, String> minioMetadata() {
Expand All @@ -36,21 +37,37 @@ private static Map<String, String> minioMetadata() {
}

@Test
@DisplayName("from deve gravar as cinco chaves cruas esperadas pelo MinIO")
@DisplayName("from deve gravar as seis chaves cruas esperadas pelo MinIO")
void from_ShouldBuildMapWithAllRawKeys() {
Map<String, String> metadata = DocumentMetadataMapper.from(
DOCUMENT_ID,
DocumentCategory.MEDICAL,
DocumentType.MEDICAL_REPORT,
OWNER,
Year.of(YEAR_VALUE));
Year.of(YEAR_VALUE),
SIZE_VALUE);

assertEquals(5, metadata.size());
assertEquals(6, metadata.size());
assertEquals(OWNER, metadata.get("owner"));
assertEquals(DOCUMENT_ID.toString(), metadata.get("id"));
assertEquals(DocumentCategory.MEDICAL.toString(), metadata.get("category"));
assertEquals(DocumentType.MEDICAL_REPORT.toString(), metadata.get("type"));
assertEquals(String.valueOf(YEAR_VALUE), metadata.get("year"));
assertEquals(String.valueOf(SIZE_VALUE), metadata.get("size"));
}

@Test
@DisplayName("from deve persistir o tamanho real informado nos metadados")
void from_ShouldPersistProvidedSize() {
Map<String, String> metadata = DocumentMetadataMapper.from(
DOCUMENT_ID,
DocumentCategory.MEDICAL,
DocumentType.MEDICAL_REPORT,
OWNER,
Year.of(YEAR_VALUE),
SIZE_VALUE);

assertEquals(String.valueOf(SIZE_VALUE), metadata.get("size"));
}

@Test
Expand All @@ -61,35 +78,14 @@ void from_ShouldSerializeEnumsByValue() {
DocumentCategory.ABSENCE,
DocumentType.ATTACHMENTANY,
OWNER,
Year.of(YEAR_VALUE));
Year.of(YEAR_VALUE),
SIZE_VALUE);

assertEquals("FALTA", metadata.get("category"));
assertEquals("ANEXO_QUALQUER", metadata.get("type"));
assertTrue(DocumentCategory.ABSENCE.toString().equals("FALTA"));
}

@Test
@DisplayName("from de DTO deve produzir exatamente o mesmo mapa da sobrecarga de cinco argumentos")
void fromDocumentDto_ShouldDelegateToFiveArgumentOverload() {
DocumentDTO dto = new DocumentDTO(
DOCUMENT_ID,
"DOCUMENTO_MEDICO/2026/LAUDO/" + DOCUMENT_ID,
DocumentCategory.MEDICAL,
DocumentType.MEDICAL_REPORT,
OWNER,
Year.of(YEAR_VALUE));

Map<String, String> fromDto = DocumentMetadataMapper.from(dto);
Map<String, String> fromArguments = DocumentMetadataMapper.from(
DOCUMENT_ID,
DocumentCategory.MEDICAL,
DocumentType.MEDICAL_REPORT,
OWNER,
Year.of(YEAR_VALUE));

assertEquals(fromArguments, fromDto);
}

@Test
@DisplayName("from de mapa deve ler as chaves prefixadas do MinIO")
void fromMap_ShouldReadMinioPrefixedKeys() {
Expand Down Expand Up @@ -152,14 +148,15 @@ void fromMap_ShouldThrowDateTimeParseException_WhenYearIsInvalid() {
}

@Test
@DisplayName("from de mapa nao faz round-trip com from de cinco argumentos, que grava chaves cruas")
void fromMap_ShouldNotRoundTripWithTheFiveArgumentOverload() {
@DisplayName("from de mapa nao faz round-trip com from de metadados, que grava chaves cruas")
void fromMap_ShouldNotRoundTripWithTheMetadataOverload() {
Map<String, String> rawKeys = DocumentMetadataMapper.from(
DOCUMENT_ID,
DocumentCategory.MEDICAL,
DocumentType.MEDICAL_REPORT,
OWNER,
Year.of(YEAR_VALUE));
Year.of(YEAR_VALUE),
SIZE_VALUE);

assertThrows(NullPointerException.class, () -> DocumentMetadataMapper.from(rawKeys));
}
Expand Down
Loading