Search in sources :

Example 1 with ImageUploadException

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();
}
Also used : ImageUploadException(alien4cloud.images.exception.ImageUploadException) IOException(java.io.IOException) Application(alien4cloud.model.application.Application) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with ImageUploadException

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)));
    }
}
Also used : ParsingError(alien4cloud.tosca.parser.ParsingError) ImageUploadException(alien4cloud.images.exception.ImageUploadException) ImageData(alien4cloud.images.ImageData) IOException(java.io.IOException)

Example 3 with ImageUploadException

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);
    }
}
Also used : ImageUploadException(alien4cloud.images.exception.ImageUploadException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ImageUploadException (alien4cloud.images.exception.ImageUploadException)3 IOException (java.io.IOException)3 Audit (alien4cloud.audit.annotation.Audit)1 ImageData (alien4cloud.images.ImageData)1 Application (alien4cloud.model.application.Application)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 ApiOperation (io.swagger.annotations.ApiOperation)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1