Search in sources :

Example 16 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class UiTest method test01AutoRenameThumbnail.

@Test
public void test01AutoRenameThumbnail() {
    final String initialImageName = "initial_image_name.png";
    final String renamedImageName = "image_name_after_rename.png";
    Image testImage = null;
    try (final Tx tx = app.tx()) {
        testImage = (Image) ImageHelper.createFileBase64(securityContext, base64Image, Image.class);
        testImage.setProperties(testImage.getSecurityContext(), new PropertyMap(Image.name, initialImageName));
        assertNotNull(testImage);
        assertTrue(testImage instanceof Image);
        final Image tnSmall = testImage.getProperty(StructrApp.key(Image.class, "tnSmall"));
        final Image tnMid = testImage.getProperty(StructrApp.key(Image.class, "tnMid"));
        assertEquals("Initial small thumbnail name not as expected", ImageHelper.getThumbnailName(initialImageName, tnSmall.getWidth(), tnSmall.getHeight()), tnSmall.getProperty(StructrApp.key(Image.class, "name")));
        assertEquals("Initial mid thumbnail name not as expected", ImageHelper.getThumbnailName(initialImageName, tnMid.getWidth(), tnMid.getHeight()), tnMid.getProperty(StructrApp.key(Image.class, "name")));
        tx.success();
    } catch (Exception ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        testImage.setProperties(testImage.getSecurityContext(), new PropertyMap(Image.name, renamedImageName));
        tx.success();
    } catch (Exception ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        final Image tnSmall = testImage.getProperty(StructrApp.key(Image.class, "tnSmall"));
        final Image tnMid = testImage.getProperty(StructrApp.key(Image.class, "tnMid"));
        assertEquals("Small Thumbnail name not auto-renamed as expected", ImageHelper.getThumbnailName(renamedImageName, tnSmall.getWidth(), tnSmall.getHeight()), tnSmall.getProperty(StructrApp.key(Image.class, "name")));
        assertEquals("Mid Thumbnail name not auto-renamed as expected", ImageHelper.getThumbnailName(renamedImageName, tnMid.getWidth(), tnMid.getHeight()), tnMid.getProperty(StructrApp.key(Image.class, "name")));
        tx.success();
    } catch (Exception ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) Image(org.structr.web.entity.Image) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 17 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class UiTest method test01CreateThumbnail.

@Test
public void test01CreateThumbnail() {
    final Class imageType = createTestImageType();
    try (final Tx tx = app.tx()) {
        Image img = (Image) ImageHelper.createFileBase64(securityContext, base64Image, imageType);
        img.setProperties(img.getSecurityContext(), new PropertyMap(AbstractNode.name, "test-image.png"));
        assertNotNull(img);
        assertTrue(img instanceof Image);
        Image tn = img.getProperty(StructrApp.key(imageType, "thumbnail"));
        assertNotNull(tn);
        assertEquals(new Integer(200), tn.getWidth());
        // cropToFit = false
        assertEquals(new Integer(48), tn.getHeight());
        assertEquals("image/" + Thumbnail.Format.jpeg, tn.getContentType());
        tx.success();
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) Image(org.structr.web.entity.Image) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 18 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class UiTest method testImageAndThumbnailDelete.

@Test
public void testImageAndThumbnailDelete() {
    User tester = null;
    try (final Tx tx = app.tx()) {
        final Image image = ImageHelper.createFileBase64(securityContext, base64Image, Image.class);
        tester = app.create(User.class, "tester");
        image.setProperty(Image.name, "test.png");
        // allow non-admin user to delete the image
        image.grant(Permission.delete, tester);
        image.grant(Permission.read, tester);
        image.getProperty(StructrApp.key(Image.class, "tnSmall"));
        image.getProperty(StructrApp.key(Image.class, "tnMid"));
        assertEquals("Image should have two thumbnails", 2, image.getThumbnails().size());
        tx.success();
    } catch (IOException | FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    final SecurityContext ctx = SecurityContext.getInstance(tester, AccessMode.Backend);
    final App testerApp = StructrApp.getInstance(ctx);
    try (final Tx tx = testerApp.tx()) {
        final Image deleteMe = testerApp.nodeQuery(Image.class).getFirst();
        assertNotNull("Image should be visible to test user", deleteMe);
        testerApp.delete(deleteMe);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    try (final Tx tx = testerApp.tx()) {
        assertEquals("No images should be visible to test user", 0, testerApp.nodeQuery(Image.class).getAsList().size());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) IOException(java.io.IOException) Image(org.structr.web.entity.Image) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 19 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class ImageHelper method createCroppedImage.

public static Thumbnail createCroppedImage(final Image originalImage, final int maxWidth, final int maxHeight, final Integer reqOffsetX, final Integer reqOffsetY, final String formatString) {
    final String imageFormatString = getImageFormatString(originalImage);
    final Thumbnail.Format format = formatString != null ? Thumbnail.Format.valueOf(formatString) : (imageFormatString != null ? Thumbnail.Format.valueOf(imageFormatString) : Thumbnail.defaultFormat);
    // String contentType = (String) originalImage.getProperty(Image.CONTENT_TYPE_KEY);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final Thumbnail tn = new Thumbnail();
    try (final InputStream in = originalImage.getInputStream()) {
        if (in == null || in.available() <= 0) {
            logger.debug("InputStream of original image {} ({}) is null or not available ({} bytes)", new Object[] { originalImage.getName(), originalImage.getId(), in != null ? in.available() : -1 });
            return null;
        }
        final long start = System.nanoTime();
        final BufferedImage source = getRotatedImage(originalImage);
        if (source != null) {
            final int sourceWidth = source.getWidth();
            final int sourceHeight = source.getHeight();
            // Update image dimensions
            final PropertyMap properties = new PropertyMap();
            properties.put(StructrApp.key(Image.class, "width"), sourceWidth);
            properties.put(StructrApp.key(Image.class, "height"), sourceHeight);
            originalImage.setProperties(originalImage.getSecurityContext(), properties);
            final int offsetX = reqOffsetX != null ? reqOffsetX : 0;
            final int offsetY = reqOffsetY != null ? reqOffsetY : 0;
            final Integer[] dims = finalImageDimensions(offsetX, offsetY, maxWidth, maxHeight, sourceWidth, sourceHeight);
            logger.debug("Offset and Size (x,y,w,h): {},{},{},{}", new Object[] { dims[0], dims[1], dims[2], dims[3] });
            Thumbnails.of(source).sourceRegion(dims[0], dims[1], dims[2], dims[3]).scale(1).outputFormat(format.name()).toOutputStream(baos);
            tn.setWidth(dims[2]);
            tn.setHeight(dims[3]);
        } else {
            logger.debug("Cropped image could not be created");
            return null;
        }
        final long end = System.nanoTime();
        final long time = (end - start) / 1000000;
        logger.debug("Cropped image created. Reading, scaling and writing took {} ms", time);
        tn.setBytes(baos.toByteArray());
        return tn;
    } catch (Throwable t) {
        logger.warn("Unable to create cropped image for image with ID {}.", originalImage.getUuid(), t);
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) Image(org.structr.web.entity.Image) BufferedImage(java.awt.image.BufferedImage) PropertyMap(org.structr.core.property.PropertyMap)

Example 20 with Image

use of org.structr.web.entity.Image in project structr by structr.

the class ImageHelper method findAndReconnectOriginalImage.

public static void findAndReconnectOriginalImage(final Image thumbnail) {
    final Class<Relation> thumbnailRel = StructrApp.getConfiguration().getRelationshipEntityClass("ImageTHUMBNAILImage");
    final PropertyKey<String> pathKey = StructrApp.key(Image.class, "path");
    final String originalImageName = thumbnail.getOriginalImageName();
    try {
        final App app = StructrApp.getInstance();
        final Image originalImage = (Image) app.nodeQuery(Image.class).and(pathKey, PathHelper.getFolderPath(thumbnail.getProperty(pathKey)) + PathHelper.PATH_SEP + originalImageName).getFirst();
        if (originalImage != null) {
            final PropertyMap relProperties = new PropertyMap();
            relProperties.put(StructrApp.key(Image.class, "width"), thumbnail.getWidth());
            relProperties.put(StructrApp.key(Image.class, "height"), thumbnail.getHeight());
            relProperties.put(StructrApp.key(Image.class, "checksum"), originalImage.getChecksum());
            app.create(originalImage, thumbnail, thumbnailRel, relProperties);
        }
    } catch (FrameworkException ex) {
        logger.debug("Error reconnecting thumbnail " + thumbnail.getName() + " to original image " + originalImageName, ex);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Relation(org.structr.core.entity.Relation) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) BufferedImage(java.awt.image.BufferedImage) Image(org.structr.web.entity.Image)

Aggregations

Image (org.structr.web.entity.Image)26 FrameworkException (org.structr.common.error.FrameworkException)18 Tx (org.structr.core.graph.Tx)12 PropertyMap (org.structr.core.property.PropertyMap)12 Test (org.junit.Test)9 StructrUiTest (org.structr.web.StructrUiTest)9 BufferedImage (java.awt.image.BufferedImage)8 IOException (java.io.IOException)7 File (org.structr.web.entity.File)6 AbstractFile (org.structr.web.entity.AbstractFile)5 App (org.structr.core.app.App)4 StructrApp (org.structr.core.app.StructrApp)4 SecurityContext (org.structr.common.SecurityContext)3 Relation (org.structr.core.entity.Relation)3 PropertyKey (org.structr.core.property.PropertyKey)3 Folder (org.structr.web.entity.Folder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)2