use of org.hisp.dhis.fileresource.ImageFileDimension in project dhis2-core by dhis2.
the class TrackedEntityInstanceController method getAttributeImage.
@GetMapping("/{teiId}/{attributeId}/image")
public void getAttributeImage(@PathVariable("teiId") String teiId, @PathVariable("attributeId") String attributeId, @RequestParam(required = false) Integer width, @RequestParam(required = false) Integer height, @RequestParam(required = false) ImageFileDimension dimension, HttpServletResponse response) throws WebMessageException {
User user = currentUserService.getCurrentUser();
org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = instanceService.getTrackedEntityInstance(teiId);
List<String> trackerAccessErrors = trackerAccessManager.canRead(user, trackedEntityInstance);
List<TrackedEntityAttributeValue> attribute = trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(val -> val.getAttribute().getUid().equals(attributeId)).collect(Collectors.toList());
if (!trackerAccessErrors.isEmpty()) {
throw new WebMessageException(unauthorized("You're not authorized to access the TrackedEntityInstance with id: " + teiId));
}
if (attribute.size() == 0) {
throw new WebMessageException(notFound("Attribute not found for ID " + attributeId));
}
TrackedEntityAttributeValue value = attribute.get(0);
if (value == null) {
throw new WebMessageException(notFound("Value not found for ID " + attributeId));
}
if (value.getAttribute().getValueType() != ValueType.IMAGE) {
throw new WebMessageException(conflict("Attribute must be of type image"));
}
// ---------------------------------------------------------------------
// Get file resource
// ---------------------------------------------------------------------
FileResource fileResource = fileResourceService.getFileResource(value.getValue());
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(notFound("A data value file resource with id " + value.getValue() + " does not exist."));
}
if (fileResource.getStorageStatus() != FileResourceStorageStatus.STORED) {
throw new WebMessageException(conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time."));
}
// ---------------------------------------------------------------------
// Build response and return
// ---------------------------------------------------------------------
FileResourceUtils.setImageFileDimensions(fileResource, MoreObjects.firstNonNull(dimension, ImageFileDimension.ORIGINAL));
response.setContentType(fileResource.getContentType());
response.setContentLength((int) fileResource.getContentLength());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
try (InputStream inputStream = fileResourceService.getFileResourceContent(fileResource)) {
BufferedImage img = ImageIO.read(inputStream);
height = height == null ? img.getHeight() : height;
width = width == null ? img.getWidth() : width;
BufferedImage resizedImg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D canvas = resizedImg.createGraphics();
canvas.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
canvas.drawImage(img, 0, 0, width, height, null);
canvas.dispose();
ImageIO.write(resizedImg, fileResource.getFormat(), response.getOutputStream());
} catch (IOException ex) {
throw new WebMessageException(error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend."));
}
}
Aggregations