Search in sources :

Example 21 with AbstractFile

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

the class StructrFilePath method delete.

@Override
public void delete() throws IOException {
    final App app = StructrApp.getInstance(fs.getSecurityContext());
    final AbstractFile actualFile = getActualFile();
    try (final Tx tx = app.tx()) {
        // if a folder is to be deleted, check contents
        if (actualFile instanceof Folder && ((Folder) actualFile).getChildren().iterator().hasNext()) {
            throw new DirectoryNotEmptyException(getActualFile().getPath());
        } else {
            app.delete(actualFile);
            // remove cached version
            this.cachedActualFile = null;
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("Unable to delete file {}: {}", new Object[] { getActualFile().getPath(), fex.getMessage() });
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) Folder(org.structr.web.entity.Folder)

Example 22 with AbstractFile

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

the class AbstractStructrFtpFile method move.

@Override
public boolean move(final FtpFile target) {
    try (Tx tx = StructrApp.getInstance(securityContext).tx()) {
        logger.info("move()");
        final AbstractStructrFtpFile targetFile = (AbstractStructrFtpFile) target;
        final String path = targetFile instanceof StructrFtpFile ? "/" : targetFile.getAbsolutePath();
        try {
            if (path.contains("/")) {
                String newParentPath = StringUtils.substringBeforeLast(path, "/");
                AbstractFile newParent = FileHelper.getFileByAbsolutePath(securityContext, newParentPath);
                if (newParent != null && newParent instanceof Folder) {
                    Folder newParentFolder = (Folder) newParent;
                    structrFile.setParent(newParentFolder);
                } else {
                    // Move to /
                    structrFile.setParent(null);
                }
            }
            if (!("/".equals(path))) {
                final String newName = path.contains("/") ? StringUtils.substringAfterLast(path, "/") : path;
                structrFile.setProperty(AbstractNode.name, newName);
            }
        } catch (FrameworkException ex) {
            logger.error("Could not move ftp file", ex);
            return false;
        }
        tx.success();
        return true;
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    return false;
}
Also used : 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 23 with AbstractFile

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

the class CatCommand method listFolder.

// ----- private methods -----
private void listFolder(final StructrShellCommand parent, final Iterable<AbstractFile> folder) throws FrameworkException, IOException {
    boolean hasContents = false;
    for (final AbstractFile child : folder) {
        if (parent.isAllowed(child, Permission.read, false)) {
            hasContents = true;
            if (child instanceof Folder) {
                term.setBold(true);
                term.setTextColor(4);
                term.print(child.getName() + "  ");
                term.setTextColor(7);
                term.setBold(false);
            } else {
                term.print(child.getName() + "  ");
            }
        }
    }
    if (hasContents) {
        term.println();
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) Folder(org.structr.web.entity.Folder)

Example 24 with AbstractFile

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

the class CatCommand method displayAutocompleteSuggestions.

private void displayAutocompleteSuggestions(final StructrShellCommand parent, final Iterable<AbstractFile> files, final String line) throws IOException {
    final StringBuilder buf = new StringBuilder();
    for (final AbstractFile file : files) {
        if (parent.isAllowed(file, Permission.read, false)) {
            buf.append(file.getName());
            if (file instanceof Folder) {
                buf.append("/  ");
            }
        }
    }
    if (buf.length() > 0) {
        term.println();
        term.print(buf.toString());
        term.println();
        parent.displayPrompt();
        term.print(line);
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) Folder(org.structr.web.entity.Folder)

Example 25 with AbstractFile

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

the class CatCommand method handleTabCompletion.

@Override
public void handleTabCompletion(final StructrShellCommand parent, final String line, final int tabCount) throws IOException {
    if (line.contains(" ") && line.length() >= 3) {
        String incompletePath = line.substring(line.indexOf(" ") + 1);
        Folder baseFolder = null;
        String lastPathPart = null;
        if (incompletePath.startsWith("\"")) {
            incompletePath = incompletePath.substring(1);
        }
        final App app = StructrApp.getInstance();
        if ("..".equals(incompletePath)) {
            term.handleCharacter('/');
            return;
        }
        if (incompletePath.startsWith("/")) {
            incompletePath = incompletePath.substring(1);
        } else {
            baseFolder = parent.getCurrentFolder();
        }
        // identify full path parts and find folders
        final String[] parts = incompletePath.split("[/]+");
        final int partCount = parts.length;
        try (final Tx tx = app.tx()) {
            // only a single path part
            if (partCount == 1) {
                lastPathPart = parts[0];
            } else {
                lastPathPart = parts[partCount - 1];
                // more than a single path part, find preceding folders
                for (int i = 0; i < partCount - 1; i++) {
                    // skip empty path parts
                    if (StringUtils.isNotBlank(parts[i])) {
                        baseFolder = app.nodeQuery(Folder.class).and(StructrApp.key(AbstractFile.class, "parent"), baseFolder).and(Folder.name, parts[i]).getFirst();
                        if (baseFolder == null) {
                            return;
                        }
                    }
                }
            }
            final List<AbstractFile> allFiles = new LinkedList<>();
            final List<AbstractFile> files = new LinkedList<>();
            if (baseFolder != null) {
                allFiles.addAll(Iterables.toList(baseFolder.getChildren()));
            } else {
                allFiles.addAll(app.nodeQuery(AbstractFile.class).and(StructrApp.key(File.class, "parent"), null).getAsList());
            }
            for (final AbstractFile file : allFiles) {
                if (file.getName().startsWith(lastPathPart)) {
                    files.add(file);
                }
            }
            if (files.size() > 1) {
                // only display autocomplete suggestions after second tab
                if (tabCount > 1) {
                    displayAutocompleteSuggestions(parent, files, line);
                }
            } else if (!files.isEmpty()) {
                final AbstractFile file = files.get(0);
                if (file instanceof Folder) {
                    final Folder folder = (Folder) file;
                    if (parent.isAllowed(folder, Permission.read, false)) {
                        if (lastPathPart.equals(folder.getName())) {
                            // only display autocomplete suggestions after second tab
                            if (tabCount > 1) {
                                displayAutocompleteSuggestions(parent, folder.getChildren(), line);
                            } else {
                                if (!line.endsWith("/")) {
                                    term.handleCharacter('/');
                                }
                            }
                        } else {
                            displayAutocompleteItem(folder, lastPathPart);
                        }
                    }
                } else {
                    final AbstractFile existingFile = files.get(0);
                    if (parent.isAllowed(existingFile, Permission.read, false)) {
                        if (!lastPathPart.equals(existingFile.getName())) {
                            displayAutocompleteItem(existingFile, lastPathPart);
                        }
                    }
                }
            }
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("", fex);
        }
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) AbstractFile(org.structr.web.entity.AbstractFile) 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) LinkedList(java.util.LinkedList)

Aggregations

AbstractFile (org.structr.web.entity.AbstractFile)36 Folder (org.structr.web.entity.Folder)24 Tx (org.structr.core.graph.Tx)17 FrameworkException (org.structr.common.error.FrameworkException)16 App (org.structr.core.app.App)14 StructrApp (org.structr.core.app.StructrApp)14 File (org.structr.web.entity.File)10 PropertyMap (org.structr.core.property.PropertyMap)8 LinkedList (java.util.LinkedList)6 CMISRootFolder (org.structr.files.cmis.repository.CMISRootFolder)4 Path (java.nio.file.Path)3 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)3 PropertyKey (org.structr.core.property.PropertyKey)3 Image (org.structr.web.entity.Image)3 Map (java.util.Map)2 ObjectData (org.apache.chemistry.opencmis.commons.data.ObjectData)2 GraphObject (org.structr.core.GraphObject)2 AbstractNode (org.structr.core.entity.AbstractNode)2 NodeAttribute (org.structr.core.graph.NodeAttribute)2 FileNotFoundException (java.io.FileNotFoundException)1