Search in sources :

Example 41 with Folder

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

the class UiSyncCommand method doExport.

// ----- private methods -----
private void doExport(final String fileName) throws FrameworkException {
    // collect all nodes etc that belong to the frontend (including files)
    // and export them to the given output file
    final Set<RelationshipInterface> rels = new LinkedHashSet<>();
    final Set<NodeInterface> nodes = new LinkedHashSet<>();
    final Set<String> filePaths = new LinkedHashSet<>();
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        // collect folders that are marked for export
        for (final Folder folder : app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "includeInFrontendExport"), true).getAsList()) {
            collectDataRecursively(app, folder, nodes, rels, filePaths);
        }
        // collect pages (including files, shared components etc.)
        for (final Page page : app.nodeQuery(Page.class).getAsList()) {
            collectDataRecursively(app, page, nodes, rels, filePaths);
        }
        SyncCommand.exportToFile(fileName, nodes, rels, filePaths, true);
        tx.success();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) RelationshipInterface(org.structr.core.graph.RelationshipInterface) Page(org.structr.web.entity.dom.Page) Folder(org.structr.web.entity.Folder) NodeInterface(org.structr.core.graph.NodeInterface)

Example 42 with Folder

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

the class UnarchiveCommand method unarchive.

private void unarchive(final SecurityContext securityContext, final File file, final String parentFolderId) throws ArchiveException, IOException, FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    final InputStream is;
    Folder existingParentFolder = null;
    final String fileName = file.getName();
    try (final Tx tx = app.tx()) {
        // search for existing parent folder
        existingParentFolder = app.get(Folder.class, parentFolderId);
        String parentFolderName = null;
        String msgString = "Unarchiving file {}";
        if (existingParentFolder != null) {
            parentFolderName = existingParentFolder.getName();
            msgString += " into existing folder {}.";
        }
        logger.info(msgString, new Object[] { fileName, parentFolderName });
        is = file.getInputStream();
        tx.success();
        if (is == null) {
            getWebSocket().send(MessageBuilder.status().code(400).message("Could not get input stream from file ".concat(fileName)).build(), true);
            return;
        }
        tx.success();
    }
    final BufferedInputStream bufferedIs = new BufferedInputStream(is);
    switch(ArchiveStreamFactory.detect(bufferedIs)) {
        // 7z doesn't support streaming
        case ArchiveStreamFactory.SEVEN_Z:
            {
                int overallCount = 0;
                logger.info("7-Zip archive format detected");
                try (final Tx outertx = app.tx()) {
                    SevenZFile sevenZFile = new SevenZFile(file.getFileOnDisk());
                    SevenZArchiveEntry sevenZEntry = sevenZFile.getNextEntry();
                    while (sevenZEntry != null) {
                        try (final Tx tx = app.tx(true, true, false)) {
                            int count = 0;
                            while (sevenZEntry != null && count++ < 50) {
                                final String entryPath = "/" + PathHelper.clean(sevenZEntry.getName());
                                logger.info("Entry path: {}", entryPath);
                                if (sevenZEntry.isDirectory()) {
                                    handleDirectory(securityContext, existingParentFolder, entryPath);
                                } else {
                                    byte[] buf = new byte[(int) sevenZEntry.getSize()];
                                    sevenZFile.read(buf, 0, buf.length);
                                    try (final ByteArrayInputStream in = new ByteArrayInputStream(buf)) {
                                        handleFile(securityContext, in, existingParentFolder, entryPath);
                                    }
                                }
                                sevenZEntry = sevenZFile.getNextEntry();
                                overallCount++;
                            }
                            logger.info("Committing transaction after {} entries.", overallCount);
                            tx.success();
                        }
                    }
                    logger.info("Unarchived {} files.", overallCount);
                    outertx.success();
                }
                break;
            }
        // ZIP needs special treatment to support "unsupported feature data descriptor"
        case ArchiveStreamFactory.ZIP:
            {
                logger.info("Zip archive format detected");
                try (final ZipArchiveInputStream in = new ZipArchiveInputStream(bufferedIs, null, false, true)) {
                    handleArchiveInputStream(in, app, securityContext, existingParentFolder);
                }
                break;
            }
        default:
            {
                logger.info("Default archive format detected");
                try (final ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(bufferedIs)) {
                    handleArchiveInputStream(in, app, securityContext, existingParentFolder);
                }
            }
    }
    getWebSocket().send(MessageBuilder.finished().callback(callback).data("success", true).data("filename", fileName).build(), true);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) SevenZFile(org.apache.commons.compress.archivers.sevenz.SevenZFile) Tx(org.structr.core.graph.Tx) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) InputStream(java.io.InputStream) SevenZArchiveEntry(org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry) Folder(org.structr.web.entity.Folder)

Example 43 with Folder

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

