use of com.enonic.xp.attachment.Attachment in project xp by enonic.
the class AttachmentHandlerWorker method execute.
@Override
public PortalResponse execute() throws Exception {
final Content content = getContent(this.id);
final Attachment attachment = resolveAttachment(content, this.name);
final BinaryReference binaryReference = attachment.getBinaryReference();
final ByteSource binary = resolveBinary(this.id, binaryReference);
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 MediaType contentType = MediaType.parse(attachment.getMimeType());
final PortalResponse.Builder portalResponse = PortalResponse.create().contentType(contentType).body(binary);
if (this.download) {
portalResponse.header("Content-Disposition", contentDispositionAttachment(attachment.getName()));
}
if (this.name.endsWith(".svgz")) {
portalResponse.header("Content-Encoding", "gzip");
}
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(this.id, binaryReference))) {
portalResponse.header(HttpHeaders.CACHE_CONTROL, cacheControlHeaderConfig);
}
}
new RangeRequestHelper().handleRangeRequest(request, portalResponse, binary, contentType);
return portalResponse.build();
}
Aggregations