Search in sources :

Example 1 with Image

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

the class UiTest method testCreateFolder.

@Test
public void testCreateFolder() {
    Folder folder1 = null;
    try (final Tx tx = app.tx()) {
        folder1 = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/folder1");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        File file1 = (File) app.create(File.class, "file1");
        assertNotNull(file1);
        assertEquals(file1.getPath(), "/file1");
        file1.setProperty(StructrApp.key(File.class, "parent"), folder1);
        assertEquals(file1.getPath(), "/folder1/file1");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        Image image1 = (Image) app.create(Image.class, "image1");
        assertNotNull(image1);
        assertEquals(image1.getPath(), "/image1");
        image1.setProperty(StructrApp.key(File.class, "parent"), folder1);
        assertEquals(image1.getPath(), "/folder1/image1");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        assertEquals(2, Iterables.toList(folder1.getFiles()).size());
        assertEquals(1, Iterables.toList(folder1.getImages()).size());
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) Image(org.structr.web.entity.Image) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with Image

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

the class UiTest method test01AutoRenameThumbnailForImageSubclass.

@Test
public void test01AutoRenameThumbnailForImageSubclass() {
    final Class testImageType = createTestImageType();
    Image subclassTestImage = null;
    final String initialImageName = "initial_image_name.png";
    final String renamedImageName = "image_name_after_rename.png";
    try (final Tx tx = app.tx()) {
        subclassTestImage = (Image) ImageHelper.createFileBase64(securityContext, base64Image, testImageType);
        subclassTestImage.setProperties(subclassTestImage.getSecurityContext(), new PropertyMap(Image.name, initialImageName));
        assertNotNull(subclassTestImage);
        assertTrue(subclassTestImage instanceof Image);
        final Image tnSmall = subclassTestImage.getProperty(StructrApp.key(testImageType, "tnSmall"));
        final Image tnMid = subclassTestImage.getProperty(StructrApp.key(testImageType, "tnMid"));
        final Image tnCustom = subclassTestImage.getProperty(StructrApp.key(testImageType, "thumbnail"));
        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")));
        assertEquals("Initial custom thumbnail name not as expected", ImageHelper.getThumbnailName(initialImageName, tnCustom.getWidth(), tnCustom.getHeight()), tnCustom.getProperty(StructrApp.key(Image.class, "name")));
        tx.success();
    } catch (Exception ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        subclassTestImage.setProperties(subclassTestImage.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 = subclassTestImage.getProperty(StructrApp.key(testImageType, "tnSmall"));
        final Image tnMid = subclassTestImage.getProperty(StructrApp.key(testImageType, "tnMid"));
        final Image tnCustom = subclassTestImage.getProperty(StructrApp.key(testImageType, "thumbnail"));
        assertEquals("Small Thumbnail name not auto-renamed as expected for image subclass", ImageHelper.getThumbnailName(renamedImageName, tnSmall.getWidth(), tnSmall.getHeight()), tnSmall.getProperty(StructrApp.key(Image.class, "name")));
        assertEquals("Mid Thumbnail name not auto-renamed as expected for image subclass", ImageHelper.getThumbnailName(renamedImageName, tnMid.getWidth(), tnMid.getHeight()), tnMid.getProperty(StructrApp.key(Image.class, "name")));
        assertEquals("Custom Thumbnail name not auto-renamed as expected for image subclass", ImageHelper.getThumbnailName(renamedImageName, tnCustom.getWidth(), tnCustom.getHeight()), tnCustom.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 3 with Image

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

the class ODFExporter method exportImage.

public static void exportImage(final ODFExporter thisNode, final String uuid) {
    final File output = thisNode.getResultDocument();
    try {
        final App app = StructrApp.getInstance();
        final Image result = app.nodeQuery(Image.class).and(GraphObject.id, uuid).getFirst();
        String imageName = result.getProperty(new StringProperty("name"));
        String contentType = result.getProperty(new StringProperty("contentType"));
        String templateImagePath = null;
        OdfDocument doc = OdfDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
        NodeList nodes = doc.getContentRoot().getElementsByTagName(ODF_IMAGE_PARENT_NAME);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node currentNode = nodes.item(i);
            NamedNodeMap attrs = currentNode.getAttributes();
            Node fieldName = attrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_PARENT_IMAGE_NAME);
            if (fieldName != null && fieldName.getTextContent().equals(imageName)) {
                NamedNodeMap childAttrs = currentNode.getFirstChild().getAttributes();
                Node filePath = childAttrs.getNamedItem(ODF_IMAGE_ATTRIBUTE_FILE_PATH);
                templateImagePath = filePath.getTextContent();
                filePath.setTextContent(ODF_IMAGE_DIRECTORY + imageName);
            }
        }
        OdfPackage pkg = doc.getPackage();
        if (templateImagePath != null && templateImagePath.length() > 0) {
            pkg.remove(templateImagePath);
        }
        pkg.insert(new URI(result.getFileOnDisk().getAbsolutePath()), ODF_IMAGE_DIRECTORY + imageName, contentType);
        pkg.save(output.getFileOnDisk().getAbsolutePath());
        pkg.close();
        doc.close();
    } catch (Exception e) {
        logger.error("Error while exporting image to document", e);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) Image(org.structr.web.entity.Image) URI(java.net.URI) FrameworkException(org.structr.common.error.FrameworkException) OdfPackage(org.odftoolkit.odfdom.pkg.OdfPackage) OdfDocument(org.odftoolkit.odfdom.doc.OdfDocument) File(org.structr.web.entity.File)

Example 4 with Image

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

the class DirectFileImportCommand method createFileOrFolder.

private FileVisitResult createFileOrFolder(final SecurityContext ctx, final App app, final Path path, final Path file, final BasicFileAttributes attrs, final String sourcePath, final String targetPath, final Mode mode, final Existing existing, final boolean doIndex) {
    ctx.setDoTransactionNotifications(false);
    final String name = file.getFileName().toString();
    try (final Tx tx = app.tx()) {
        final String relativePath = PathHelper.getRelativeNodePath(path.toString(), file.toString());
        String parentPath = targetPath + PathHelper.getFolderPath(relativePath);
        // fix broken path concatenation
        if (parentPath.startsWith("//")) {
            parentPath = parentPath.substring(1);
        }
        if (attrs.isDirectory()) {
            final Folder newFolder = app.create(Folder.class, new NodeAttribute(Folder.name, name), new NodeAttribute(StructrApp.key(File.class, "parent"), FileHelper.createFolderPath(securityContext, parentPath)));
            folderCount++;
            logger.info("Created folder " + newFolder.getPath());
        } else if (attrs.isRegularFile()) {
            final File existingFile = app.nodeQuery(File.class).and(StructrApp.key(AbstractFile.class, "path"), parentPath + name).getFirst();
            if (existingFile != null) {
                switch(existing) {
                    case SKIP:
                        logger.info("Skipping import of {}, file exists and mode is SKIP.", parentPath + name);
                        return FileVisitResult.CONTINUE;
                    case OVERWRITE:
                        logger.info("Overwriting {}, file exists and mode is OVERWRITE.", parentPath + name);
                        app.delete(existingFile);
                        break;
                    case RENAME:
                        logger.info("Renaming existing file {}, file exists and mode is RENAME.", parentPath + name);
                        existingFile.setProperty(AbstractFile.name, existingFile.getProperty(AbstractFile.name).concat("_").concat(FileHelper.getDateString()));
                        break;
                }
            }
            final String contentType = FileHelper.getContentMimeType(file.toFile(), file.getFileName().toString());
            boolean isImage = (contentType != null && contentType.startsWith("image"));
            boolean isVideo = (contentType != null && contentType.startsWith("video"));
            Class cls = null;
            if (isImage) {
                cls = Image.class;
            } else if (isVideo) {
                cls = SchemaHelper.getEntityClassForRawType("VideoFile");
                if (cls == null) {
                    logger.warn("Unable to create entity of type VideoFile, class is not defined.");
                }
            } else {
                cls = File.class;
            }
            final File newFile = (File) app.create(cls, new NodeAttribute(File.name, name), new NodeAttribute(StructrApp.key(File.class, "parent"), FileHelper.createFolderPath(securityContext, parentPath)), new NodeAttribute(AbstractNode.type, cls.getSimpleName()));
            final java.io.File fileOnDisk = newFile.getFileOnDisk(false);
            final Path fullFolderPath = fileOnDisk.toPath();
            Files.createDirectories(fullFolderPath.getParent());
            switch(mode) {
                case MOVE:
                    Files.move(file, fullFolderPath);
                    break;
                case COPY:
                    Files.copy(file, fullFolderPath);
                    break;
            }
            FileHelper.updateMetadata(newFile);
            if (doIndex) {
                indexer.addToFulltextIndex(newFile);
            }
            fileCount++;
        }
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.debug("File: " + name + ", path: " + sourcePath, ex);
    }
    return FileVisitResult.CONTINUE;
}
Also used : Path(java.nio.file.Path) NodeAttribute(org.structr.core.graph.NodeAttribute) AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) Image(org.structr.web.entity.Image) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 5 with Image

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

the class ClosingFileOutputStream method close.

@Override
public void close() throws IOException {
    if (closed) {
        return;
    }
    try (Tx tx = StructrApp.getInstance().tx()) {
        super.close();
        final String _contentType = FileHelper.getContentMimeType(thisFile);
        final PropertyMap changedProperties = new PropertyMap();
        changedProperties.put(StructrApp.key(File.class, "checksum"), FileHelper.getChecksum(file));
        changedProperties.put(StructrApp.key(File.class, "size"), file.length());
        changedProperties.put(StructrApp.key(File.class, "contentType"), _contentType);
        if (StringUtils.startsWith(_contentType, "image") || ImageHelper.isImageType(thisFile.getName())) {
            changedProperties.put(NodeInterface.type, Image.class.getSimpleName());
        }
        thisFile.unlockSystemPropertiesOnce();
        thisFile.setProperties(thisFile.getSecurityContext(), changedProperties);
        thisFile.increaseVersion();
        if (notifyIndexerAfterClosing) {
            thisFile.notifyUploadCompletion();
        }
        tx.success();
    } catch (Throwable ex) {
        logger.error("Could not determine or save checksum and size after closing file output stream", ex);
    }
    closed = true;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) Image(org.structr.web.entity.Image) File(org.structr.web.entity.File)

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