Leonardo Vannucci
2018-08-09 b5e4a0dc28966479e0f2d5c0d69f2f38f734fee1
dg1cloud-core/src/main/java/it/digione/dg1cloud/controller/DownloadFileController.java
....@@ -1,82 +1,40 @@
11 package it.digione.dg1cloud.controller;
22
3
-import java.io.File;
4
-import java.io.FileInputStream;
53 import java.io.IOException;
64
7
-import javax.servlet.ServletContext;
8
-
9
-import org.apache.logging.log4j.util.Strings;
105 import org.slf4j.Logger;
116 import org.slf4j.LoggerFactory;
127 import org.springframework.beans.factory.annotation.Autowired;
138 import org.springframework.core.io.InputStreamResource;
14
-import org.springframework.http.HttpHeaders;
15
-import org.springframework.http.MediaType;
169 import org.springframework.http.ResponseEntity;
1710 import org.springframework.stereotype.Controller;
1811 import org.springframework.web.bind.annotation.RequestMapping;
1912 import org.springframework.web.bind.annotation.RequestParam;
2013
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;
2416
2517 @Controller
2618 public class DownloadFileController {
2719
28
- private static final Logger logger = LoggerFactory.getLogger(CloudService.class);
20
+ private static final Logger logger = LoggerFactory.getLogger(DownloadFileController.class);
2921
30
- @Autowired private ServletContext servletContext;
31
- @Autowired private RegDocumentRepository regDocumentRepository;
22
+ @Autowired private Utils utils;
23
+ @Autowired CaptchaSettings captchaSettings;
3224
3325 @RequestMapping("/downloadFile")
3426 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 {
3729
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");
4534 }
4635
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);
8137 }
38
+
39
+
8240 }