Search in sources :

Example 16 with AbstractFile

use of org.structr.web.entity.AbstractFile 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)

Example 17 with AbstractFile

use of org.structr.web.entity.AbstractFile 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 18 with AbstractFile

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

the class StructrFilePath method getActualFile.

public AbstractFile getActualFile() {
    if (cachedActualFile == null) {
        final String filePath = toString().substring(StructrPath.FILES_DIRECTORY.length() + 1);
        final App app = StructrApp.getInstance(fs.getSecurityContext());
        try (final Tx tx = app.tx()) {
            // remove /files from path since it is a virtual directory
            cachedActualFile = app.nodeQuery(AbstractFile.class).and(StructrApp.key(AbstractFile.class, "path"), filePath).getFirst();
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("Unable to load actual file for path {}: {}", new Object[] { toString(), fex.getMessage() });
        }
    }
    return cachedActualFile;
}
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)

Example 19 with AbstractFile

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

the class StructrFilePath method move.

@Override
public void move(final Path target, final CopyOption... options) throws IOException {
    if (target instanceof StructrFilePath) {
        final App app = StructrApp.getInstance(fs.getSecurityContext());
        final StructrFilePath other = (StructrFilePath) target;
        final AbstractFile otherFile = other.getActualFile();
        final AbstractFile thisFile = getActualFile();
        final String targetName = target.getFileName().toString();
        try (final Tx tx = app.tx()) {
            final Path otherParent = other.getParent();
            if (otherParent instanceof StructrFilesPath) {
                // rename & move (parent is null: root path)
                thisFile.setParent(null);
                thisFile.setProperty(AbstractNode.name, targetName);
                // this is a move operation, delete existing file
                if (otherFile != null) {
                    app.delete(otherFile);
                }
            } else {
                final StructrFilePath parent = (StructrFilePath) other.getParent();
                final Folder newParentFolder = (Folder) parent.getActualFile();
                // rename & move
                thisFile.setParent(newParentFolder);
                thisFile.setProperty(AbstractNode.name, targetName);
                // this is a move operation, delete existing file
                if (otherFile != null) {
                    app.delete(otherFile);
                }
            }
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("", fex);
        }
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) StructrPath(org.structr.files.ssh.filesystem.StructrPath) Path(java.nio.file.Path) 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 20 with AbstractFile

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

the class StructrFilePath method newFileChannel.

@Override
public FileChannel newFileChannel(final Set<? extends OpenOption> options, final FileAttribute<?>... attrs) throws IOException {
    AbstractFile actualFile = getActualFile();
    FileChannel channel = null;
    final boolean create = options.contains(StandardOpenOption.CREATE);
    final boolean createNew = options.contains(StandardOpenOption.CREATE_NEW);
    final boolean write = options.contains(StandardOpenOption.WRITE);
    final boolean truncate = options.contains(StandardOpenOption.TRUNCATE_EXISTING);
    final boolean append = options.contains(StandardOpenOption.APPEND);
    if (write) {
        try (final Tx tx = StructrApp.getInstance(fs.getSecurityContext()).tx()) {
            // creation of a new file requested (=> create a new schema method)
            if (create || createNew) {
                // if CREATE_NEW, file must not exist, otherwise an error should be thrown
                if (createNew && actualFile != null) {
                    throw new java.nio.file.FileAlreadyExistsException(toString());
                }
                // only create new file when it does not already exist
                if (actualFile == null) {
                    try {
                        actualFile = createNewFile();
                        setParentFolder(actualFile);
                    } catch (FrameworkException fex) {
                        logger.warn("", fex);
                    }
                }
            }
            if (actualFile != null && actualFile instanceof File) {
                final File file = (File) actualFile;
                channel = new StructrFileChannel(file.getOutputStream(true, !truncate || append));
            }
            tx.success();
        } catch (FrameworkException fex) {
            logger.warn("Unable to open file channel for writing of {}: {}", new Object[] { actualFile.getPath(), fex.getMessage() });
        }
    } else {
        if (actualFile != null && actualFile instanceof File) {
            try (final Tx tx = StructrApp.getInstance(fs.getSecurityContext()).tx()) {
                channel = FileChannel.open(((File) actualFile).getFileOnDisk().toPath(), options);
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("Unable to open file channel for reading of {}: {}", new Object[] { actualFile.getPath(), fex.getMessage() });
            }
        } else {
            throw new FileNotFoundException("File " + actualFile.getPath() + " does not exist.");
        }
    }
    return channel;
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StructrFileChannel(org.structr.files.ssh.filesystem.StructrFileChannel) FileChannel(java.nio.channels.FileChannel) StructrFileChannel(org.structr.files.ssh.filesystem.StructrFileChannel) FileNotFoundException(java.io.FileNotFoundException) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

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