Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit fb0cc72

Browse files
author
Thomas Broyer
committed
Store a reference in database to document storage
This reference is the path relative to the documents.path for the current filesystem-based implementation. Change-Id: I2e8adfaa8987bd68a199ea4b557d29b6a6b0996d
1 parent 3021e06 commit fb0cc72

5 files changed

Lines changed: 31 additions & 20 deletions

File tree

sormas-backend/src/main/java/de/symeda/sormas/backend/document/Document.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class Document extends AbstractDomainObject {
4545
private String name;
4646
private String contentType;
4747
private long size;
48+
private String storageReference;
4849
private String relatedEntityUuid;
4950
private DocumentRelatedEntityType relatedEntityType;
5051

@@ -91,6 +92,15 @@ public void setSize(long size) {
9192
this.size = size;
9293
}
9394

95+
@Column(name = "storage_reference")
96+
public String getStorageReference() {
97+
return storageReference;
98+
}
99+
100+
public void setStorageReference(String storageReference) {
101+
this.storageReference = storageReference;
102+
}
103+
94104
@Column(name = "relatedentity_uuid")
95105
public String getRelatedEntityUuid() {
96106
return relatedEntityUuid;

sormas-backend/src/main/java/de/symeda/sormas/backend/document/DocumentFacadeEjb.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ public DocumentDto saveDocument(DocumentDto dto, byte[] content) throws IOExcept
6666
}
6767

6868
Document document = fromDto(dto);
69+
70+
document.setStorageReference(documentStorageService.save(document, content));
71+
6972
documentService.persist(document);
7073
documentService.doFlush();
7174

72-
documentStorageService.save(document, content);
73-
7475
documentSavedEvent.fire(new DocumentSaved(document));
7576

7677
return toDto(document);
@@ -95,14 +96,14 @@ public String isExistingDocument(DocumentRelatedEntityType type, String uuid, St
9596
@Override
9697
public byte[] read(String uuid) throws IOException {
9798
Document document = documentService.getByUuid(uuid);
98-
return documentStorageService.read(document);
99+
return documentStorageService.read(document.getStorageReference());
99100
}
100101

101102
@Override
102103
public void cleanupDeletedDocuments() {
103104
List<Document> deleted = documentService.getDeletedDocuments();
104105
for (Document document : deleted) {
105-
documentStorageService.delete(document);
106+
documentStorageService.delete(document.getStorageReference());
106107
documentService.delete(document);
107108
}
108109
}

sormas-backend/src/main/java/de/symeda/sormas/backend/document/DocumentStorageService.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.slf4j.Logger;
3030
import org.slf4j.LoggerFactory;
3131

32-
import de.symeda.sormas.api.ConfigFacade;
33-
import de.symeda.sormas.backend.common.ConfigFacadeEjb;
3432
import de.symeda.sormas.backend.common.ConfigFacadeEjb.ConfigFacadeEjbLocal;
3533

3634
@Stateless
@@ -42,25 +40,27 @@ public class DocumentStorageService {
4240
@EJB
4341
private ConfigFacadeEjbLocal configFacade;
4442

45-
public byte[] read(Document document) throws IOException {
46-
return Files.readAllBytes(computeFilePath(document));
43+
public byte[] read(String storageReference) throws IOException {
44+
return Files.readAllBytes(Paths.get(configFacade.getDocumentFilesPath(), storageReference));
4745
}
4846

49-
public void save(Document document, byte[] content) throws IOException {
50-
Path filePath = computeFilePath(document);
47+
public String save(Document document, byte[] content) throws IOException {
48+
Path relativePath = computeRelativePath(document);
49+
Path filePath = Paths.get(configFacade.getDocumentFilesPath()).resolve(relativePath);
5150
Files.createDirectories(filePath.getParent());
5251
Files.write(filePath, content, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
52+
return relativePath.toString();
5353
}
5454

5555
/**
5656
* Delete file on disk when a document failed to be saved in database.
5757
*/
5858
public void cleanupUnsavedDocument(@Observes(during = TransactionPhase.AFTER_FAILURE) DocumentSaved event) {
59-
delete(event.getDocument());
59+
delete(event.getDocument().getStorageReference());
6060
}
6161

62-
public void delete(Document document) {
63-
Path path = computeFilePath(document);
62+
public void delete(String storageReference) {
63+
Path path = Paths.get(configFacade.getDocumentFilesPath(), storageReference);
6464
try {
6565
Files.deleteIfExists(path);
6666
} catch (IOException e) {
@@ -69,9 +69,8 @@ public void delete(Document document) {
6969
}
7070

7171
@SuppressWarnings("deprecation")
72-
private Path computeFilePath(Document document) {
72+
private Path computeRelativePath(Document document) {
7373
return Paths.get(
74-
configFacade.getDocumentFilesPath(),
7574
Integer.toString(1900 + document.getCreationDate().getYear()),
7675
Integer.toString(1 + document.getCreationDate().getMonth()),
7776
Integer.toString(document.getCreationDate().getDate()),

sormas-backend/src/main/resources/sql/sormas_schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5565,6 +5565,7 @@ CREATE TABLE documents (
55655565
name character varying(255) NOT NULL,
55665566
contenttype character varying(255) NOT NULL,
55675567
size bigint NOT NULL,
5568+
storage_reference character varying(255) NOT NULL,
55685569
relatedentity_uuid character varying(36) NOT NULL,
55695570
relatedentity_type character varying(255) NOT NULL,
55705571

sormas-backend/src/test/java/de/symeda/sormas/backend/document/MockDocumentStorageService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
package de.symeda.sormas.backend.document;
1616

17-
import java.io.IOException;
1817
import java.nio.charset.StandardCharsets;
1918

2019
import javax.enterprise.inject.Specializes;
@@ -23,16 +22,17 @@
2322
public class MockDocumentStorageService extends DocumentStorageService {
2423

2524
@Override
26-
public byte[] read(Document document) throws IOException {
27-
return document.getUuid().getBytes(StandardCharsets.UTF_8);
25+
public byte[] read(String storageReference) {
26+
return storageReference.getBytes(StandardCharsets.UTF_8);
2827
}
2928

3029
@Override
31-
public void save(Document document, byte[] content) throws IOException {
30+
public String save(Document document, byte[] content) {
31+
return document.getUuid();
3232
}
3333

3434
@Override
35-
public void delete(Document document) {
35+
public void delete(String storageReference) {
3636
}
3737

3838
@Override

0 commit comments

Comments
 (0)