Browse Source

Download logic now on GUI

Mario Martínez Hernández 7 hours ago
parent
commit
9148850710

+ 3 - 2
src/main/java/es/uv/saic/feign/DocumentClient.java

@@ -20,6 +20,7 @@ import es.uv.saic.dto.CategoriaDTO;
 import es.uv.saic.dto.DocumentTmpDTO;
 import es.uv.saic.dto.PdfDTO;
 import fr.opensagres.xdocreport.core.XDocReportException;
+import jakarta.servlet.http.HttpServletResponse;
 
 @FeignClient(value = "document-service", url = "${saic.url.docs.domain}")
 public interface DocumentClient {
@@ -48,7 +49,7 @@ public interface DocumentClient {
 
     @GetMapping(value="/download/document/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody
-	public ResponseEntity<FileSystemResource> downloadDocument(@PathVariable("id") Integer idDocument) throws FileNotFoundException;
+	public ResponseEntity<byte[]> downloadDocument(@PathVariable("id") Integer idDocument) throws FileNotFoundException;
 
    	@GetMapping(value="/download/report/{t}/{p}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody 
@@ -56,7 +57,7 @@ public interface DocumentClient {
 
     @GetMapping(value="/download/template/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody 
-	public byte[] downloadTemplate(@PathVariable("id") BigInteger idTascai) throws IOException, XDocReportException;
+	public ResponseEntity<byte[]> downloadTemplate(@PathVariable("id") BigInteger idTascai) throws IOException, XDocReportException;
 
     @GetMapping(value="/test/template/{titulacio}/{centre}/{idProces}/{idTascap}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody

+ 21 - 4
src/main/java/es/uv/saic/web/DownloadController.java

@@ -50,6 +50,7 @@ public class DownloadController {
 		return new FileSystemResource(tempFile.toFile());
 	}					
 
+	// PARA BORRAR
 	/*
 	 * Download a document by its ID
 	 * @param model
@@ -59,8 +60,16 @@ public class DownloadController {
 	 */
 	@GetMapping(value="/download/document/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody
-	public FileSystemResource downloadDocument(Model model, @PathVariable("id") Integer idDocument) throws FileNotFoundException {		
-		return dc.downloadDocument(idDocument).getBody();
+	public FileSystemResource downloadDocument(Model model, @PathVariable("id") Integer idDocument) throws IOException {		
+		ResponseEntity<byte[]> response = dc.downloadDocument(idDocument);
+		byte[] data = response.getBody();
+		String fileName = response.getHeaders().getFirst("Content-Disposition").split("filename=")[1].replace("\"", "");
+
+		Path tempFile = Files.createTempFile( "download-", "-" + fileName);
+		Files.write(tempFile, data);
+		tempFile.toFile().deleteOnExit();
+
+		return new FileSystemResource(tempFile.toFile());
 	}
 
 	/*
@@ -72,8 +81,16 @@ public class DownloadController {
 	 */
 	@GetMapping(value="/download/template/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody 
-	public byte[] downloadTemplate(Model model, @PathVariable("id") BigInteger idTascai) throws IOException, XDocReportException {
-		return dc.downloadTemplate(idTascai);
+	public FileSystemResource downloadTemplate(Model model, @PathVariable("id") BigInteger idTascai) throws IOException, XDocReportException {
+		ResponseEntity<byte[]> response = dc.downloadTemplate(idTascai);
+		byte[] data = response.getBody();
+		String fileName = response.getHeaders().getFirst("Content-Disposition").split("filename=")[1].replace("\"", "");
+
+		Path tempFile = Files.createTempFile( "download-", "-" + fileName);
+		Files.write(tempFile, data);
+		tempFile.toFile().deleteOnExit();
+
+		return new FileSystemResource(tempFile.toFile());
 	}
 
 	/*