Commit 380e1eab by Jan Hrabal

w

parent ca44afd2
package com.jh.boot.attachment.api; package com.jh.boot.attachment.api;
import java.io.InputStream;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
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;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
...@@ -11,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody; ...@@ -11,6 +17,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import com.jh.boot.attachment.Attachment; import com.jh.boot.attachment.Attachment;
import com.jh.boot.attachment.AttachmentService; import com.jh.boot.attachment.AttachmentService;
import com.jh.boot.web.error.NotFoundException;
@Controller @Controller
@ConditionalOnBean(AttachmentService.class) @ConditionalOnBean(AttachmentService.class)
...@@ -26,5 +33,23 @@ public class AttachmentApiController { ...@@ -26,5 +33,23 @@ public class AttachmentApiController {
} }
@GetMapping("/attachments/{attachmentId}")
public ResponseEntity<InputStreamResource> fetch(@PathVariable("unitId") Long unitId, @PathVariable("objectType") String objectType, @PathVariable("objectId") Long objectId, @PathVariable("attachmentId") Long attachmentId) {
Attachment a = service.fetchAttachment(attachmentId);
if (a == null) {
throw new NotFoundException();
}
InputStream is = service.fetchAttachmentBody(a);
if (is == null) {
throw new NotFoundException();
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.set("Content-Disposition", "attachment; filename=\"" + a.getName() + "\"");
return new ResponseEntity<>(content, headers, HttpStatus.OK);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment