Leonardo Vannucci
2019-04-03 84d99d0f0700b2a19f504b21b1747275251305d8
dg1cloud-core/src/main/java/it/digione/dg1cloud/service/CloudService.java
....@@ -18,6 +18,7 @@
1818 import org.apache.commons.codec.digest.DigestUtils;
1919 import org.apache.commons.io.FileUtils;
2020 import org.apache.commons.io.FilenameUtils;
21
+import org.apache.logging.log4j.util.Strings;
2122 import org.slf4j.Logger;
2223 import org.slf4j.LoggerFactory;
2324 import org.springframework.beans.factory.annotation.Autowired;
....@@ -165,10 +166,17 @@
165166 throw new RuntimeException("Errore caricamento classe dal json del descrittore", e);
166167 }
167168
168
- logger.debug("Controllo esistenza file sul db");
169
- RegDocument regDocument = regDocumentRepository.findOneByHashAndName(uploadedFileDescriptor.getSha256(), uploadedFileDescriptor.getFileName());
170
-
171169 String userName = cloudFileDescriptor.getParentFile().getName();
170
+
171
+ RegDocument regDocument = null;
172
+ if ( Strings.isBlank(uploadedFileDescriptor.getCustomKey()) == false ) {
173
+ logger.debug("Controllo esistenza file sul db tramite customKey {}", uploadedFileDescriptor.getCustomKey());
174
+ regDocument = regDocumentRepository.findOneByCustomKey(uploadedFileDescriptor.getCustomKey());
175
+ } else {
176
+ logger.debug("Controllo esistenza file sul db");
177
+ regDocument = regDocumentRepository.findOneByHashAndName(uploadedFileDescriptor.getSha256(),
178
+ uploadedFileDescriptor.getFileName(), userName);
179
+ }
172180
173181 File uploadedFile = new File(getUploadedFilePath(cloudFileDescriptorPath));
174182
....@@ -177,19 +185,20 @@
177185 logger.debug("Creazione nuovo record per il documento da conservare");
178186 regDocument = new RegDocument();
179187 regDocument.setCreatedBy(userName);
180
- regDocument.setModifiedBy(userName);
181
- regDocument.setFileHash(uploadedFileDescriptor.getSha256());
182
- regDocument.setFileName(uploadedFileDescriptor.getFileName());
183
- regDocument.setFileSize(uploadedFile.length());
184
- regDocument.setDueDate(uploadedFileDescriptor.getDueDate());
185
- regDocument.setFilePath("");
186
- regDocument.setSecretKey(uploadedFileDescriptor.getSecretKey());
187
-
188
- regDocumentRepository.save(regDocument);
189
-
190188 } else {
191189 logger.debug("Trovato 1 record per il documento da memorizzare");
192190 }
191
+
192
+ regDocument.setModifiedBy(userName);
193
+ regDocument.setFileHash(uploadedFileDescriptor.getSha256());
194
+ regDocument.setFileName(uploadedFileDescriptor.getFileName());
195
+ regDocument.setFileSize(uploadedFile.length());
196
+ regDocument.setDueDate(uploadedFileDescriptor.getDueDate());
197
+ regDocument.setFilePath("");
198
+ regDocument.setSecretKey(uploadedFileDescriptor.getSecretKey());
199
+ regDocument.setCustomKey(uploadedFileDescriptor.getCustomKey());
200
+
201
+ regDocumentRepository.save(regDocument);
193202
194203 try {
195204 moveUploadedFileToStorage(uploadedFile.getAbsolutePath(), regDocument);
....@@ -216,8 +225,6 @@
216225 private void moveUploadedFileToStorage( String source, RegDocument regDocument ) throws IOException {
217226 logger.debug("Spostamento file nello storage");
218227
219
- String sha256Descriptor = regDocument.getFileHash();
220
-
221228 File sourceFile = new File(source);
222229 File destinationFile = new File(generateStoragePath(regDocument));
223230
....@@ -226,15 +233,8 @@
226233 FileUtils.forceMkdirParent(destinationFile);
227234 FileUtils.copyFile(sourceFile, destinationFile);
228235 } else {
229
- logger.debug("Il file risulta gia' presente nello storage. Verifico l'integrita'");
230
- FileInputStream fis = new FileInputStream(destinationFile);
231
- String hash = DigestUtils.sha256Hex(fis);
232
- fis.close();
233
- if ( sha256Descriptor.equalsIgnoreCase(hash) == false ) {
234
- throw new IllegalStateException("L'hash del file nello storage non corrisponde con l'hash dichiarato nel descrittore del file:\n\t" +
235
- "hash file inviato: " + hash + "\n\t" +
236
- "hash dichiarato nel descrittore: " + sha256Descriptor);
237
- }
236
+ logger.debug("Il file risulta gia' presente nello storage. Aggiorno il file");
237
+ FileUtils.copyFile(sourceFile, destinationFile);
238238 }
239239
240240 regDocument.setFilePath(destinationFile.getAbsolutePath());