Search in sources :

Example 1 with ReadImageParams

use of com.enonic.xp.image.ReadImageParams in project xp by enonic.

the class ImageHandlerWorker method execute.

@Override
public PortalResponse execute() throws Exception {
    final Media content = getImage(this.contentId);
    final String contentName = content.getName().toString();
    final boolean contentNameEquals = contentName.equals(this.name);
    if (!(contentNameEquals || contentName.equals(Files.getNameWithoutExtension(this.name)))) {
        throw WebException.notFound(String.format("Image [%s] not found for content [%s]", this.name, this.contentId));
    }
    final Attachment attachment = content.getMediaAttachment();
    if (attachment == null) {
        throw WebException.notFound(String.format("Attachment [%s] not found", content.getName()));
    }
    final BinaryReference binaryReference = attachment.getBinaryReference();
    final ByteSource binary = this.contentService.getBinary(this.contentId, binaryReference);
    if (binary == null) {
        throw WebException.notFound(String.format("Binary [%s] not found for content [%s]", binaryReference, this.contentId));
    }
    if (request.getMethod() == HttpMethod.OPTIONS) {
        // it will be handled by default OPTIONS handler in BaseWebHandler
        return PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
    }
    final String attachmentMimeType = attachment.getMimeType();
    final PortalResponse.Builder portalResponse = PortalResponse.create();
    if ("svgz".equals(attachment.getExtension())) {
        portalResponse.contentType(MediaType.SVG_UTF_8.withoutParameters());
        portalResponse.header("Content-Encoding", "gzip");
        portalResponse.body(binary);
    } else if (attachmentMimeType.equals("image/svg+xml") || attachmentMimeType.equals("image/gif")) {
        portalResponse.contentType(MediaType.parse(attachmentMimeType));
        portalResponse.body(binary);
    } else {
        final ImageOrientation imageOrientation = Objects.requireNonNullElseGet(content.getOrientation(), () -> Objects.requireNonNullElse(mediaInfoService.getImageOrientation(binary), ImageOrientation.TopLeft));
        final MediaType mimeType = contentNameEquals ? MediaType.parse(attachmentMimeType) : MediaTypes.instance().fromFile(this.name);
        portalResponse.contentType(mimeType);
        try {
            final ReadImageParams readImageParams = ReadImageParams.newImageParams().contentId(this.contentId).binaryReference(binaryReference).cropping(content.getCropping()).scaleParams(this.scaleParams).focalPoint(content.getFocalPoint()).filterParam(this.filterParam).backgroundColor(getBackgroundColor()).mimeType(mimeType.toString()).quality(getImageQuality()).orientation(imageOrientation).build();
            portalResponse.body(this.imageService.readImage(readImageParams));
        } catch (IllegalArgumentException e) {
            throw new WebException(HttpStatus.BAD_REQUEST, "Invalid parameters", e);
        } catch (ThrottlingException e) {
            throw new WebException(HttpStatus.TOO_MANY_REQUESTS, "Try again later", e);
        }
    }
    if (!nullToEmpty(this.fingerprint).isBlank()) {
        final boolean isPublic = content.getPermissions().isAllowedFor(RoleKeys.EVERYONE, Permission.READ) && ContentConstants.BRANCH_MASTER.equals(request.getBranch());
        final String cacheControlHeaderConfig = isPublic ? publicCacheControlHeaderConfig : privateCacheControlHeaderConfig;
        if (!nullToEmpty(cacheControlHeaderConfig).isBlank() && this.fingerprint.equals(resolveHash(content))) {
            portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
        }
    }
    final Trace trace = Tracer.current();
    if (trace != null) {
        trace.put("contentPath", content.getPath());
        trace.put("type", "image");
    }
    return portalResponse.build();
}
Also used : WebException(com.enonic.xp.web.WebException) Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) BinaryReference(com.enonic.xp.util.BinaryReference) ImageOrientation(com.enonic.xp.media.ImageOrientation) ReadImageParams(com.enonic.xp.image.ReadImageParams) Trace(com.enonic.xp.trace.Trace) PortalResponse(com.enonic.xp.portal.PortalResponse) ByteSource(com.google.common.io.ByteSource) MediaType(com.google.common.net.MediaType) ThrottlingException(com.enonic.xp.exception.ThrottlingException)

