use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class ContentDataSerializer method extractAttachments.
private void extractAttachments(final PropertySet contentAsSet, final Content.Builder<?> builder) {
final Attachments attachments = dataToAttachments(contentAsSet.getSets(ATTACHMENT));
builder.attachments(attachments);
final Attachment thumbnailAttachment = attachments.byName(AttachmentNames.THUMBNAIL);
if (thumbnailAttachment != null) {
final BinaryReference thumbnailBinaryRef = thumbnailAttachment.getBinaryReference();
final Thumbnail thumbnail = Thumbnail.from(thumbnailBinaryRef, thumbnailAttachment.getMimeType(), thumbnailAttachment.getSize());
builder.thumbnail(thumbnail);
}
}
use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class ContentDataSerializer method mapValidationError.
private ValidationError mapValidationError(final PropertySet ve) {
final Object[] args = Optional.ofNullable(ve.getString("args")).map(argsJson -> {
try {
return OBJECT_MAPPER.readValue(argsJson, Object[].class);
} catch (JsonProcessingException e) {
throw new UncheckedIOException(e);
}
}).orElse(null);
final ValidationErrorCode errorCode = ValidationErrorCode.parse(ve.getString("errorCode"));
if (ve.hasProperty("propertyPath")) {
return ValidationError.dataError(errorCode, PropertyPath.from(ve.getString("propertyPath"))).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
} else if (ve.hasProperty("attachment")) {
return ValidationError.attachmentError(errorCode, BinaryReference.from(ve.getString("attachment"))).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
} else {
return ValidationError.generalError(errorCode).message(ve.getString("message"), true).i18n(ve.getString("i18n")).args(args).build();
}
}
use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class ContentDataSerializer method mergeExistingAndUpdatedAttachments.
private Attachments mergeExistingAndUpdatedAttachments(final Attachments existingAttachments, final UpdateContentTranslatorParams params) {
CreateAttachments createAttachments = params.getCreateAttachments();
BinaryReferences removeAttachments = params.getRemoveAttachments();
if (createAttachments == null && removeAttachments == null && !params.isClearAttachments()) {
return existingAttachments;
}
createAttachments = createAttachments == null ? CreateAttachments.empty() : createAttachments;
removeAttachments = removeAttachments == null ? BinaryReferences.empty() : removeAttachments;
final Map<BinaryReference, Attachment> attachments = new LinkedHashMap<>();
if (!params.isClearAttachments()) {
existingAttachments.stream().forEach((a) -> attachments.put(a.getBinaryReference(), a));
}
removeAttachments.stream().forEach(attachments::remove);
// added attachments with same BinaryReference will replace existing ones
for (final CreateAttachment createAttachment : createAttachments) {
final Attachment attachment = Attachment.create().name(createAttachment.getName()).label(createAttachment.getLabel()).mimeType(createAttachment.getMimeType()).size(attachmentSize(createAttachment)).textContent(createAttachment.getTextContent()).build();
attachments.put(attachment.getBinaryReference(), attachment);
}
return Attachments.from(attachments.values());
}
use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class AbstractAttachmentHandlerWorker method execute.
@Override
public PortalResponse execute() throws Exception {
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 T content = cast(getContent(this.id));
final Attachment attachment = resolveAttachment(content, this.name);
final BinaryReference binaryReference = attachment.getBinaryReference();
final ByteSource binary = getBinary(this.id, binaryReference);
final MediaType attachmentMimeType = MediaType.parse(attachment.getMimeType());
final boolean isSvgz = "svgz".equals(attachment.getExtension());
final boolean isGif = attachmentMimeType.is(MediaType.GIF);
final PortalResponse.Builder portalResponse = PortalResponse.create();
final MediaType contentType;
if (isSvgz) {
contentType = SVG_MEDIA_TYPE;
portalResponse.header("Content-Encoding", "gzip");
} else if (isGif) {
contentType = MediaType.GIF;
} else if (shouldConvert(content, this.name)) {
contentType = MediaTypes.instance().fromFile(this.name);
} else {
contentType = attachmentMimeType;
}
if (contentType.is(SVG_MEDIA_TYPE)) {
if (!nullToEmpty(contentSecurityPolicySvg).isBlank()) {
portalResponse.header("Content-Security-Policy", contentSecurityPolicySvg);
}
} else {
if (!nullToEmpty(contentSecurityPolicy).isBlank()) {
portalResponse.header("Content-Security-Policy", contentSecurityPolicy);
}
}
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, binaryReference))) {
portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
}
}
if (download) {
portalResponse.header("Content-Disposition", contentDispositionAttachment(attachment.getName()));
}
final ByteSource body;
if (isGif || isSvgz || attachmentMimeType.is(SVG_MEDIA_TYPE)) {
body = binary;
} else {
body = transform(content, binaryReference, binary, contentType);
}
addTrace(content);
writeResponseContent(portalResponse, contentType, body);
return portalResponse.build();
}
use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class AbstractAttachmentHandlerWorker method resolveAttachment.
protected Attachment resolveAttachment(final Content content, final String name) {
final Attachments attachments = content.getAttachments();
final Attachment attachment = attachments.byName(name);
if (attachment != null) {
return attachment;
}
throw WebException.notFound(String.format("Attachment [%s] not found for [%s]", name, content.getPath()));
}
Aggregations