Leonardo Vannucci
2019-04-30 d50b201eb6593d5090e3cad496895ced88a5a7c3
dg1cloud-core/src/main/java/it/digione/dg1cloud/service/CloudService.java
....@@ -171,7 +171,7 @@
171171 RegDocument regDocument = null;
172172 if ( Strings.isBlank(uploadedFileDescriptor.getCustomKey()) == false ) {
173173 logger.debug("Controllo esistenza file sul db tramite customKey {}", uploadedFileDescriptor.getCustomKey());
174
- regDocument = regDocumentRepository.findOneByCustomKey(uploadedFileDescriptor.getCustomKey());
174
+ regDocument = regDocumentRepository.findOneByCustomKey(uploadedFileDescriptor.getCustomKey(), userName);
175175 } else {
176176 logger.debug("Controllo esistenza file sul db");
177177 regDocument = regDocumentRepository.findOneByHashAndName(uploadedFileDescriptor.getSha256(),
....@@ -181,7 +181,6 @@
181181 File uploadedFile = new File(getUploadedFilePath(cloudFileDescriptorPath));
182182
183183 if ( regDocument == null ) {
184
-
185184 logger.debug("Creazione nuovo record per il documento da conservare");
186185 regDocument = new RegDocument();
187186 regDocument.setCreatedBy(userName);
....@@ -189,19 +188,28 @@
189188 logger.debug("Trovato 1 record per il documento da memorizzare");
190189 }
191190
191
+ String oldFilePath = null;
192
+
193
+ if ( Strings.isBlank(regDocument.getFilePath()) == false ) {
194
+ oldFilePath = regDocument.getFilePath();
195
+ }
196
+
192197 regDocument.setModifiedBy(userName);
193198 regDocument.setFileHash(uploadedFileDescriptor.getSha256());
194
- regDocument.setFileName(uploadedFileDescriptor.getFileName());
199
+ if ( Strings.isBlank(uploadedFileDescriptor.getCustomKey()) == true ||
200
+ Strings.isBlank(regDocument.getFileName() ) == true ) {
201
+ regDocument.setFileName(uploadedFileDescriptor.getFileName());
202
+ regDocument.setSecretKey(uploadedFileDescriptor.getSecretKey());
203
+ }
195204 regDocument.setFileSize(uploadedFile.length());
196205 regDocument.setDueDate(uploadedFileDescriptor.getDueDate());
197206 regDocument.setFilePath("");
198
- regDocument.setSecretKey(uploadedFileDescriptor.getSecretKey());
199207 regDocument.setCustomKey(uploadedFileDescriptor.getCustomKey());
200208
201209 regDocumentRepository.save(regDocument);
202210
203211 try {
204
- moveUploadedFileToStorage(uploadedFile.getAbsolutePath(), regDocument);
212
+ moveUploadedFileToStorage(uploadedFile.getAbsolutePath(), regDocument, uploadedFileDescriptor.getFileName());
205213 } catch (Exception e) {
206214 try {
207215 writeKoFile(cloudFileDescriptorPath, e.getMessage());
....@@ -210,6 +218,14 @@
210218 regDocumentRepository.delete(regDocument);
211219 }
212220 }
221
+
222
+ if ( Strings.isBlank(oldFilePath) == false && regDocument.getFilePath().equalsIgnoreCase(oldFilePath) == false ) {
223
+ logger.debug("Il file path e' cambiato. Rimuovo il vecchio file");
224
+ File oldFile = new File( oldFilePath );
225
+ if (oldFile.exists() == true )
226
+ FileUtils.deleteQuietly(oldFile);
227
+ }
228
+
213229 try {
214230 createOkFile(cloudFileDescriptorPath, regDocument);
215231 } catch (Exception e) {
....@@ -222,11 +238,11 @@
222238 }
223239 }
224240
225
- private void moveUploadedFileToStorage( String source, RegDocument regDocument ) throws IOException {
241
+ private void moveUploadedFileToStorage( String source, RegDocument regDocument, String fileName ) throws IOException {
226242 logger.debug("Spostamento file nello storage");
227243
228244 File sourceFile = new File(source);
229
- File destinationFile = new File(generateStoragePath(regDocument));
245
+ File destinationFile = new File(generateStoragePath(regDocument, fileName));
230246
231247 if ( destinationFile.exists() == false ) {
232248 logger.debug("Creazione alberatura directory: " + destinationFile.getParent());
....@@ -264,9 +280,14 @@
264280 String json = mapper.writeValueAsString(uploadedFileDescriptorOk);
265281 logger.debug("Scrittura json di ok:\n{}", json);
266282 FileUtils.writeStringToFile(okFile, json, StandardCharsets.UTF_8);
267
- set777(okFile);
268283 } catch (IOException e) {
269284 throw new RuntimeException("Errore nella scrittura del file di OK", e);
285
+ }
286
+
287
+ try {
288
+ set777(okFile);
289
+ } catch (Exception e) {
290
+ logger.error("Non e' stato possibile settare i permessi di scrittura 777", e);
270291 }
271292
272293 File cloudFile = new File( getUploadedFilePath(cloudFileDescriptorPath) );
....@@ -309,7 +330,7 @@
309330 return fileDescriptor.getParentFile().getAbsolutePath() + File.separator + FilenameUtils.getBaseName(fileDescriptor.getName());
310331 }
311332
312
- private String generateStoragePath( RegDocument regDocument ) {
333
+ private String generateStoragePath( RegDocument regDocument, String fileName ) {
313334
314335 if ( regDocument.getCreated() == null )
315336 throw new IllegalStateException("La data di creazione del RegDocument non e' valorizzata");
....@@ -332,7 +353,7 @@
332353 sb.append(File.separator);
333354 sb.append(regDocument.getDocumentId());
334355 sb.append(File.separator);
335
- sb.append(regDocument.getFileName());
356
+ sb.append(fileName);
336357
337358 return sb.toString();
338359 }