Search in sources :

Example 6 with Media

use of com.enonic.xp.content.Media 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 7 with Media

use of com.enonic.xp.content.Media in project xp by enonic.

the class AttachmentHandlerTest method setupMedia.

private void setupMedia() throws Exception {
    final Attachment attachment = Attachment.create().name("logo.png").mimeType("image/png").label("small").build();
    final Media content = createMedia("123456", "path/to/content", attachment);
    when(this.contentService.getById(eq(content.getId()))).thenReturn(content);
    when(this.contentService.getByPath(eq(content.getPath()))).thenReturn(content);
    when(this.contentService.getBinaryKey(eq(content.getId()), eq(content.getMediaAttachment().getBinaryReference()))).thenReturn("98765");
    this.mediaBytes = ByteSource.wrap(new byte[] { '0', '1', '2', '3', '4', '5', '6' });
    when(this.contentService.getBinary(isA(ContentId.class), isA(BinaryReference.class))).thenReturn(this.mediaBytes);
}
Also used : Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) ContentId(com.enonic.xp.content.ContentId) BinaryReference(com.enonic.xp.util.BinaryReference)

Example 8 with Media

use of com.enonic.xp.content.Media in project xp by enonic.

the class ImageUrlBuilderTest method init.

@BeforeEach
public void init() {
    final PortalRequest portalRequest = new PortalRequest();
    portalRequest.setBranch(Branch.from("draft"));
    portalRequest.setApplicationKey(ApplicationKey.from("myapplication"));
    portalRequest.setBaseUri("/site");
    portalRequest.setContentPath(ContentPath.from("context/path"));
    this.imageUrlParams = new ImageUrlParams().portalRequest(portalRequest).scale("testScale");
    urlBuilder = new ImageUrlBuilder();
    urlBuilder.setParams(imageUrlParams);
    final Attachment attachment = Attachment.create().name("attachmentName").mimeType("attachmentMimeType").size(1).build();
    media = Mockito.mock(Media.class);
    final ContentId contentId = ContentId.from("testID");
    Mockito.when(media.getId()).thenReturn(contentId);
    Mockito.when(media.getName()).thenReturn(ContentName.from("testName"));
    Mockito.when(media.getType()).thenReturn(ContentTypeName.imageMedia());
    Mockito.when(media.getMediaAttachment()).thenReturn(attachment);
    final ContentService contentService = Mockito.mock(ContentService.class);
    Mockito.when(contentService.getByPath(Mockito.any())).thenReturn(media);
    Mockito.when(contentService.getById(Mockito.any())).thenReturn(media);
    Mockito.when(contentService.getBinaryKey(contentId, attachment.getBinaryReference())).thenReturn("binaryHash");
    urlBuilder.contentService = contentService;
}
Also used : Media(com.enonic.xp.content.Media) Attachment(com.enonic.xp.attachment.Attachment) ContentId(com.enonic.xp.content.ContentId) ContentService(com.enonic.xp.content.ContentService) PortalRequest(com.enonic.xp.portal.PortalRequest) ImageUrlParams(com.enonic.xp.portal.url.ImageUrlParams) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with Media

use of com.enonic.xp.content.Media in project xp by enonic.

the class UpdatedEventSyncCommand method doSyncMedia.

private void doSyncMedia(final ContentToSync content, final UpdateContentParams updateParams) {
    if (content.getSourceContent() instanceof Media) {
        final Media sourceMedia = (Media) content.getSourceContent();
        final Attachment mediaAttachment = sourceMedia.getMediaAttachment();
        final ByteSource sourceBinary = content.getSourceContext().callWith(() -> contentService.getBinary(sourceMedia.getId(), mediaAttachment.getBinaryReference()));
        final MediaInfo mediaInfo = content.getSourceContext().callWith(() -> mediaInfoService.parseMediaInfo(sourceBinary));
        final ContentTypeName type = ContentTypeFromMimeTypeResolver.resolve(mediaAttachment.getMimeType());
        final CreateAttachment createAttachment = CreateAttachment.create().name(mediaAttachment.getName()).mimeType(mediaAttachment.getMimeType()).label("source").byteSource(sourceBinary).text(type != null && type.isTextualMedia() ? mediaInfo.getTextContent() : null).build();
        updateParams.clearAttachments(true).createAttachments(CreateAttachments.from(createAttachment));
    }
}
Also used : MediaInfo(com.enonic.xp.media.MediaInfo) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Media(com.enonic.xp.content.Media) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ByteSource(com.google.common.io.ByteSource) Attachment(com.enonic.xp.attachment.Attachment) CreateAttachment(com.enonic.xp.attachment.CreateAttachment)

