DownloadController.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package es.uv.saic.web;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.math.BigInteger;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.util.Optional;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.core.io.FileSystemResource;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PathVariable;
  16. import org.springframework.web.bind.annotation.PostMapping;
  17. import org.springframework.web.bind.annotation.RequestParam;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import es.uv.saic.dto.PdfDTO;
  20. import es.uv.saic.feign.DocumentClient;
  21. import fr.opensagres.xdocreport.core.XDocReportException;
  22. @Controller
  23. public class DownloadController {
  24. @Autowired
  25. private DocumentClient dc;
  26. /*
  27. * Download a file associated with a task instance
  28. * @param model
  29. * @param idInstanciaTasca The ID of the task instance
  30. * @param response HttpServletResponse
  31. * @return A FileSystemResource representing the file to download
  32. */
  33. @GetMapping(value="/download/{fileName}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  34. @ResponseBody
  35. public FileSystemResource download(Model model, @PathVariable("fileName") BigInteger idInstanciaTasca) throws IOException {
  36. // Convert byte[] to FileSystemResource
  37. ResponseEntity<byte[]> response = dc.download(idInstanciaTasca);
  38. byte[] data = response.getBody();
  39. String fileName = response.getHeaders().getFirst("Content-Disposition").split("filename=")[1].replace("\"", "");
  40. Path tempFile = Files.createTempFile( "download-", "-" + fileName);
  41. Files.write(tempFile, data);
  42. tempFile.toFile().deleteOnExit();
  43. return new FileSystemResource(tempFile.toFile());
  44. }
  45. /*
  46. * Download a document by its ID
  47. * @param model
  48. * @param idDocument The ID of the document to download
  49. * @param response HttpServletResponse
  50. * @return A FileSystemResource representing the document to download
  51. */
  52. @GetMapping(value="/download/document/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  53. @ResponseBody
  54. public FileSystemResource downloadDocument(Model model, @PathVariable("id") Integer idDocument) throws FileNotFoundException {
  55. return dc.downloadDocument(idDocument).getBody();
  56. }
  57. /*
  58. * Download a populated template for a given task instance
  59. * @param model
  60. * @param idTascai The ID of the task instance
  61. * @param response HttpServletResponse
  62. * @return A byte array representing the populated template to download
  63. */
  64. @GetMapping(value="/download/template/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  65. @ResponseBody
  66. public byte[] downloadTemplate(Model model, @PathVariable("id") BigInteger idTascai) throws IOException, XDocReportException {
  67. return dc.downloadTemplate(idTascai);
  68. }
  69. /*
  70. * Generate a PDF preview from provided content
  71. * @param model
  72. * @param content The content to convert to PDF
  73. * @param idtascai Optional ID of the task instance for context
  74. * @param response HttpServletResponse
  75. */
  76. @PostMapping(value="/pdf/preview")
  77. @ResponseBody
  78. public byte[] downloadTemplatePdf(Model model,
  79. @RequestParam("content") String content, @RequestParam("idtascai") Optional<BigInteger> idtascai) throws IOException, InterruptedException {
  80. PdfDTO pdf = new PdfDTO(content, idtascai);
  81. return dc.downloadTemplatePdf(pdf);
  82. }
  83. }