| .. | .. |
|---|
| 1 | 1 | package it.digione.dg1cloud.controller; |
|---|
| 2 | 2 | |
|---|
| 3 | | -import java.io.File; |
|---|
| 4 | | -import java.io.FileInputStream; |
|---|
| 5 | 3 | import java.io.IOException; |
|---|
| 6 | 4 | |
|---|
| 7 | | -import javax.servlet.ServletContext; |
|---|
| 8 | | - |
|---|
| 9 | | -import org.apache.logging.log4j.util.Strings; |
|---|
| 10 | 5 | import org.slf4j.Logger; |
|---|
| 11 | 6 | import org.slf4j.LoggerFactory; |
|---|
| 12 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
|---|
| 13 | 8 | import org.springframework.core.io.InputStreamResource; |
|---|
| 14 | | -import org.springframework.http.HttpHeaders; |
|---|
| 15 | | -import org.springframework.http.MediaType; |
|---|
| 16 | 9 | import org.springframework.http.ResponseEntity; |
|---|
| 17 | 10 | import org.springframework.stereotype.Controller; |
|---|
| 18 | 11 | import org.springframework.web.bind.annotation.RequestMapping; |
|---|
| 19 | 12 | import org.springframework.web.bind.annotation.RequestParam; |
|---|
| 20 | 13 | |
|---|
| 21 | | -import it.digione.dg1cloud.model.RegDocument; |
|---|
| 22 | | -import it.digione.dg1cloud.repository.RegDocumentRepository; |
|---|
| 23 | | -import it.digione.dg1cloud.service.CloudService; |
|---|
| 14 | +import it.digione.dg1cloud.recaptcha.CaptchaSettings; |
|---|
| 15 | +import it.digione.dg1cloud.service.Utils; |
|---|
| 24 | 16 | |
|---|
| 25 | 17 | @Controller |
|---|
| 26 | 18 | public class DownloadFileController { |
|---|
| 27 | 19 | |
|---|
| 28 | | - private static final Logger logger = LoggerFactory.getLogger(CloudService.class); |
|---|
| 20 | + private static final Logger logger = LoggerFactory.getLogger(DownloadFileController.class); |
|---|
| 29 | 21 | |
|---|
| 30 | | - @Autowired private ServletContext servletContext; |
|---|
| 31 | | - @Autowired private RegDocumentRepository regDocumentRepository; |
|---|
| 22 | + @Autowired private Utils utils; |
|---|
| 23 | + @Autowired CaptchaSettings captchaSettings; |
|---|
| 32 | 24 | |
|---|
| 33 | 25 | @RequestMapping("/downloadFile") |
|---|
| 34 | 26 | public ResponseEntity<InputStreamResource> downloadFile(@RequestParam String fileName, |
|---|
| 35 | | - @RequestParam long id, |
|---|
| 36 | | - @RequestParam(value = "secretKey", required = false) String secretKey) throws IOException { |
|---|
| 27 | + @RequestParam long id, |
|---|
| 28 | + @RequestParam(value = "secretKey", required = false) String secretKey) throws IOException { |
|---|
| 37 | 29 | |
|---|
| 38 | | - logger.debug("Avvio download file {} con id {}, fileName, id"); |
|---|
| 39 | | - |
|---|
| 40 | | - MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM; |
|---|
| 41 | | - try { |
|---|
| 42 | | - mediaType = MediaType.parseMediaType(servletContext.getMimeType(fileName)); |
|---|
| 43 | | - } catch (Exception e) { |
|---|
| 44 | | - logger.warn("Errore nello stabilire il mime type del file. VerrĂ usato " + mediaType.toString(), e); |
|---|
| 30 | + if ( captchaSettings.isEnabled() == true ) { |
|---|
| 31 | + String error = "Download diretto non ammesso. Usare la pagina di download tramite reCaptcha"; |
|---|
| 32 | + logger.error(error); |
|---|
| 33 | + throw new RuntimeException("Download diretto non ammesso. Usare la pagina di download tramite reCaptcha"); |
|---|
| 45 | 34 | } |
|---|
| 46 | 35 | |
|---|
| 47 | | - RegDocument regDocument = regDocumentRepository.getOne(id); |
|---|
| 48 | | - |
|---|
| 49 | | - if (regDocument.getFileName().equalsIgnoreCase(fileName) == false ) { |
|---|
| 50 | | - logger.error("Il nome del file richiesto non corrisponde con quello referenziato dall'id"); |
|---|
| 51 | | - throw new RuntimeException("Il nome del file richiesto non corrisponde con quello referenziato dall'id"); |
|---|
| 52 | | - } |
|---|
| 53 | | - |
|---|
| 54 | | - if ( regDocument.getSecretKey() != null ) { |
|---|
| 55 | | - logger.debug("E' stata specificata una secretKey. Avvio le verifiche."); |
|---|
| 56 | | - if ( Strings.isEmpty(secretKey) == true ) { |
|---|
| 57 | | - logger.error("Non e' stata inviata la secretKey"); |
|---|
| 58 | | - throw new RuntimeException("Per scaricare il file occorre specificare la secretKey"); |
|---|
| 59 | | - } else { |
|---|
| 60 | | - logger.debug("Controllo corrispondenza della secretKey"); |
|---|
| 61 | | - if (secretKey.equals(regDocument.getSecretKey()) == false ) { |
|---|
| 62 | | - logger.error("La secretKey inviata non corrisponde a quella impostata in fase di richiesta salvataggio del file"); |
|---|
| 63 | | - throw new RuntimeException("La secretKey inviata non corrisponde a quella impostata in fase di richiesta salvataggio del file"); |
|---|
| 64 | | - } else { |
|---|
| 65 | | - logger.debug("SecretKey verificata correttamente"); |
|---|
| 66 | | - } |
|---|
| 67 | | - } |
|---|
| 68 | | - } |
|---|
| 69 | | - |
|---|
| 70 | | - File file = new File(regDocument.getFilePath()); |
|---|
| 71 | | - FileInputStream fis = new FileInputStream(file); |
|---|
| 72 | | - InputStreamResource isr = new InputStreamResource(fis); |
|---|
| 73 | | - return ResponseEntity.ok() |
|---|
| 74 | | - // Content-Disposition |
|---|
| 75 | | - .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName()) |
|---|
| 76 | | - // Content-Type |
|---|
| 77 | | - .contentType(mediaType) |
|---|
| 78 | | - // Contet-Length |
|---|
| 79 | | - .contentLength(file.length()) // |
|---|
| 80 | | - .body(isr); |
|---|
| 36 | + return utils.getDownloadResponseEntity(fileName, id, secretKey); |
|---|
| 81 | 37 | } |
|---|
| 38 | + |
|---|
| 39 | + |
|---|
| 82 | 40 | } |
|---|