| .. | .. |
|---|
| 171 | 171 | RegDocument regDocument = null; |
|---|
| 172 | 172 | if ( Strings.isBlank(uploadedFileDescriptor.getCustomKey()) == false ) { |
|---|
| 173 | 173 | logger.debug("Controllo esistenza file sul db tramite customKey {}", uploadedFileDescriptor.getCustomKey()); |
|---|
| 174 | | - regDocument = regDocumentRepository.findOneByCustomKey(uploadedFileDescriptor.getCustomKey()); |
|---|
| 174 | + regDocument = regDocumentRepository.findOneByCustomKey(uploadedFileDescriptor.getCustomKey(), userName); |
|---|
| 175 | 175 | } else { |
|---|
| 176 | 176 | logger.debug("Controllo esistenza file sul db"); |
|---|
| 177 | 177 | regDocument = regDocumentRepository.findOneByHashAndName(uploadedFileDescriptor.getSha256(), |
|---|
| .. | .. |
|---|
| 181 | 181 | File uploadedFile = new File(getUploadedFilePath(cloudFileDescriptorPath)); |
|---|
| 182 | 182 | |
|---|
| 183 | 183 | if ( regDocument == null ) { |
|---|
| 184 | | - |
|---|
| 185 | 184 | logger.debug("Creazione nuovo record per il documento da conservare"); |
|---|
| 186 | 185 | regDocument = new RegDocument(); |
|---|
| 187 | 186 | regDocument.setCreatedBy(userName); |
|---|
| .. | .. |
|---|
| 189 | 188 | logger.debug("Trovato 1 record per il documento da memorizzare"); |
|---|
| 190 | 189 | } |
|---|
| 191 | 190 | |
|---|
| 191 | + String oldFilePath = null; |
|---|
| 192 | + |
|---|
| 193 | + if ( Strings.isBlank(regDocument.getFilePath()) == false ) { |
|---|
| 194 | + oldFilePath = regDocument.getFilePath(); |
|---|
| 195 | + } |
|---|
| 196 | + |
|---|
| 192 | 197 | regDocument.setModifiedBy(userName); |
|---|
| 193 | 198 | 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 | + } |
|---|
| 195 | 204 | regDocument.setFileSize(uploadedFile.length()); |
|---|
| 196 | 205 | regDocument.setDueDate(uploadedFileDescriptor.getDueDate()); |
|---|
| 197 | 206 | regDocument.setFilePath(""); |
|---|
| 198 | | - regDocument.setSecretKey(uploadedFileDescriptor.getSecretKey()); |
|---|
| 199 | 207 | regDocument.setCustomKey(uploadedFileDescriptor.getCustomKey()); |
|---|
| 200 | 208 | |
|---|
| 201 | 209 | regDocumentRepository.save(regDocument); |
|---|
| 202 | 210 | |
|---|
| 203 | 211 | try { |
|---|
| 204 | | - moveUploadedFileToStorage(uploadedFile.getAbsolutePath(), regDocument); |
|---|
| 212 | + moveUploadedFileToStorage(uploadedFile.getAbsolutePath(), regDocument, uploadedFileDescriptor.getFileName()); |
|---|
| 205 | 213 | } catch (Exception e) { |
|---|
| 206 | 214 | try { |
|---|
| 207 | 215 | writeKoFile(cloudFileDescriptorPath, e.getMessage()); |
|---|
| .. | .. |
|---|
| 210 | 218 | regDocumentRepository.delete(regDocument); |
|---|
| 211 | 219 | } |
|---|
| 212 | 220 | } |
|---|
| 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 | + |
|---|
| 213 | 229 | try { |
|---|
| 214 | 230 | createOkFile(cloudFileDescriptorPath, regDocument); |
|---|
| 215 | 231 | } catch (Exception e) { |
|---|
| .. | .. |
|---|
| 222 | 238 | } |
|---|
| 223 | 239 | } |
|---|
| 224 | 240 | |
|---|
| 225 | | - private void moveUploadedFileToStorage( String source, RegDocument regDocument ) throws IOException { |
|---|
| 241 | + private void moveUploadedFileToStorage( String source, RegDocument regDocument, String fileName ) throws IOException { |
|---|
| 226 | 242 | logger.debug("Spostamento file nello storage"); |
|---|
| 227 | 243 | |
|---|
| 228 | 244 | File sourceFile = new File(source); |
|---|
| 229 | | - File destinationFile = new File(generateStoragePath(regDocument)); |
|---|
| 245 | + File destinationFile = new File(generateStoragePath(regDocument, fileName)); |
|---|
| 230 | 246 | |
|---|
| 231 | 247 | if ( destinationFile.exists() == false ) { |
|---|
| 232 | 248 | logger.debug("Creazione alberatura directory: " + destinationFile.getParent()); |
|---|
| .. | .. |
|---|
| 264 | 280 | String json = mapper.writeValueAsString(uploadedFileDescriptorOk); |
|---|
| 265 | 281 | logger.debug("Scrittura json di ok:\n{}", json); |
|---|
| 266 | 282 | FileUtils.writeStringToFile(okFile, json, StandardCharsets.UTF_8); |
|---|
| 267 | | - set777(okFile); |
|---|
| 268 | 283 | } catch (IOException e) { |
|---|
| 269 | 284 | 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); |
|---|
| 270 | 291 | } |
|---|
| 271 | 292 | |
|---|
| 272 | 293 | File cloudFile = new File( getUploadedFilePath(cloudFileDescriptorPath) ); |
|---|
| .. | .. |
|---|
| 309 | 330 | return fileDescriptor.getParentFile().getAbsolutePath() + File.separator + FilenameUtils.getBaseName(fileDescriptor.getName()); |
|---|
| 310 | 331 | } |
|---|
| 311 | 332 | |
|---|
| 312 | | - private String generateStoragePath( RegDocument regDocument ) { |
|---|
| 333 | + private String generateStoragePath( RegDocument regDocument, String fileName ) { |
|---|
| 313 | 334 | |
|---|
| 314 | 335 | if ( regDocument.getCreated() == null ) |
|---|
| 315 | 336 | throw new IllegalStateException("La data di creazione del RegDocument non e' valorizzata"); |
|---|
| .. | .. |
|---|
| 332 | 353 | sb.append(File.separator); |
|---|
| 333 | 354 | sb.append(regDocument.getDocumentId()); |
|---|
| 334 | 355 | sb.append(File.separator); |
|---|
| 335 | | - sb.append(regDocument.getFileName()); |
|---|
| 356 | + sb.append(fileName); |
|---|
| 336 | 357 | |
|---|
| 337 | 358 | return sb.toString(); |
|---|
| 338 | 359 | } |
|---|