From ec7f974783fce19ba7fb0df835e37b18680f7cec Mon Sep 17 00:00:00 2001 From: Fabian Urner Date: Tue, 9 Jul 2024 08:28:25 +0200 Subject: [PATCH] feat: file will be downloaded automatically if download-flag is true --- .../java/dev/urner/volodb/rest/FileRestController.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/dev/urner/volodb/rest/FileRestController.java b/src/main/java/dev/urner/volodb/rest/FileRestController.java index 0c8529d..651f994 100644 --- a/src/main/java/dev/urner/volodb/rest/FileRestController.java +++ b/src/main/java/dev/urner/volodb/rest/FileRestController.java @@ -1,6 +1,7 @@ package dev.urner.volodb.rest; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import dev.urner.volodb.service.FileService; @@ -28,15 +29,18 @@ public class FileRestController { @GetMapping("/{bucket}/**") public void getTestMethod(@PathVariable("bucket") String bucket, HttpServletRequest request, - HttpServletResponse response) throws IOException { + HttpServletResponse response, @RequestParam(name = "download", defaultValue = "true") Boolean download) + throws IOException { // Extract object-string from URL String object = request.getRequestURI().split(bucket)[1]; InputStream inputStream = fileService.getFile(bucket, object); - // response.addHeader("Content-disposition", "attachment;filename=" + object); - // /* <- force the browser to download the file */ + // force the browser to download the file + if (download) + response.addHeader("Content-disposition", "attachment;filename=" + object); + response.setContentType(URLConnection.guessContentTypeFromName(object)); IOUtils.copy(inputStream, response.getOutputStream());