| .. | .. |
|---|
| 1 | 1 | package it.digione.dg1cloud.service; |
|---|
| 2 | 2 | |
|---|
| 3 | 3 | import java.io.File; |
|---|
| 4 | +import java.io.FileInputStream; |
|---|
| 5 | +import java.io.FileNotFoundException; |
|---|
| 4 | 6 | import java.io.IOException; |
|---|
| 5 | 7 | import java.net.MalformedURLException; |
|---|
| 6 | 8 | import java.net.URL; |
|---|
| 9 | + |
|---|
| 10 | +import javax.servlet.ServletContext; |
|---|
| 7 | 11 | |
|---|
| 8 | 12 | import org.apache.commons.io.FileUtils; |
|---|
| 9 | 13 | import org.apache.logging.log4j.util.Strings; |
|---|
| 10 | 14 | import org.slf4j.Logger; |
|---|
| 11 | 15 | import org.slf4j.LoggerFactory; |
|---|
| 12 | 16 | import org.springframework.beans.factory.annotation.Autowired; |
|---|
| 17 | +import org.springframework.core.io.InputStreamResource; |
|---|
| 18 | +import org.springframework.http.HttpHeaders; |
|---|
| 19 | +import org.springframework.http.MediaType; |
|---|
| 20 | +import org.springframework.http.ResponseEntity; |
|---|
| 13 | 21 | import org.springframework.stereotype.Service; |
|---|
| 14 | 22 | import org.springframework.web.util.UriBuilder; |
|---|
| 15 | 23 | import org.springframework.web.util.UriComponentsBuilder; |
|---|
| 16 | 24 | |
|---|
| 17 | 25 | import it.digione.dg1cloud.config.AppConfig; |
|---|
| 18 | 26 | import it.digione.dg1cloud.model.RegDocument; |
|---|
| 27 | +import it.digione.dg1cloud.repository.RegDocumentRepository; |
|---|
| 19 | 28 | |
|---|
| 20 | 29 | @Service("utils") |
|---|
| 21 | 30 | public class Utils { |
|---|
| 22 | 31 | |
|---|
| 23 | 32 | @Autowired private AppConfig appConfig; |
|---|
| 33 | + @Autowired private RegDocumentRepository regDocumentRepository; |
|---|
| 34 | + @Autowired private ServletContext servletContext; |
|---|
| 24 | 35 | |
|---|
| 25 | 36 | private static final Logger logger = LoggerFactory.getLogger(CloudService.class); |
|---|
| 26 | 37 | |
|---|
| 27 | 38 | public URL generateUrl(RegDocument regDocument) throws MalformedURLException { |
|---|
| 28 | 39 | logger.debug("Genero l'url per il record file {} con id {}", regDocument.getFileName(), regDocument.getDocumentId()); |
|---|
| 29 | 40 | UriBuilder uriBuilder = UriComponentsBuilder.fromUriString(appConfig.getExternalBaseUrl()); |
|---|
| 30 | | - uriBuilder.path("/downloadFile"); |
|---|
| 41 | + uriBuilder.path("/downloadPage"); |
|---|
| 31 | 42 | uriBuilder.queryParam("fileName", regDocument.getFileName()); |
|---|
| 32 | 43 | uriBuilder.queryParam("id", regDocument.getDocumentId()); |
|---|
| 33 | 44 | if ( Strings.isEmpty(regDocument.getSecretKey()) == false ) { |
|---|
| .. | .. |
|---|
| 53 | 64 | logger.debug("La directory {} non e' vuota", directory.getAbsolutePath()); |
|---|
| 54 | 65 | } |
|---|
| 55 | 66 | } |
|---|
| 67 | + |
|---|
| 68 | + public ResponseEntity<InputStreamResource> getDownloadResponseEntity(String fileName, long id, String secretKey) |
|---|
| 69 | + throws FileNotFoundException { |
|---|
| 70 | + logger.debug("Avvio download file {} con id {}", fileName, id); |
|---|
| 71 | + |
|---|
| 72 | + MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM; |
|---|
| 73 | + try { |
|---|
| 74 | + mediaType = MediaType.parseMediaType(servletContext.getMimeType(fileName)); |
|---|
| 75 | + } catch (Exception e) { |
|---|
| 76 | + logger.warn("Errore nello stabilire il mime type del file. VerrĂ usato " + mediaType.toString(), e); |
|---|
| 77 | + } |
|---|
| 78 | + |
|---|
| 79 | + RegDocument regDocument = regDocumentRepository.getOne(id); |
|---|
| 80 | + |
|---|
| 81 | + if (regDocument.getFileName().equalsIgnoreCase(fileName) == false ) { |
|---|
| 82 | + logger.error("Il nome del file richiesto non corrisponde con quello referenziato dall'id"); |
|---|
| 83 | + throw new RuntimeException("Il nome del file richiesto non corrisponde con quello referenziato dall'id"); |
|---|
| 84 | + } |
|---|
| 85 | + |
|---|
| 86 | + if ( regDocument.getSecretKey() != null ) { |
|---|
| 87 | + logger.debug("E' stata specificata una secretKey. Avvio le verifiche."); |
|---|
| 88 | + if ( Strings.isEmpty(secretKey) == true ) { |
|---|
| 89 | + logger.error("Non e' stata inviata la secretKey"); |
|---|
| 90 | + throw new RuntimeException("Per scaricare il file occorre specificare la secretKey"); |
|---|
| 91 | + } else { |
|---|
| 92 | + logger.debug("Controllo corrispondenza della secretKey"); |
|---|
| 93 | + if (secretKey.equals(regDocument.getSecretKey()) == false ) { |
|---|
| 94 | + logger.error("La secretKey inviata non corrisponde a quella impostata in fase di richiesta salvataggio del file"); |
|---|
| 95 | + throw new RuntimeException("La secretKey inviata non corrisponde a quella impostata in fase di richiesta salvataggio del file"); |
|---|
| 96 | + } else { |
|---|
| 97 | + logger.debug("SecretKey verificata correttamente"); |
|---|
| 98 | + } |
|---|
| 99 | + } |
|---|
| 100 | + } |
|---|
| 101 | + |
|---|
| 102 | + File file = new File(regDocument.getFilePath()); |
|---|
| 103 | + FileInputStream fis = new FileInputStream(file); |
|---|
| 104 | + InputStreamResource isr = new InputStreamResource(fis); |
|---|
| 105 | + return ResponseEntity.ok() |
|---|
| 106 | + // Content-Disposition |
|---|
| 107 | + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName()) |
|---|
| 108 | + // Content-Type |
|---|
| 109 | + .contentType(mediaType) |
|---|
| 110 | + // Contet-Length |
|---|
| 111 | + .contentLength(file.length()) // |
|---|
| 112 | + .body(isr); |
|---|
| 113 | + } |
|---|
| 56 | 114 | } |
|---|