Search in sources :

Example 1 with InvalidImageException

use of io.gravitee.rest.api.exception.InvalidImageException in project gravitee-management-rest-api by gravitee-io.

the class CategoryResource method updateCategory.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update the category", notes = "User must have the PORTAL_CATEGORY[UPDATE] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Category successfully updated", response = CategoryEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_CATEGORY, acls = RolePermissionAction.UPDATE) })
public Response updateCategory(@Valid @NotNull final UpdateCategoryEntity category) {
    try {
        ImageUtils.verify(category.getPicture());
        ImageUtils.verify(category.getBackground());
    } catch (InvalidImageException e) {
        throw new BadRequestException("Invalid image format");
    }
    CategoryEntity categoryEntity = categoryService.update(categoryId, category);
    setPictures(categoryEntity, false);
    return Response.ok(categoryEntity).build();
}
Also used : InvalidImageException(io.gravitee.rest.api.exception.InvalidImageException) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 2 with InvalidImageException

use of io.gravitee.rest.api.exception.InvalidImageException in project gravitee-management-rest-api by gravitee-io.

the class ImageUtils method rescale.

private static Image rescale(Image image, int width, int height) throws InvalidImageException {
    try {
        ImageInputStream imageInputStream = ImageIO.createImageInputStream(image.getData());
        Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
        while (imageReaders.hasNext()) {
            ImageReader reader = imageReaders.next();
            String discoveredType = reader.getFormatName();
            if (!ALLOWED_MIMETYPE.contains(discoveredType.toLowerCase())) {
                throw new InvalidImageException(discoveredType + " format is not supported");
            }
            reader.setInput(imageInputStream);
            reader.getNumImages(true);
            BufferedImage bufferedImage = reader.read(0);
            java.awt.Image scaledImage = bufferedImage.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
            BufferedImage bufferedScaledImage = new BufferedImage(width, height, bufferedImage.getType());
            Graphics2D g2 = bufferedScaledImage.createGraphics();
            g2.drawImage(scaledImage, 0, 0, null);
            g2.dispose();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(bufferedScaledImage, discoveredType, bos);
            return new Image(image.getType(), image.getMimeType(), bos.toByteArray());
        }
        throw new InvalidImageException("Image can not be rescaled");
    } catch (IOException ioe) {
        throw new InvalidImageException("Image can not be rescaled", ioe);
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) InvalidImageException(io.gravitee.rest.api.exception.InvalidImageException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) java.awt(java.awt)

Example 3 with InvalidImageException

use of io.gravitee.rest.api.exception.InvalidImageException in project gravitee-management-rest-api by gravitee-io.

the class ApiMediaResource method uploadApiMediaImage.

@POST
@ApiOperation(value = "Create a media for an API", notes = "User must have the API_DOCUMENTATION[CREATE] permission to use this service")
@ApiResponses({ @ApiResponse(code = 201, message = "Media successfully created", response = PageEntity.class), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.API_DOCUMENTATION, acls = RolePermissionAction.CREATE) })
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
public Response uploadApiMediaImage(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("file") final FormDataBodyPart body) throws IOException {
    final String mediaId;
    if (fileDetail.getSize() > this.mediaService.getMediaMaxSize()) {
        throw new UploadUnauthorized("Max size achieved " + fileDetail.getSize());
    } else {
        MediaEntity mediaEntity = new MediaEntity();
        mediaEntity.setSize(fileDetail.getSize());
        mediaEntity.setType(body.getMediaType().getType());
        mediaEntity.setSubType(body.getMediaType().getSubtype());
        mediaEntity.setData(IOUtils.toByteArray(uploadedInputStream));
        mediaEntity.setFileName(fileDetail.getFileName());
        try {
            ImageUtils.verify(body.getMediaType().getType(), body.getMediaType().getSubtype(), mediaEntity.getData());
        } catch (InvalidImageException e) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Invalid image format").build();
        }
        mediaId = mediaService.saveApiMedia(api, mediaEntity);
    }
    return Response.status(200).entity(mediaId).build();
}
Also used : UploadUnauthorized(io.gravitee.rest.api.service.exceptions.UploadUnauthorized) InvalidImageException(io.gravitee.rest.api.exception.InvalidImageException) MediaEntity(io.gravitee.rest.api.model.MediaEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 4 with InvalidImageException

use of io.gravitee.rest.api.exception.InvalidImageException in project gravitee-management-rest-api by gravitee-io.

the class PortalMediaResource method uploadPortalMedia.

@POST
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_DOCUMENTATION, acls = RolePermissionAction.CREATE) })
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/plain")
@ApiOperation(value = "Create a media for the portal", notes = "User must have the PORTAL_DOCUMENTATION[CREATE] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Media successfully created"), @ApiResponse(code = 500, message = "Internal server error") })
public Response uploadPortalMedia(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("file") final FormDataBodyPart body) throws IOException {
    String mediaId;
    if (fileDetail.getSize() > this.mediaService.getMediaMaxSize()) {
        throw new UploadUnauthorized("Max size achieved " + fileDetail.getSize());
    } else {
        MediaEntity mediaEntity = new MediaEntity();
        mediaEntity.setSize(fileDetail.getSize());
        mediaEntity.setType(body.getMediaType().getType());
        mediaEntity.setSubType(body.getMediaType().getSubtype());
        mediaEntity.setData(IOUtils.toByteArray(uploadedInputStream));
        mediaEntity.setFileName(fileDetail.getFileName());
        try {
            ImageUtils.verify(body.getMediaType().getType(), body.getMediaType().getSubtype(), mediaEntity.getData());
        } catch (InvalidImageException e) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Invalid image format").build();
        }
        mediaId = mediaService.savePortalMedia(mediaEntity);
    }
    return Response.status(200).entity(mediaId).build();
}
Also used : UploadUnauthorized(io.gravitee.rest.api.service.exceptions.UploadUnauthorized) InvalidImageException(io.gravitee.rest.api.exception.InvalidImageException) MediaEntity(io.gravitee.rest.api.model.MediaEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

InvalidImageException (io.gravitee.rest.api.exception.InvalidImageException)4 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)3 MediaEntity (io.gravitee.rest.api.model.MediaEntity)2 UploadUnauthorized (io.gravitee.rest.api.service.exceptions.UploadUnauthorized)2 CategoryEntity (io.gravitee.rest.api.model.CategoryEntity)1 UpdateCategoryEntity (io.gravitee.rest.api.model.UpdateCategoryEntity)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 java.awt (java.awt)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ImageReader (javax.imageio.ImageReader)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1