Search in sources :

Example 1 with StructrFileChannel

use of org.structr.files.ssh.filesystem.StructrFileChannel 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

FileNotFoundException (java.io.FileNotFoundException)1 FileChannel (java.nio.channels.FileChannel)1 FrameworkException (org.structr.common.error.FrameworkException)1 Tx (org.structr.core.graph.Tx)1 StructrFileChannel (org.structr.files.ssh.filesystem.StructrFileChannel)1 AbstractFile (org.structr.web.entity.AbstractFile)1 File (org.structr.web.entity.File)1