소스 검색

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 주 전
부모
커밋
832c401f6e
2개의 변경된 파일21개의 추가작업 그리고 5개의 파일을 삭제
  1. 2 2
      src/main/java/es/uv/saic/feign/DocumentClient.java
  2. 19 3
      src/main/java/es/uv/saic/web/DownloadController.java

+ 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());
 	}