the class AppendFileCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    String id = webSocketData.getId();
    Map<String, Object> nodeData = webSocketData.getNodeData();
    String parentId = (String) nodeData.get("parentId");
    // check node to append
    if (id == null) {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node, no id is given").build(), true);
        return;
    }
    // check for parent ID
    if (parentId == null) {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node without parentId").build(), true);
        return;
    }
    // never append to self
    if (parentId.equals(id)) {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append node as its own child.").build(), true);
        return;
    }
    // check if parent node with given ID exists
    AbstractNode parentNode = getNode(parentId);
    if (parentNode == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
        return;
    }
    if (parentNode instanceof Folder) {
        Folder folder = (Folder) parentNode;
        AbstractFile file = (AbstractFile) getNode(id);
        if (file != null) {
            try {
                // Remove from existing parent
                Folder currentParent = (Folder) file.treeGetParent();
                if (currentParent != null) {
                    currentParent.treeRemoveChild(file);
                }
                folder.treeAppendChild(file);
                TransactionCommand.registerNodeCallback(file, callback);
            } catch (FrameworkException ex) {
                logger.error("", ex);
                getWebSocket().send(MessageBuilder.status().code(422).message("Cannot append file").build(), true);
            }
        }
    } else {
        // send exception
        getWebSocket().send(MessageBuilder.status().code(422).message("Parent node is not instance of Folder").build(), true);
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) Folder(org.structr.web.entity.Folder)

Example 44 with Folder

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

the class StructrPosixFileAttributes method isDirectory.

@Override
public boolean isDirectory() {
    boolean isDirectory = false;
    try (Tx tx = StructrApp.getInstance().tx()) {
        isDirectory = file instanceof Folder;
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("", fex);
    }
    return isDirectory;
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder)

Example 45 with Folder

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

the class StructrSSHFileSystem method provider.

