use of alien4cloud.images.exception.ImageUploadException in project alien4cloud by alien4cloud.
the class ApplicationController method updateImage.
/**
* Update application's image.
*
* @param applicationId The application id.
* @param image new image of the application
* @return nothing if success, error will be handled in global exception strategy
*/
@ApiOperation(value = "Updates the image for the application.", notes = "The logged-in user must have the application manager role for this application. Application role required [ APPLICATION_MANAGER ]")
@RequestMapping(value = "/{applicationId:.+}/image", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
@Audit
public RestResponse<String> updateImage(@PathVariable String applicationId, @RequestParam("file") MultipartFile image) {
Application application = applicationService.checkAndGetApplication(applicationId, ApplicationRole.APPLICATION_MANAGER);
String imageId;
try {
imageId = imageDAO.writeImage(image.getBytes());
} catch (IOException e) {
throw new ImageUploadException("Unable to read image from file upload [" + image.getOriginalFilename() + "] to update application [" + applicationId + "]", e);
}
application.setImageId(imageId);
alienDAO.save(application);
return RestResponseBuilder.<String>builder().data(imageId).build();
}
use of alien4cloud.images.exception.ImageUploadException in project alien4cloud by alien4cloud.
the class ArchiveImageLoader method importImage.
private void importImage(Path archiveFile, List<ParsingError> parsingErrors, Tag iconTag) {
FileSystem csarFS = null;
Path iconPath = null;
try {
csarFS = FileSystems.newFileSystem(archiveFile, null);
iconPath = csarFS.getPath(iconTag.getValue());
if (!Files.isDirectory(iconPath)) {
String iconId = UUID.randomUUID().toString();
// Saving the image
ImageData imageData = new ImageData();
imageData.setData(Files.readAllBytes(iconPath));
imageData.setId(iconId);
imageDAO.writeImage(imageData);
// Replace the image uri by the indexed image ID
iconTag.setValue(iconId);
} else {
parsingErrors.add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.INVALID_ICON_FORMAT, "Icon loading", null, "Invalid icon format at path <" + iconPath + ">", null, safeToString(iconPath)));
}
} catch (NoSuchFileException | InvalidPathException e) {
parsingErrors.add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.MISSING_FILE, "Icon loading", null, "No icon file found at path <" + iconPath + ">", null, safeToString(iconPath)));
} catch (ImageUploadException e) {
parsingErrors.add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.INVALID_ICON_FORMAT, "Icon loading", null, "Invalid icon format at path <" + iconPath + ">", null, safeToString(iconPath)));
} catch (IOException e) {
parsingErrors.add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.FAILED_TO_READ_FILE, "Icon loading", null, "IO error while loading icon at path <" + iconPath + ">", null, safeToString(iconPath)));
}
}
use of alien4cloud.images.exception.ImageUploadException in project alien4cloud by alien4cloud.
the class ImageDAO method writeImage.
@Override
public void writeImage(final ImageData imageData) {
// resize the image to store the different available qualities.
InputStream is = new ByteArrayInputStream(imageData.getData());
try {
BufferedImage original = ImageIO.read(is);
if (original == null) {
throw new ImageUploadException("The image is not valid and cannot be read");
}
resizeAndWrite(getImageId(ImageQuality.QUALITY_16, imageData.getId()), original, ImageQuality.QUALITY_16.getSize());
resizeAndWrite(getImageId(ImageQuality.QUALITY_32, imageData.getId()), original, ImageQuality.QUALITY_32.getSize());
resizeAndWrite(getImageId(ImageQuality.QUALITY_64, imageData.getId()), original, ImageQuality.QUALITY_64.getSize());
resizeAndWrite(getImageId(ImageQuality.QUALITY_128, imageData.getId()), original, ImageQuality.QUALITY_128.getSize());
// resizeAndWrite(getImageId(ImageQuality.QUALITY_512, imageData.getId()), original, ImageQuality.QUALITY_512.getSize());
saveAsPng(imageData.getId(), original);
} catch (IOException e) {
throw new ImageUploadException("Unable to write uploaded image to data source", e);
}
}
Aggregations