Эх сурвалжийг харах

Refactor download endpoints to use byte arrays

Changed DocumentClient and DownloadController to handle downloads as byte arrays instead of FileSystemResource. This improves compatibility with Feign clients and allows for more flexible file handling by writing the received bytes to temporary files before returning them as FileSystemResource.
Mario Martínez Hernández 3 долоо хоног өмнө
parent
commit
832c401f6e

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

@@ -49,11 +49,11 @@ 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 
-	public ResponseEntity<FileSystemResource> downloadReport(@PathVariable("t") Integer idTitulacio, @PathVariable("p") String nomProces) throws IOException, XDocReportException;
+	public ResponseEntity<byte[]> downloadReport(@PathVariable("t") Integer idTitulacio, @PathVariable("p") String nomProces) throws IOException, XDocReportException;
 
     @GetMapping(value="/download/template/{id}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody 

+ 19 - 3
src/main/java/es/uv/saic/web/DownloadController.java

@@ -61,8 +61,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());
 	}
 
 	/*
@@ -76,7 +84,15 @@ public class DownloadController {
 	@GetMapping(value="/download/report/{t}/{p}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
 	@ResponseBody 
 	public FileSystemResource downloadReport(Model model, @PathVariable("t") Integer idTitulacio, @PathVariable("p") String nomProces) throws IOException, XDocReportException {
-		return dc.downloadReport(idTitulacio, nomProces).getBody();
+		ResponseEntity<byte[]> response = dc.downloadReport(idTitulacio, nomProces);
+		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());
 	}