Skip to content

Commit

Permalink
avniproject/avni-media#174 - endpoint that can be used to download a …
Browse files Browse the repository at this point in the history
…file.
  • Loading branch information
petmongrels authored and shraddha761 committed Jul 3, 2024
1 parent aaa738c commit c12c937
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -25,6 +27,7 @@
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
Expand Down Expand Up @@ -179,4 +182,14 @@ private boolean isInvalidDimension(File tempSourceFile, AvniFiles.ImageType imag
Dimension dimension = AvniFiles.getImageDimension(tempSourceFile, imageType);
return dimension.getHeight() > 75 || dimension.getWidth() > 75;
}

@GetMapping("/web/media/downloadStream")
public ResponseEntity<InputStreamResource> downloadFile(@RequestParam String s3Url, @RequestParam String fileName) throws IOException {
InputStreamResource resource = new InputStreamResource(s3Service.getObjectContentFromUrl(s3Url));

return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);
}
}

0 comments on commit c12c937

Please sign in to comment.