use of com.manydesigns.elements.blobs.BlobManager in project Portofino by ManyDesigns.
the class Utilities method downloadBlob.
public static Response downloadBlob(Blob blob, BlobManager blobManager, HttpServletRequest request, Logger logger) {
if (blob == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
if (blob.getInputStream() == null) {
try {
blobManager.loadMetadata(blob);
} catch (IOException e) {
logger.error("Could not load blob", e);
return Response.status(Response.Status.NOT_FOUND).build();
}
}
long contentLength = blob.getSize();
String contentType = blob.getContentType();
String fileName = blob.getFilename();
long lastModified = blob.getCreateTimestamp().getMillis();
if (request.getHeader("If-Modified-Since") != null) {
long ifModifiedSince = request.getDateHeader("If-Modified-Since");
if (ifModifiedSince >= lastModified) {
return Response.status(Response.Status.NOT_MODIFIED).build();
}
}
final InputStream inputStream;
if (blob.getInputStream() == null) {
try {
inputStream = blobManager.openStream(blob);
} catch (IOException e) {
logger.error("Could not load blob", e);
return Response.status(Response.Status.NOT_FOUND).build();
}
} else {
inputStream = blob.getInputStream();
}
StreamingOutput streamingOutput = output -> {
try (InputStream i = inputStream) {
IOUtils.copyLarge(i, output);
}
};
Response.ResponseBuilder responseBuilder = Response.ok(streamingOutput).type(contentType).lastModified(new Date(lastModified)).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName);
if (contentLength > 0) {
responseBuilder.header(HttpHeaders.CONTENT_LENGTH, contentLength);
}
return responseBuilder.build();
}
use of com.manydesigns.elements.blobs.BlobManager in project Portofino by ManyDesigns.
the class AbstractCrudAction method uploadBlob.
@PUT
@Path(":blob/{propertyName}")
@RequiresPermissions(permissions = PERMISSION_EDIT)
@Guard(test = "isEditEnabled()", type = GuardType.VISIBLE)
@Operation(summary = "Upload a blob property")
public Response uploadBlob(@Parameter(description = "The name of the property", required = true) @PathParam("propertyName") String propertyName, @Parameter(description = "The name of uploaded file") @QueryParam("filename") String filename, InputStream inputStream) throws IOException {
if (object == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Object can not be null (this method can only be called with /objectKey)").build();
}
checkAccessorPermissions(new String[] { PERMISSION_EDIT });
setupForm(Mode.EDIT);
form.readFromObject(object);
AbstractBlobField field = (AbstractBlobField) form.findFieldByPropertyName(propertyName);
if (field == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
if (!field.isUpdatable()) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Property not writable").build();
}
Blob blob = new Blob(field.generateNewCode());
blob.setFilename(filename);
blob.setSize(context.getRequest().getContentLength());
blob.setContentType(context.getRequest().getContentType());
blob.setCharacterEncoding(context.getRequest().getCharacterEncoding());
blob.setCreateTimestamp(new DateTime());
blob.setInputStream(inputStream);
Blob oldBlob = field.getValue();
field.setValue(blob);
field.writeToObject(object);
if (!field.isSaveBlobOnObject()) {
BlobManager blobManager = getBlobManager();
blobManager.save(blob);
if (oldBlob != null) {
try {
blobManager.delete(oldBlob);
} catch (IOException e) {
logger.warn("Could not delete old blob (code: " + oldBlob.getCode() + ")", e);
}
}
}
commitTransaction();
return Response.ok().build();
}
use of com.manydesigns.elements.blobs.BlobManager in project Portofino by ManyDesigns.
the class AbstractCrudAction method downloadBlob.
@GET
@Path(":blob/{propertyName}")
@Operation(summary = "Downloads the contents of a blob property")
public Response downloadBlob(@Parameter(description = "The name of the property", required = true) @PathParam("propertyName") String propertyName) {
if (object == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Object can not be null (this method can only be called with /objectKey)").build();
}
checkAccessorPermissions(new String[] { PERMISSION_READ });
setupForm(Mode.VIEW);
form.readFromObject(object);
BlobManager blobManager = getBlobManager();
AbstractBlobField field = (AbstractBlobField) form.findFieldByPropertyName(propertyName);
if (field == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Utilities.downloadBlob(field.getValue(), blobManager, context.getRequest(), logger);
}
use of com.manydesigns.elements.blobs.BlobManager in project Portofino by ManyDesigns.
the class AbstractCrudAction method deleteBlob.
@DELETE
@Path(":blob/{propertyName}")
@RequiresPermissions(permissions = PERMISSION_EDIT)
@Operation(summary = "Delete the contents of a blob property")
public Response deleteBlob(@Parameter(description = "The name of the property", required = true) @PathParam("propertyName") String propertyName) throws IOException {
if (object == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Object can not be null (this method can only be called with /objectKey)").build();
}
checkAccessorPermissions(new String[] { PERMISSION_EDIT });
setupForm(Mode.EDIT);
form.readFromObject(object);
AbstractBlobField field = (AbstractBlobField) form.findFieldByPropertyName(propertyName);
if (!field.isUpdatable() || field.isRequired()) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Property not writable").build();
}
Blob blob = field.getValue();
field.setValue(null);
field.writeToObject(object);
if (!field.isSaveBlobOnObject() && blob != null) {
BlobManager blobManager = getBlobManager();
blobManager.delete(blob);
}
commitTransaction();
return Response.ok().build();
}
Aggregations