| .. | .. |
|---|
| 18 | 18 | import org.apache.commons.codec.digest.DigestUtils; |
|---|
| 19 | 19 | import org.apache.commons.io.FileUtils; |
|---|
| 20 | 20 | import org.apache.commons.io.FilenameUtils; |
|---|
| 21 | +import org.apache.logging.log4j.util.Strings; |
|---|
| 21 | 22 | import org.slf4j.Logger; |
|---|
| 22 | 23 | import org.slf4j.LoggerFactory; |
|---|
| 23 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
|---|
| .. | .. |
|---|
| 165 | 166 | throw new RuntimeException("Errore caricamento classe dal json del descrittore", e); |
|---|
| 166 | 167 | } |
|---|
| 167 | 168 | |
|---|
| 168 | | - logger.debug("Controllo esistenza file sul db"); |
|---|
| 169 | | - RegDocument regDocument = regDocumentRepository.findOneByHashAndName(uploadedFileDescriptor.getSha256(), uploadedFileDescriptor.getFileName()); |
|---|
| 170 | | - |
|---|
| 171 | 169 | 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 | + } |
|---|
| 172 | 180 | |
|---|
| 173 | 181 | File uploadedFile = new File(getUploadedFilePath(cloudFileDescriptorPath)); |
|---|
| 174 | 182 | |
|---|
| .. | .. |
|---|
| 177 | 185 | logger.debug("Creazione nuovo record per il documento da conservare"); |
|---|
| 178 | 186 | regDocument = new RegDocument(); |
|---|
| 179 | 187 | 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 | | - |
|---|
| 190 | 188 | } else { |
|---|
| 191 | 189 | logger.debug("Trovato 1 record per il documento da memorizzare"); |
|---|
| 192 | 190 | } |
|---|
| 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); |
|---|
| 193 | 202 | |
|---|
| 194 | 203 | try { |
|---|
| 195 | 204 | moveUploadedFileToStorage(uploadedFile.getAbsolutePath(), regDocument); |
|---|
| .. | .. |
|---|
| 216 | 225 | private void moveUploadedFileToStorage( String source, RegDocument regDocument ) throws IOException { |
|---|
| 217 | 226 | logger.debug("Spostamento file nello storage"); |
|---|
| 218 | 227 | |
|---|
| 219 | | - String sha256Descriptor = regDocument.getFileHash(); |
|---|
| 220 | | - |
|---|
| 221 | 228 | File sourceFile = new File(source); |
|---|
| 222 | 229 | File destinationFile = new File(generateStoragePath(regDocument)); |
|---|
| 223 | 230 | |
|---|
| .. | .. |
|---|
| 226 | 233 | FileUtils.forceMkdirParent(destinationFile); |
|---|
| 227 | 234 | FileUtils.copyFile(sourceFile, destinationFile); |
|---|
| 228 | 235 | } 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); |
|---|
| 238 | 238 | } |
|---|
| 239 | 239 | |
|---|
| 240 | 240 | regDocument.setFilePath(destinationFile.getAbsolutePath()); |
|---|