Search in sources :

Example 26 with Folder

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

the class FileOrFolder method createOutputStream.

@Override
public OutputStream createOutputStream(final long l) throws IOException {
    try (Tx tx = StructrApp.getInstance().tx()) {
        if (structrFile == null) {
            final Folder parentFolder = (Folder) FileHelper.getFileByAbsolutePath(securityContext, StringUtils.substringBeforeLast(newPath, "/"));
            try {
                structrFile = FileHelper.createFile(securityContext, new byte[0], null, File.class, getName());
                structrFile.setProperty(AbstractNode.type, File.class.getSimpleName());
                structrFile.setProperty(AbstractNode.owner, owner.getStructrUser());
                if (parentFolder != null) {
                    structrFile.setParent(parentFolder);
                }
            } catch (FrameworkException ex) {
                logger.error("", ex);
                return null;
            }
        }
        tx.success();
        return ((File) structrFile).getOutputStream();
    } catch (FrameworkException fex) {
        logger.error(null, fex);
    }
    return null;
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) FtpFile(org.apache.ftpserver.ftplet.FtpFile)

Example 27 with Folder

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

the class FileOrFolder method mkdir.

@Override
public boolean mkdir() {
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        logger.info("mkdir() Folder");
        AbstractFile existing = FileHelper.getFileByAbsolutePath(SecurityContext.getSuperUserInstance(), newPath);
        if (existing != null) {
            logger.warn("File {} already exists.", newPath);
            return false;
        }
        final Folder parentFolder = (Folder) FileHelper.getFileByAbsolutePath(securityContext, StringUtils.substringBeforeLast(newPath, "/"));
        try {
            Folder newFolder = (Folder) app.command(CreateNodeCommand.class).execute(new NodeAttribute(AbstractNode.type, Folder.class.getSimpleName()), new NodeAttribute(AbstractNode.owner, owner.getStructrUser()), new NodeAttribute(AbstractNode.name, getName()));
            if (parentFolder != null) {
                newFolder.setParent(parentFolder);
            }
        } catch (FrameworkException ex) {
            logger.error("", ex);
            return false;
        }
        tx.success();
        return true;
    } catch (FrameworkException ex) {
        logger.error("", ex);
        return false;
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) NodeAttribute(org.structr.core.graph.NodeAttribute) AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder)

Example 28 with Folder

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

the class StructrSSHFile method findFile.

public StructrSSHFile findFile(final String path) {
    if ("/".equals(path)) {
        return getRootFolder();
    }
    final App app = StructrApp.getInstance(getRootFolder().getSecurityContext());
    final boolean isAbsolute = path.startsWith("/");
    final String localPath = isAbsolute ? path.substring(1) : path;
    final String[] parts = localPath.split("[/]+");
    final String localPart = parts[0];
    if (".".equals(path)) {
        return this;
    }
    if ("..".equals(path)) {
        return parent;
    }
    if (isAbsolute && parent != null) {
        return getRootFolder().findFile(path);
    } else {
        // look for files/folders without parent
        try (final Tx tx = app.tx()) {
            for (final Folder folder : getFolders()) {
                final String folderName = folder.getName();
                if (localPart.equals(folderName)) {
                    final StructrSSHFile matchingFolder = new StructrSSHFile(this, folderName, folder);
                    if (parts.length > 1) {
                        return matchingFolder.findFile(localPath.substring(folderName.length() + 1));
                    } else {
                        // match found
                        return matchingFolder;
                    }
                }
            }
            for (final File file : getFiles()) {
                final String fileName = file.getName();
                if (localPart.equals(fileName)) {
                    if (parts.length > 1) {
                        throw new IllegalStateException("Found file where folder was expected, aborting.");
                    }
                    return new StructrSSHFile(this, fileName, file);
                }
            }
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("", fex);
        }
    }
    return new StructrSSHFile(this, path, null);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 29 with Folder

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

the class StructrShellCommand method getPrompt.

public String getPrompt() {
    final App app = StructrApp.getInstance();
    final StringBuilder buf = new StringBuilder();
    try (final Tx tx = app.tx()) {
        buf.append(user.getName());
        buf.append("@structr:");
        if (currentFolder != null) {
            String folderPart = currentFolder.getPath();
            final Folder homeFolder = user.getHomeDirectory();
            if (homeFolder != null) {
                // replace home directory with ~ if at the beginning of the full path
                final String homeFolderPath = homeFolder.getPath();
                if (folderPart.startsWith(homeFolderPath)) {
                    folderPart = "~" + folderPart.substring(homeFolderPath.length());
                }
            }
            buf.append(folderPart);
        } else {
            buf.append("/");
        }
        buf.append(user.isAdmin() ? "#" : "$");
        buf.append(" ");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    return buf.toString();
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder)

Example 30 with Folder

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

the class StructrFilePath method getDirectoryStream.

@Override
public DirectoryStream<Path> getDirectoryStream(DirectoryStream.Filter<? super Path> filter) {
    final Folder folder = (Folder) getActualFile();
    if (folder != null) {
        return new DirectoryStream() {

            boolean closed = false;

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

            @Override
            public void close() throws IOException {
                closed = true;
            }
        };
    }
    return null;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StructrPath(org.structr.files.ssh.filesystem.StructrPath) DirectoryStream(java.nio.file.DirectoryStream) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) LinkedList(java.util.LinkedList)

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