Commit cfc11eb8 by Jan Hrabal

attachments

parent d5e6d62e
...@@ -39,3 +39,4 @@ boot-react-common/.apt_generated ...@@ -39,3 +39,4 @@ boot-react-common/.apt_generated
boot-react-common/.settings boot-react-common/.settings
boot-react-common/target boot-react-common/target
/.idea/ /.idea/
/jh-boot.iml
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.ConditionalOnProperty;
import org.springframework.core.io.InputStreamResource; 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.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;
...@@ -17,15 +13,17 @@ import org.springframework.web.bind.annotation.ResponseBody; ...@@ -17,15 +13,17 @@ 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) @ConditionalOnProperty(value = "attachments.api.enabled", havingValue = "true", matchIfMissing = true)
public class AttachmentApiController { public class AttachmentApiController {
@Autowired @Autowired
private AttachmentService service; private AttachmentService service;
@Autowired
private AttachmentContentProvider contentProvider;
@GetMapping("/attachments/{unitId}/{objectType}/{objectId}") @GetMapping("/attachments/{unitId}/{objectType}/{objectId}")
public @ResponseBody List<Attachment> listAttachments(@PathVariable("unitId") Long unitId, @PathVariable("objectType") String objectType, @PathVariable("objectId") Long objectId) { public @ResponseBody List<Attachment> listAttachments(@PathVariable("unitId") Long unitId, @PathVariable("objectType") String objectType, @PathVariable("objectId") Long objectId) {
...@@ -33,23 +31,9 @@ public class AttachmentApiController { ...@@ -33,23 +31,9 @@ public class AttachmentApiController {
} }
@GetMapping("/attachments/{attachmentId}") @GetMapping("/attachments/{attachmentId}/data")
public ResponseEntity<InputStreamResource> fetch(@PathVariable("unitId") Long unitId, @PathVariable("objectType") String objectType, @PathVariable("objectId") Long objectId, @PathVariable("attachmentId") Long attachmentId) { public ResponseEntity<InputStreamResource> fetch(@PathVariable("attachmentId") Long attachmentId) {
Attachment a = service.fetchAttachment(attachmentId); return contentProvider.fetch(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);
} }
} }
package com.jh.boot.attachment.api;
import java.io.InputStream;
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;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.jh.boot.attachment.Attachment;
import com.jh.boot.attachment.AttachmentService;
import com.jh.boot.web.error.NotFoundException;
@Component
public class AttachmentContentProvider {
private AttachmentService service;
public ResponseEntity<InputStreamResource> fetch(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(StringUtils.hasText(a.getMimeType()) ? MediaType.parseMediaType(a.getMimeType()) : MediaType.APPLICATION_OCTET_STREAM);
headers.set("Content-Disposition", "attachment; filename=\"" + a.getFilename() + "\"");
return new ResponseEntity<>(new InputStreamResource(is), headers, HttpStatus.OK);
}
@Autowired(required = false)
public void setService(AttachmentService service) {
this.service = service;
}
}
package com.jh.boot.web;
import com.jh.boot.jpa.AbstractIdEntity;
import com.jh.boot.web.list.Page;
import com.jh.boot.web.list.SortTrend;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Converter {
private Converter() {}
public static <S, D> D convert(S source, Function<S, D> mappingFunction) {
if (source == null || mappingFunction == null) {
return null;
}
return mappingFunction.apply(source);
}
public static <S, D> List<D> convertList(List<S> list, Function<S, D> mappingFunction) {
if (list == null) {
return null;
}
return list.stream().map(mappingFunction).collect(Collectors.toList());
}
public static <S, D> Page<D> convertPage(Page<S> page, Function<S, D> mappingFunction) {
if (page == null || mappingFunction == null) {
return null;
}
List<D> content = new ArrayList<>();
if (page.getContent() != null) {
for (S s : page.getContent()) {
content.add(mappingFunction.apply(s));
}
}
return new Page<D>(page.getPage(), page.getPagesCount(), page.getTotalElements(), content, page.getSortBy(), SortTrend.parse(page.getTrend()));
}
/**
* Helper function to safely get id
*
* @param entity
* @return
*/
public static Long getId(AbstractIdEntity entity) {
return entity == null ? null : entity.getId();
}
}
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