From d50b201eb6593d5090e3cad496895ced88a5a7c3 Mon Sep 17 00:00:00 2001
From: Leonardo Vannucci <leonardo.vannucci@grupposistematica.it>
Date: Tue, 30 Apr 2019 09:57:29 +0200
Subject: [PATCH] Aggiunta customKey + fix pubblicazione file con nome diverso ma stessa customKey
---
dg1cloud-core/src/main/java/it/digione/dg1cloud/service/CloudService.java | 41 +++++++++++++++++++++++++++++++----------
1 files changed, 31 insertions(+), 10 deletions(-)
diff --git a/dg1cloud-core/src/main/java/it/digione/dg1cloud/service/CloudService.java b/dg1cloud-core/src/main/java/it/digione/dg1cloud/service/CloudService.java
index 0fe93bd..3786d06 100644
--- a/dg1cloud-core/src/main/java/it/digione/dg1cloud/service/CloudService.java
+++ b/dg1cloud-core/src/main/java/it/digione/dg1cloud/service/CloudService.java
@@ -171,7 +171,7 @@
RegDocument regDocument = null;
if ( Strings.isBlank(uploadedFileDescriptor.getCustomKey()) == false ) {
logger.debug("Controllo esistenza file sul db tramite customKey {}", uploadedFileDescriptor.getCustomKey());
- regDocument = regDocumentRepository.findOneByCustomKey(uploadedFileDescriptor.getCustomKey());
+ regDocument = regDocumentRepository.findOneByCustomKey(uploadedFileDescriptor.getCustomKey(), userName);
} else {
logger.debug("Controllo esistenza file sul db");
regDocument = regDocumentRepository.findOneByHashAndName(uploadedFileDescriptor.getSha256(),
@@ -181,7 +181,6 @@
File uploadedFile = new File(getUploadedFilePath(cloudFileDescriptorPath));
if ( regDocument == null ) {
-
logger.debug("Creazione nuovo record per il documento da conservare");
regDocument = new RegDocument();
regDocument.setCreatedBy(userName);
@@ -189,19 +188,28 @@
logger.debug("Trovato 1 record per il documento da memorizzare");
}
+ String oldFilePath = null;
+
+ if ( Strings.isBlank(regDocument.getFilePath()) == false ) {
+ oldFilePath = regDocument.getFilePath();
+ }
+
regDocument.setModifiedBy(userName);
regDocument.setFileHash(uploadedFileDescriptor.getSha256());
- regDocument.setFileName(uploadedFileDescriptor.getFileName());
+ if ( Strings.isBlank(uploadedFileDescriptor.getCustomKey()) == true ||
+ Strings.isBlank(regDocument.getFileName() ) == true ) {
+ regDocument.setFileName(uploadedFileDescriptor.getFileName());
+ regDocument.setSecretKey(uploadedFileDescriptor.getSecretKey());
+ }
regDocument.setFileSize(uploadedFile.length());
regDocument.setDueDate(uploadedFileDescriptor.getDueDate());
regDocument.setFilePath("");
- regDocument.setSecretKey(uploadedFileDescriptor.getSecretKey());
regDocument.setCustomKey(uploadedFileDescriptor.getCustomKey());
regDocumentRepository.save(regDocument);
try {
- moveUploadedFileToStorage(uploadedFile.getAbsolutePath(), regDocument);
+ moveUploadedFileToStorage(uploadedFile.getAbsolutePath(), regDocument, uploadedFileDescriptor.getFileName());
} catch (Exception e) {
try {
writeKoFile(cloudFileDescriptorPath, e.getMessage());
@@ -210,6 +218,14 @@
regDocumentRepository.delete(regDocument);
}
}
+
+ if ( Strings.isBlank(oldFilePath) == false && regDocument.getFilePath().equalsIgnoreCase(oldFilePath) == false ) {
+ logger.debug("Il file path e' cambiato. Rimuovo il vecchio file");
+ File oldFile = new File( oldFilePath );
+ if (oldFile.exists() == true )
+ FileUtils.deleteQuietly(oldFile);
+ }
+
try {
createOkFile(cloudFileDescriptorPath, regDocument);
} catch (Exception e) {
@@ -222,11 +238,11 @@
}
}
- private void moveUploadedFileToStorage( String source, RegDocument regDocument ) throws IOException {
+ private void moveUploadedFileToStorage( String source, RegDocument regDocument, String fileName ) throws IOException {
logger.debug("Spostamento file nello storage");
File sourceFile = new File(source);
- File destinationFile = new File(generateStoragePath(regDocument));
+ File destinationFile = new File(generateStoragePath(regDocument, fileName));
if ( destinationFile.exists() == false ) {
logger.debug("Creazione alberatura directory: " + destinationFile.getParent());
@@ -264,9 +280,14 @@
String json = mapper.writeValueAsString(uploadedFileDescriptorOk);
logger.debug("Scrittura json di ok:\n{}", json);
FileUtils.writeStringToFile(okFile, json, StandardCharsets.UTF_8);
- set777(okFile);
} catch (IOException e) {
throw new RuntimeException("Errore nella scrittura del file di OK", e);
+ }
+
+ try {
+ set777(okFile);
+ } catch (Exception e) {
+ logger.error("Non e' stato possibile settare i permessi di scrittura 777", e);
}
File cloudFile = new File( getUploadedFilePath(cloudFileDescriptorPath) );
@@ -309,7 +330,7 @@
return fileDescriptor.getParentFile().getAbsolutePath() + File.separator + FilenameUtils.getBaseName(fileDescriptor.getName());
}
- private String generateStoragePath( RegDocument regDocument ) {
+ private String generateStoragePath( RegDocument regDocument, String fileName ) {
if ( regDocument.getCreated() == null )
throw new IllegalStateException("La data di creazione del RegDocument non e' valorizzata");
@@ -332,7 +353,7 @@
sb.append(File.separator);
sb.append(regDocument.getDocumentId());
sb.append(File.separator);
- sb.append(regDocument.getFileName());
+ sb.append(fileName);
return sb.toString();
}
--
Gitblit v1.6.2