Example 2 with ReadImageParams

use of com.enonic.xp.image.ReadImageParams in project xp by enonic.

the class ImageServiceImplTest method testReadImageWithCache.

@Test
public void testReadImageWithCache() throws IOException {
    Cropping cropping = Cropping.create().top(0.25).bottom(0.75).left(0.25).right(0.75).zoom(2).build();
    final ReadImageParams readImageParams = ReadImageParams.newImageParams().contentId(contentId).binaryReference(binaryReference).cropping(cropping).scaleSize(128).filterParam("blur(10)").mimeType("image/jpeg").backgroundColor(0xFF0000).quality(5).orientation(ImageOrientation.BottomLeft).build();
    ByteSource imageData = imageService.readImage(readImageParams);
    assertArrayEquals(ByteStreams.toByteArray(getClass().getResourceAsStream("processed.jpg")), imageData.read());
    Mockito.verify(imageFilter).apply(any());
    imageData = imageService.readImage(readImageParams);
    assertArrayEquals(ByteStreams.toByteArray(getClass().getResourceAsStream("processed.jpg")), imageData.read());
    Mockito.verify(imageFilter).apply(any());
}
Also used : ReadImageParams(com.enonic.xp.image.ReadImageParams) ByteSource(com.google.common.io.ByteSource) Cropping(com.enonic.xp.image.Cropping) Test(org.junit.jupiter.api.Test)

Example 3 with ReadImageParams

use of com.enonic.xp.image.ReadImageParams in project xp by enonic.

the class ImageServiceImplTest method testReadImageWithFormat.

@Test
@Deprecated
public void testReadImageWithFormat() throws IOException {
    final ReadImageParams readImageParams = ReadImageParams.newImageParams().contentId(contentId).binaryReference(binaryReference).format("png").build();
    final ByteSource imageData = imageService.readImage(readImageParams);
    assertArrayEquals(imageDataOriginal, imageData.read());
}
Also used : ReadImageParams(com.enonic.xp.image.ReadImageParams) ByteSource(com.google.common.io.ByteSource) Test(org.junit.jupiter.api.Test)

Example 4 with ReadImageParams

use of com.enonic.xp.image.ReadImageParams in project xp by enonic.

the class ImageServiceImplTest method testReadImageMinimal.

@Test
public void testReadImageMinimal() throws IOException {
    final ReadImageParams readImageParams = ReadImageParams.newImageParams().contentId(contentId).binaryReference(binaryReference).mimeType("image/png").build();
    final ByteSource imageData = imageService.readImage(readImageParams);
    assertArrayEquals(imageDataOriginal, imageData.read());
}
Also used : ReadImageParams(com.enonic.xp.image.ReadImageParams) ByteSource(com.google.common.io.ByteSource) Test(org.junit.jupiter.api.Test)

Aggregations

ReadImageParams (com.enonic.xp.image.ReadImageParams)4 ByteSource (com.google.common.io.ByteSource)4 Test (org.junit.jupiter.api.Test)3 Attachment (com.enonic.xp.attachment.Attachment)1 Media (com.enonic.xp.content.Media)1 ThrottlingException (com.enonic.xp.exception.ThrottlingException)1 Cropping (com.enonic.xp.image.Cropping)1 ImageOrientation (com.enonic.xp.media.ImageOrientation)1 PortalResponse (com.enonic.xp.portal.PortalResponse)1 Trace (com.enonic.xp.trace.Trace)1 BinaryReference (com.enonic.xp.util.BinaryReference)1 WebException (com.enonic.xp.web.WebException)1 MediaType (com.google.common.net.MediaType)1