@Override
public FileSystemProvider provider() {
    logger.info("x");
    return new FileSystemProvider() {

        @Override
        public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException {
            logger.info("x");
            OutputStream os = null;
            File actualFile = (File) ((StructrSSHFile) path).getActualFile();
            try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
                if (actualFile == null) {
                    actualFile = (File) create(path);
                }
                if (actualFile != null) {
                    os = ((File) actualFile).getOutputStream();
                }
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                throw new IOException(fex);
            }
            return os;
        }

        @Override
        public InputStream newInputStream(Path path, OpenOption... options) throws IOException {
            // Remote file => file node in Structr
            logger.info("x");
            InputStream inputStream = null;
            try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
                final File fileNode = (File) ((StructrSSHFile) path).getActualFile();
                inputStream = fileNode.getInputStream();
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                throw new IOException(fex);
            }
            return inputStream;
        }

        @Override
        public String getScheme() {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public FileSystem newFileSystem(URI uri, Map<String, ?> env) throws IOException {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public FileSystem getFileSystem(URI uri) {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public Path getPath(URI uri) {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException {
            logger.info("x");
            SeekableByteChannel channel = null;
            final File fileNode = (File) ((StructrSSHFile) path).getActualFile();
            if (fileNode != null) {
                try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
                    final Path filePath = fileNode.getFileOnDisk().toPath();
                    channel = Files.newByteChannel(filePath);
                    tx.success();
                } catch (FrameworkException fex) {
                    logger.error("", fex);
                    throw new IOException(fex);
                }
            }
            return channel;
        }

        @Override
        public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
            logger.info("x");
            return new DirectoryStream() {

                boolean closed = false;

                @Override
                public Iterator iterator() {
                    if (!closed) {
                        final App app = StructrApp.getInstance(securityContext);
                        final List<StructrSSHFile> files = new LinkedList<>();
                        final StructrSSHFile thisDir = (StructrSSHFile) dir;
                        try (final Tx tx = app.tx()) {
                            for (final Folder child : thisDir.getFolders()) {
                                files.add(new StructrSSHFile(thisDir, child.getName(), child));
                            }
                            for (final File child : thisDir.getFiles()) {
                                files.add(new StructrSSHFile(thisDir, child.getName(), child));
                            }
                            tx.success();
                        } catch (FrameworkException fex) {
                            logger.warn("", fex);
                        }
                        return files.iterator();
                    }
                    return Collections.emptyIterator();
                }

                @Override
                public void close() throws IOException {
                    closed = true;
                }
            };
        }

        @Override
        public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
            logger.info("x");
            final StructrSSHFile parent = (StructrSSHFile) dir.getParent();
            final App app = StructrApp.getInstance(securityContext);
            final String name = dir.getFileName().toString();
            try (final Tx tx = app.tx()) {
                final Folder folder = app.create(Folder.class, new NodeAttribute(AbstractNode.name, name), new NodeAttribute(StructrApp.key(AbstractFile.class, "parent"), parent != null ? parent.getActualFile() : null));
                ((StructrSSHFile) dir).setActualFile(folder);
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                throw new IOException(fex);
            }
        }

        @Override
        public void delete(Path path) throws IOException {
            logger.info("Method not implemented yet");
        }

        @Override
        public void copy(Path source, Path target, CopyOption... options) throws IOException {
            logger.info("Method not implemented yet");
        }

        @Override
        public void move(Path source, Path target, CopyOption... options) throws IOException {
            logger.info("Method not implemented yet");
        }

        @Override
        public boolean isSameFile(Path path, Path path2) throws IOException {
            logger.info("x");
            return path != null && path.equals(path);
        }

        @Override
        public boolean isHidden(Path path) throws IOException {
            logger.info("Method not implemented yet");
            return false;
        }

        @Override
        public FileStore getFileStore(Path path) throws IOException {
            logger.info("Method not implemented yet");
            return null;
        }

        @Override
        public void checkAccess(Path path, AccessMode... modes) throws IOException {
            logger.info("Checking access", new Object[] { path, modes });
        }

        @Override
        public <V extends FileAttributeView> V getFileAttributeView(final Path path, final Class<V> type, final LinkOption... options) {
            logger.info("x");
            return (V) new PosixFileAttributeView() {

                @Override
                public String name() {
                    return "posix";
                }

                @Override
                public PosixFileAttributes readAttributes() throws IOException {
                    return new StructrPosixFileAttributes((StructrSSHFile) path);
                }

                @Override
                public void setPermissions(Set<PosixFilePermission> set) throws IOException {
                    logger.info("Method not implemented yet");
                }

                @Override
                public void setGroup(GroupPrincipal gp) throws IOException {
                    logger.info("Method not implemented yet");
                }

                @Override
                public void setTimes(FileTime ft, FileTime ft1, FileTime ft2) throws IOException {
                    logger.info("Method not implemented yet");
                }

                @Override
                public UserPrincipal getOwner() throws IOException {
                    logger.info("Method not implemented yet");
                    return null;
                }

                @Override
                public void setOwner(UserPrincipal up) throws IOException {
                    logger.info("Method not implemented yet");
                }
            };
        }

        @Override
        public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) throws IOException {
            logger.info("x");
            if (path != null) {
                if (path instanceof StructrSSHFile) {
                    final StructrSSHFile sshFile = (StructrSSHFile) path;
                    if (sshFile.getActualFile() == null) {
                        throw new NoSuchFileException("SSH file doesn't exist");
                    }
                    BasicFileAttributes attrs = new StructrPosixFileAttributes((StructrSSHFile) path);
                    return (A) attrs;
                }
            }
            throw new IOException("Unable to read attributes: Path is null");
        }

        @Override
        public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) throws IOException {
            return Collections.EMPTY_MAP;
        }

        @Override
        public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException {
            logger.info("Method not implemented yet");
            ;
        }

        private AbstractFile create(final Path path) throws IOException {
            logger.info("x");
            final StructrSSHFile parent = (StructrSSHFile) path.getParent();
            AbstractFile newFile = null;
            final App app = StructrApp.getInstance(securityContext);
            try (final Tx tx = app.tx()) {
                final String fileName = path.getFileName().toString();
                final Folder parentFolder = (Folder) parent.getActualFile();
                newFile = app.create(File.class, new NodeAttribute(AbstractNode.name, fileName), new NodeAttribute(StructrApp.key(AbstractFile.class, "parent"), parentFolder));
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                throw new IOException(fex);
            }
            return newFile;
        }
    };
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) HashSet(java.util.HashSet) Set(java.util.Set) CopyOption(java.nio.file.CopyOption) AbstractFile(org.structr.web.entity.AbstractFile) OutputStream(java.io.OutputStream) DirectoryStream(java.nio.file.DirectoryStream) NoSuchFileException(java.nio.file.NoSuchFileException) FileTime(java.nio.file.attribute.FileTime) Folder(org.structr.web.entity.Folder) URI(java.net.URI) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Path(java.nio.file.Path) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkOption(java.nio.file.LinkOption) InputStream(java.io.InputStream) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) LinkedList(java.util.LinkedList) UserPrincipal(java.nio.file.attribute.UserPrincipal) SeekableByteChannel(java.nio.channels.SeekableByteChannel) OpenOption(java.nio.file.OpenOption) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) FileSystemProvider(java.nio.file.spi.FileSystemProvider) AccessMode(java.nio.file.AccessMode) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Map(java.util.Map) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) FileAttributeView(java.nio.file.attribute.FileAttributeView) FileAttribute(java.nio.file.attribute.FileAttribute)

Aggregations

Folder (org.structr.web.entity.Folder)95 Tx (org.structr.core.graph.Tx)64 FrameworkException (org.structr.common.error.FrameworkException)60 AbstractFile (org.structr.web.entity.AbstractFile)42 App (org.structr.core.app.App)35 StructrApp (org.structr.core.app.StructrApp)35 File (org.structr.web.entity.File)34 Test (org.junit.Test)23 StructrUiTest (org.structr.web.StructrUiTest)21 IOException (java.io.IOException)20 PropertyMap (org.structr.core.property.PropertyMap)16 Path (java.nio.file.Path)14 LinkedList (java.util.LinkedList)10 NodeAttribute (org.structr.core.graph.NodeAttribute)9 InputStream (java.io.InputStream)5 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)5 CMISRootFolder (org.structr.files.cmis.repository.CMISRootFolder)5 User (org.structr.web.entity.User)5 Page (org.structr.web.entity.dom.Page)5 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4