Search in sources :

Example 46 with Folder

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

the class StructrShellCommand method findRelativeFolder.

public Folder findRelativeFolder(final Folder baseFolder, final String path) throws FrameworkException {
    final App app = StructrApp.getInstance();
    Folder folder = baseFolder;
    boolean found = false;
    for (final String part : path.split("[/]+")) {
        if (folder == null) {
            folder = app.nodeQuery(Folder.class).and(Folder.name, part).getFirst();
        } else {
            for (final Folder child : folder.getFolders()) {
                if (part.equals(child.getName())) {
                    folder = child;
                    found = true;
                }
            }
            if (!found) {
                return null;
            }
        }
    }
    return folder;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Folder(org.structr.web.entity.Folder)

Example 47 with Folder

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

the class StructrFileSystemView method getHomeDirectory.

@Override
public FtpFile getHomeDirectory() throws FtpException {
    try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
        org.structr.web.entity.User structrUser = (org.structr.web.entity.User) AuthHelper.getPrincipalForCredential(Principal.name, user.getName());
        final Folder homeDir = structrUser.getHomeDirectory();
        tx.success();
        return new StructrFtpFolder(securityContext, homeDir);
    } catch (FrameworkException fex) {
        logger.error("Error while getting home directory", fex);
    }
    return null;
}
Also used : User(org.apache.ftpserver.ftplet.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder)

Example 48 with Folder

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

the class StructrFileSystemView method getFile.

@Override
public FtpFile getFile(final String rawRequestedPath) throws FtpException {
    String requestedPath = rawRequestedPath;
    // remove trailing slash
    if (requestedPath.endsWith("/")) {
        requestedPath = requestedPath.substring(0, requestedPath.length() - 1);
    }
    logger.info("Requested path: {}, cleaned to {}", rawRequestedPath, requestedPath);
    try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
        if (StringUtils.isBlank(requestedPath) || "/".equals(requestedPath)) {
            return getHomeDirectory();
        }
        StructrFtpFolder cur = (StructrFtpFolder) getWorkingDirectory();
        if (".".equals(requestedPath) || "./".equals(requestedPath)) {
            return cur;
        }
        if ("..".equals(requestedPath) || "../".equals(requestedPath)) {
            return new StructrFtpFolder(securityContext, cur.getStructrFile().getParent());
        }
        // If relative path requested, prepend base path
        if (!requestedPath.startsWith("/")) {
            String basePath = cur.getAbsolutePath();
            logger.info("Base path: {}", basePath);
            while (requestedPath.startsWith("..")) {
                requestedPath = StringUtils.stripStart(StringUtils.stripStart(requestedPath, ".."), "/");
                basePath = StringUtils.substringBeforeLast(basePath, "/");
            }
            requestedPath = StringUtils.stripEnd(basePath.equals("/") ? "/".concat(requestedPath) : basePath.concat("/").concat(requestedPath), "/");
            logger.info("Base path: {}, requestedPath: {}", new Object[] { basePath, requestedPath });
        }
        AbstractFile file = FileHelper.getFileByAbsolutePath(securityContext, requestedPath);
        if (file != null) {
            if (file instanceof Folder) {
                tx.success();
                return new StructrFtpFolder(securityContext, (Folder) file);
            } else {
                tx.success();
                return new StructrFtpFile(securityContext, (File) file);
            }
        }
        // Look up a page by its name
        Page page = StructrApp.getInstance(securityContext).nodeQuery(Page.class).andName(PathHelper.getName(requestedPath)).getFirst();
        if (page != null) {
            tx.success();
            return new FtpFilePageWrapper(page);
        }
        logger.warn("No existing file found: {}", requestedPath);
        tx.success();
        return new FileOrFolder(requestedPath, user);
    } catch (FrameworkException fex) {
        logger.error("Error in getFile()", fex);
    }
    return null;
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Page(org.structr.web.entity.dom.Page) Folder(org.structr.web.entity.Folder)

Example 49 with Folder

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

the class StructrFtpFolder method listFiles.

@Override
public List<FtpFile> listFiles() {
    final List<FtpFile> ftpFiles = new ArrayList();
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        String requestedPath = getAbsolutePath();
        logger.debug("Children of {} requested", requestedPath);
        if ("/".equals(requestedPath)) {
            try {
                Result<Folder> folders = app.nodeQuery(Folder.class).getResult();
                logger.debug("{} folders found", folders.size());
                for (Folder f : folders.getResults()) {
                    if (f.getHasParent()) {
                        continue;
                    }
                    FtpFile ftpFile = new StructrFtpFolder(securityContext, f);
                    logger.debug("Folder found: {}", ftpFile.getAbsolutePath());
                    ftpFiles.add(ftpFile);
                }
                Result<File> files = app.nodeQuery(File.class).getResult();
                logger.debug("{} files found", files.size());
                for (File f : files.getResults()) {
                    if (f.getHasParent()) {
                        continue;
                    }
                    logger.debug("Structr file found: {}", f);
                    FtpFile ftpFile = new StructrFtpFile(securityContext, f);
                    logger.debug("File found: {}", ftpFile.getAbsolutePath());
                    ftpFiles.add(ftpFile);
                }
                Result<Page> pages = app.nodeQuery(Page.class).getResult();
                logger.debug("{} pages found", pages.size());
                for (Page p : pages.getResults()) {
                    logger.debug("Structr page found: {}", p);
                    ftpFiles.add(new FtpFilePageWrapper(p));
                }
                return ftpFiles;
            } catch (FrameworkException ex) {
                logger.error("", ex);
            }
        }
        Iterable<Folder> folders = ((Folder) structrFile).getFolders();
        for (Folder f : folders) {
            FtpFile ftpFile = new StructrFtpFolder(securityContext, f);
            logger.debug("Subfolder found: {}", ftpFile.getAbsolutePath());
            ftpFiles.add(ftpFile);
        }
        Iterable<File> files = ((Folder) structrFile).getFiles();
        for (File f : files) {
            FtpFile ftpFile = new StructrFtpFile(securityContext, f);
            logger.debug("File found: {}", ftpFile.getAbsolutePath());
            ftpFiles.add(ftpFile);
        }
        tx.success();
        return ftpFiles;
    } catch (FrameworkException fex) {
        logger.error("Error in listFiles()", fex);
    }
    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) ArrayList(java.util.ArrayList) Page(org.structr.web.entity.dom.Page) FtpFile(org.apache.ftpserver.ftplet.FtpFile) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File) FtpFile(org.apache.ftpserver.ftplet.FtpFile)

Example 50 with Folder

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

the class StructrFilesPath method getDirectoryStream.

@Override
public DirectoryStream<Path> getDirectoryStream(final DirectoryStream.Filter<? super Path> filter) {
    return new DirectoryStream() {

        boolean closed = false;

        @Override
        public Iterator iterator() {
            if (!closed) {
                final App app = StructrApp.getInstance(fs.getSecurityContext());
                final PropertyKey<Boolean> hasParentKey = StructrApp.key(AbstractFile.class, "hasParent");
                final List<StructrPath> files = new LinkedList<>();
                try (final Tx tx = app.tx()) {
                    for (final Folder folder : app.nodeQuery(Folder.class).and(hasParentKey, false).getAsList()) {
                        files.add(new StructrFilePath(fs, StructrFilesPath.this, folder.getName()));
                    }
                    for (final File file : app.nodeQuery(File.class).and(hasParentKey, false).getAsList()) {
                        files.add(new StructrFilePath(fs, StructrFilesPath.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;
        }
    };
}
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