Skip to content

Commit

Permalink
feat: file will be downloaded automatically if download-flag is true
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-urner committed Jul 9, 2024
1 parent a0f3dfc commit ec7f974
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/dev/urner/volodb/rest/FileRestController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit ec7f974

Please sign in to comment.