Example 10 with Media

use of com.enonic.xp.content.Media in project xp by enonic.

the class ImageContentProcessor method updateImageMetadata.

private ExtraDatas updateImageMetadata(final EditableContent editable) {
    final Media media = (Media) editable.source;
    final Attachment mediaAttachment = media.getMediaAttachment();
    if (mediaAttachment == null) {
        return editable.extraDatas;
    }
    final ByteSource binary = contentService.getBinary(editable.source.getId(), mediaAttachment.getBinaryReference());
    if (binary == null) {
        return editable.extraDatas;
    }
    final BufferedImage image = toBufferedImage(binary);
    final Cropping cropping = media.getCropping();
    final long byteSize = mediaAttachment.getSize();
    final long imageWidth;
    final long imageHeight;
    final long imageSize;
    if (cropping == null || cropping.isUnmodified()) {
        imageWidth = image.getWidth();
        imageHeight = image.getHeight();
        imageSize = imageWidth * imageHeight;
    } else {
        final BufferedImage croppedImage = cropImage(image, cropping);
        imageWidth = croppedImage.getWidth();
        imageHeight = croppedImage.getHeight();
        imageSize = imageWidth * imageHeight;
    }
    ExtraData extraData = editable.extraDatas.getMetadata(MediaInfo.IMAGE_INFO_METADATA_NAME);
    if (extraData != null) {
        final PropertyTree xData = extraData.getData();
        setLongProperty(xData, IMAGE_INFO_PIXEL_SIZE, imageSize);
        setLongProperty(xData, IMAGE_INFO_IMAGE_HEIGHT, imageHeight);
        setLongProperty(xData, IMAGE_INFO_IMAGE_WIDTH, imageWidth);
        setLongProperty(xData, MEDIA_INFO_BYTE_SIZE, byteSize);
    }
    return editable.extraDatas;
}
Also used : PropertyTree(com.enonic.xp.data.PropertyTree) Media(com.enonic.xp.content.Media) ByteSource(com.google.common.io.ByteSource) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) Attachment(com.enonic.xp.attachment.Attachment) ExtraData(com.enonic.xp.content.ExtraData) BufferedImage(java.awt.image.BufferedImage) Cropping(com.enonic.xp.image.Cropping)

Aggregations

Media (com.enonic.xp.content.Media)19 Test (org.junit.jupiter.api.Test)7 Attachment (com.enonic.xp.attachment.Attachment)6 ProcessHtmlParams (com.enonic.xp.portal.url.ProcessHtmlParams)5 ByteSource (com.google.common.io.ByteSource)5 ContentId (com.enonic.xp.content.ContentId)4 Content (com.enonic.xp.content.Content)3 ExtraData (com.enonic.xp.content.ExtraData)3 PropertyTree (com.enonic.xp.data.PropertyTree)3 ImageOrientation (com.enonic.xp.media.ImageOrientation)3 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)2 BinaryReference (com.enonic.xp.util.BinaryReference)2 ContentService (com.enonic.xp.content.ContentService)1 CreateMediaParams (com.enonic.xp.content.CreateMediaParams)1 ThrottlingException (com.enonic.xp.exception.ThrottlingException)1 Cropping (com.enonic.xp.image.Cropping)1 ReadImageParams (com.enonic.xp.image.ReadImageParams)1 MediaInfo (com.enonic.xp.media.MediaInfo)1 PortalRequest (com.enonic.xp.portal.PortalRequest)1 PortalResponse (com.enonic.xp.portal.PortalResponse)1