Search in sources :

Example 1 with ImageFileDimension

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."));
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PathVariable(org.springframework.web.bind.annotation.PathVariable) APPLICATION_XML_VALUE(org.springframework.http.MediaType.APPLICATION_XML_VALUE) RequestParam(org.springframework.web.bind.annotation.RequestParam) ValueType(org.hisp.dhis.common.ValueType) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequiredArgsConstructor(lombok.RequiredArgsConstructor) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) FileResourceStorageStatus(org.hisp.dhis.fileresource.FileResourceStorageStatus) TEI_IMPORT(org.hisp.dhis.scheduling.JobType.TEI_IMPORT) NodeUtils(org.hisp.dhis.node.NodeUtils) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) PutMapping(org.springframework.web.bind.annotation.PutMapping) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) ImageIO(javax.imageio.ImageIO) GridUtils(org.hisp.dhis.system.grid.GridUtils) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PostMapping(org.springframework.web.bind.annotation.PostMapping) ContextService(org.hisp.dhis.webapi.service.ContextService) DxfNamespaces(org.hisp.dhis.common.DxfNamespaces) BufferedImage(java.awt.image.BufferedImage) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) HttpHeaders(org.springframework.http.HttpHeaders) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) CacheStrategy(org.hisp.dhis.common.cache.CacheStrategy) TrackedEntityInstanceCriteria(org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria) Collectors(java.util.stream.Collectors) TrackedEntityInstanceSchemaDescriptor(org.hisp.dhis.schema.descriptors.TrackedEntityInstanceSchemaDescriptor) ImageFileDimension(org.hisp.dhis.fileresource.ImageFileDimension) List(java.util.List) FileResourceUtils(org.hisp.dhis.webapi.utils.FileResourceUtils) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) WebMessageUtils.conflict(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.conflict) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) RootNode(org.hisp.dhis.node.types.RootNode) Joiner(com.google.common.base.Joiner) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) WebMessageUtils.notFound(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.notFound) TrackerEntityInstanceRequest(org.hisp.dhis.webapi.strategy.old.tracker.imports.request.TrackerEntityInstanceRequest) TrackedEntityInstanceStrategyHandler(org.hisp.dhis.webapi.strategy.old.tracker.imports.TrackedEntityInstanceStrategyHandler) CollectionNode(org.hisp.dhis.node.types.CollectionNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) StreamUtils(org.hisp.dhis.commons.util.StreamUtils) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) WebMessageUtils.importSummaries(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.importSummaries) WebMessageUtils.importSummary(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.importSummary) WebMessageUtils.error(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.error) User(org.hisp.dhis.user.User) GetMapping(org.springframework.web.bind.annotation.GetMapping) WebMessageUtils.jobConfigurationReport(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.jobConfigurationReport) BadRequestException(org.hisp.dhis.webapi.controller.exception.BadRequestException) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) WebMessageUtils.unauthorized(org.hisp.dhis.dxf2.webmessage.WebMessageUtils.unauthorized) TrackedEntityCriteriaMapper(org.hisp.dhis.webapi.controller.event.mapper.TrackedEntityCriteriaMapper) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceSupportService(org.hisp.dhis.webapi.service.TrackedEntityInstanceSupportService) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) Pager(org.hisp.dhis.common.Pager) FileResource(org.hisp.dhis.fileresource.FileResource) HttpServletResponse(javax.servlet.http.HttpServletResponse) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) MoreObjects(com.google.common.base.MoreObjects) IOException(java.io.IOException) APPLICATION_JSON_VALUE(org.springframework.http.MediaType.APPLICATION_JSON_VALUE) Grid(org.hisp.dhis.common.Grid) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) java.awt(java.awt) CurrentUserService(org.hisp.dhis.user.CurrentUserService) FileResourceDomain(org.hisp.dhis.fileresource.FileResourceDomain) InputStream(java.io.InputStream) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InputStream(java.io.InputStream) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) FileResource(org.hisp.dhis.fileresource.FileResource) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Joiner (com.google.common.base.Joiner)1 MoreObjects (com.google.common.base.MoreObjects)1 Lists (com.google.common.collect.Lists)1 java.awt (java.awt)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ImageIO (javax.imageio.ImageIO)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 StringUtils (org.apache.commons.lang3.StringUtils)1 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)1 DxfNamespaces (org.hisp.dhis.common.DxfNamespaces)1 Grid (org.hisp.dhis.common.Grid)1 Pager (org.hisp.dhis.common.Pager)1 ValueType (org.hisp.dhis.common.ValueType)1 CacheStrategy (org.hisp.dhis.common.cache.CacheStrategy)1