Search in sources :

Example 1 with ImageData

use of alien4cloud.images.ImageData in project alien4cloud by alien4cloud.

the class ArchiveImageLoaderTest method checkImages.

private void checkImages(Map<String, ? extends AbstractInheritableToscaType> elements) {
    boolean elementHasTags = false;
    String currentUUID = null;
    ImageData image = null;
    Tag iconTag = new Tag("icon", "");
    for (Map.Entry<String, ? extends AbstractInheritableToscaType> toscaInheritableElement : elements.entrySet()) {
        elementHasTags = toscaInheritableElement.getValue().getTags() != null;
        if (elementHasTags) {
            int indexOfIcon = toscaInheritableElement.getValue().getTags().indexOf(iconTag);
            if (indexOfIcon >= 0) {
                currentUUID = toscaInheritableElement.getValue().getTags().get(indexOfIcon).getValue();
                image = imageGenericIdDAO.findById(ImageData.class, currentUUID);
                // get registered images in ES by the UUID
                assertEquals(currentUUID, image.getId());
            }
        }
    }
}
Also used : ImageData(alien4cloud.images.ImageData) Tag(alien4cloud.model.common.Tag) Map(java.util.Map)

Example 2 with ImageData

use of alien4cloud.images.ImageData 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 ImageData

use of alien4cloud.images.ImageData in project alien4cloud by alien4cloud.

the class ImageServlet method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final String imageId = req.getParameter("id");
    final String quality = req.getParameter("quality");
    final ImageQuality imageQuality;
    if (quality == null) {
        imageQuality = ImageQuality.QUALITY_BEST;
    } else {
        imageQuality = ImageQuality.valueOf(quality);
    }
    final ImageData imageData = this.imageDAO.readImage(imageId, imageQuality);
    if (imageData != null) {
        // Set content type
        resp.setContentType(imageData.getMime());
        // Set content size
        resp.setContentLength(imageData.getData().length);
        // Open the file and output streams
        final OutputStream out = resp.getOutputStream();
        try {
            out.write(imageData.getData());
        } finally {
            out.close();
        }
    } else {
        resp.setStatus(HttpStatus.NOT_FOUND.value());
    }
}
Also used : ImageQuality(alien4cloud.utils.ImageQuality) ImageData(alien4cloud.images.ImageData) OutputStream(java.io.OutputStream)

Aggregations

ImageData (alien4cloud.images.ImageData)3 ImageUploadException (alien4cloud.images.exception.ImageUploadException)1 Tag (alien4cloud.model.common.Tag)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 ImageQuality (alien4cloud.utils.ImageQuality)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1