Search in sources :

Example 1 with BlobManager

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();
}
Also used : IOUtils(org.apache.commons.io.IOUtils) BlobManager(com.manydesigns.elements.blobs.BlobManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) Logger(org.slf4j.Logger) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Date(java.util.Date) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) Blob(com.manydesigns.elements.blobs.Blob) InputStream(java.io.InputStream) Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) Date(java.util.Date)

Example 2 with BlobManager

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();
}
Also used : Blob(com.manydesigns.elements.blobs.Blob) FileBlob(com.manydesigns.elements.annotations.FileBlob) BlobManager(com.manydesigns.elements.blobs.BlobManager) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) Guard(com.manydesigns.portofino.operations.annotations.Guard) Operation(io.swagger.v3.oas.annotations.Operation)

Example 3 with BlobManager

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);
}
Also used : BlobManager(com.manydesigns.elements.blobs.BlobManager) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with BlobManager

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();
}
Also used : Blob(com.manydesigns.elements.blobs.Blob) FileBlob(com.manydesigns.elements.annotations.FileBlob) BlobManager(com.manydesigns.elements.blobs.BlobManager) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

BlobManager (com.manydesigns.elements.blobs.BlobManager)4 Blob (com.manydesigns.elements.blobs.Blob)3 Operation (io.swagger.v3.oas.annotations.Operation)3 FileBlob (com.manydesigns.elements.annotations.FileBlob)2 IOException (java.io.IOException)2 Guard (com.manydesigns.portofino.operations.annotations.Guard)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 Response (javax.ws.rs.core.Response)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 IOUtils (org.apache.commons.io.IOUtils)1 DateTime (org.joda.time.DateTime)1 Logger (org.slf4j.Logger)1