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();
}
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);
}
}
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();
}
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();
}
Aggregations