Search in sources :

Example 21 with Folder

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

the class DirectFileImportCommand method createFileOrFolder.

private FileVisitResult createFileOrFolder(final SecurityContext ctx, final App app, final Path path, final Path file, final BasicFileAttributes attrs, final String sourcePath, final String targetPath, final Mode mode, final Existing existing, final boolean doIndex) {
    ctx.setDoTransactionNotifications(false);
    final String name = file.getFileName().toString();
    try (final Tx tx = app.tx()) {
        final String relativePath = PathHelper.getRelativeNodePath(path.toString(), file.toString());
        String parentPath = targetPath + PathHelper.getFolderPath(relativePath);
        // fix broken path concatenation
        if (parentPath.startsWith("//")) {
            parentPath = parentPath.substring(1);
        }
        if (attrs.isDirectory()) {
            final Folder newFolder = app.create(Folder.class, new NodeAttribute(Folder.name, name), new NodeAttribute(StructrApp.key(File.class, "parent"), FileHelper.createFolderPath(securityContext, parentPath)));
            folderCount++;
            logger.info("Created folder " + newFolder.getPath());
        } else if (attrs.isRegularFile()) {
            final File existingFile = app.nodeQuery(File.class).and(StructrApp.key(AbstractFile.class, "path"), parentPath + name).getFirst();
            if (existingFile != null) {
                switch(existing) {
                    case SKIP:
                        logger.info("Skipping import of {}, file exists and mode is SKIP.", parentPath + name);
                        return FileVisitResult.CONTINUE;
                    case OVERWRITE:
                        logger.info("Overwriting {}, file exists and mode is OVERWRITE.", parentPath + name);
                        app.delete(existingFile);
                        break;
                    case RENAME:
                        logger.info("Renaming existing file {}, file exists and mode is RENAME.", parentPath + name);
                        existingFile.setProperty(AbstractFile.name, existingFile.getProperty(AbstractFile.name).concat("_").concat(FileHelper.getDateString()));
                        break;
                }
            }
            final String contentType = FileHelper.getContentMimeType(file.toFile(), file.getFileName().toString());
            boolean isImage = (contentType != null && contentType.startsWith("image"));
            boolean isVideo = (contentType != null && contentType.startsWith("video"));
            Class cls = null;
            if (isImage) {
                cls = Image.class;
            } else if (isVideo) {
                cls = SchemaHelper.getEntityClassForRawType("VideoFile");
                if (cls == null) {
                    logger.warn("Unable to create entity of type VideoFile, class is not defined.");
                }
            } else {
                cls = File.class;
            }
            final File newFile = (File) app.create(cls, new NodeAttribute(File.name, name), new NodeAttribute(StructrApp.key(File.class, "parent"), FileHelper.createFolderPath(securityContext, parentPath)), new NodeAttribute(AbstractNode.type, cls.getSimpleName()));
            final java.io.File fileOnDisk = newFile.getFileOnDisk(false);
            final Path fullFolderPath = fileOnDisk.toPath();
            Files.createDirectories(fullFolderPath.getParent());
            switch(mode) {
                case MOVE:
                    Files.move(file, fullFolderPath);
                    break;
                case COPY:
                    Files.copy(file, fullFolderPath);
                    break;
            }
            FileHelper.updateMetadata(newFile);
            if (doIndex) {
                indexer.addToFulltextIndex(newFile);
            }
            fileCount++;
        }
        tx.success();
    } catch (IOException | FrameworkException ex) {
        logger.debug("File: " + name + ", path: " + sourcePath, ex);
    }
    return FileVisitResult.CONTINUE;
}
Also used : Path(java.nio.file.Path) NodeAttribute(org.structr.core.graph.NodeAttribute) AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) Image(org.structr.web.entity.Image) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 22 with Folder

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

the class DirectFileImportCommand method execute.

@Override
public void execute(final Map<String, Object> attributes) throws FrameworkException {
    indexer = StructrApp.getInstance(securityContext).getFulltextIndexer();
    final String sourcePath = getParameterValueAsString(attributes, "source", null);
    final String modeString = getParameterValueAsString(attributes, "mode", Mode.COPY.name()).toUpperCase();
    final String existingString = getParameterValueAsString(attributes, "existing", Existing.SKIP.name()).toUpperCase();
    final boolean doIndex = Boolean.parseBoolean(getParameterValueAsString(attributes, "index", Boolean.TRUE.toString()));
    if (StringUtils.isBlank(sourcePath)) {
        throw new FrameworkException(422, "Please provide 'source' attribute for deployment source directory path.");
    }
    if (!EnumUtils.isValidEnum(Mode.class, modeString)) {
        throw new FrameworkException(422, "Unknown value for 'mode' attribute. Valid values are: copy, move");
    }
    if (!EnumUtils.isValidEnum(Existing.class, existingString)) {
        throw new FrameworkException(422, "Unknown value for 'existing' attribute. Valid values are: skip, overwrite, rename");
    }
    // use actual enums
    final Existing existing = Existing.valueOf(existingString);
    final Mode mode = Mode.valueOf(modeString);
    final List<Path> paths = new ArrayList<>();
    if (sourcePath.contains(PathHelper.PATH_SEP)) {
        final String folderPart = PathHelper.getFolderPath(sourcePath);
        final String namePart = PathHelper.getName(sourcePath);
        if (StringUtils.isNotBlank(folderPart)) {
            final Path source = Paths.get(folderPart);
            if (!Files.exists(source)) {
                throw new FrameworkException(422, "Source path " + sourcePath + " does not exist.");
            }
            if (!Files.isDirectory(source)) {
                throw new FrameworkException(422, "Source path " + sourcePath + " is not a directory.");
            }
            try {
                try (final DirectoryStream<Path> stream = Files.newDirectoryStream(source, namePart)) {
                    for (final Path entry : stream) {
                        paths.add(entry);
                    }
                } catch (final DirectoryIteratorException ex) {
                    throw ex.getCause();
                }
            } catch (final IOException ioex) {
                throw new FrameworkException(422, "Unable to parse source path " + sourcePath + ".");
            }
        }
    } else {
        // Relative path
        final Path source = Paths.get(Settings.BasePath.getValue()).resolve(sourcePath);
        if (!Files.exists(source)) {
            throw new FrameworkException(422, "Source path " + sourcePath + " does not exist.");
        }
        paths.add(source);
    }
    final SecurityContext ctx = SecurityContext.getSuperUserInstance();
    final App app = StructrApp.getInstance(ctx);
    String targetPath = getParameterValueAsString(attributes, "target", "/");
    Folder targetFolder = null;
    ctx.setDoTransactionNotifications(false);
    if (StringUtils.isNotBlank(targetPath) && !("/".equals(targetPath))) {
        try (final Tx tx = app.tx()) {
            targetFolder = app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), targetPath).getFirst();
            if (targetFolder == null) {
                throw new FrameworkException(422, "Target path " + targetPath + " does not exist.");
            }
            tx.success();
        }
    }
    String msg = "Starting direct file import from source directory " + sourcePath + " into target path " + targetPath;
    logger.info(msg);
    publishProgressMessage(msg);
    paths.forEach((path) -> {
        try {
            final String newTargetPath;
            // If path is a directory, create it and use it as the new target folder
            if (Files.isDirectory(path)) {
                Path parentPath = path.getParent();
                if (parentPath == null) {
                    parentPath = path;
                }
                createFileOrFolder(ctx, app, parentPath, path, Files.readAttributes(path, BasicFileAttributes.class), sourcePath, targetPath, mode, existing, doIndex);
                newTargetPath = targetPath + PathHelper.PATH_SEP + PathHelper.clean(path.getFileName().toString());
            } else {
                newTargetPath = targetPath;
            }
            Files.walkFileTree(path, new FileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                    return createFileOrFolder(ctx, app, path, file, attrs, sourcePath, newTargetPath, mode, existing, doIndex);
                }

                @Override
                public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (final IOException ex) {
            logger.debug("Mode: " + modeString + ", path: " + sourcePath, ex);
        }
    });
    msg = "Finished direct file import from source directory " + sourcePath + ". Imported " + folderCount + " folders and " + fileCount + " files.";
    logger.info(msg);
    publishProgressMessage(msg);
}
Also used : Path(java.nio.file.Path) DirectoryIteratorException(java.nio.file.DirectoryIteratorException) StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) Tx(org.structr.core.graph.Tx) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) SecurityContext(org.structr.common.SecurityContext) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 23 with Folder

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

the class CreateArchiveFunction method addFoldersToArchive.

private void addFoldersToArchive(String path, Iterable<Folder> list, ArchiveOutputStream aps) throws IOException {
    for (Folder folder : list) {
        addFilesToArchive(path + folder.getProperty(Folder.name) + "/", folder.getFiles(), aps);
        addFoldersToArchive(path + folder.getProperty(Folder.name) + "/", folder.getFolders(), aps);
    }
}
Also used : Folder(org.structr.web.entity.Folder)

Example 24 with Folder

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

the class SSHTest method createFTPFile.

protected File createFTPFile(final String path, final String name) throws FrameworkException {
    PropertyMap props = new PropertyMap();
    props.put(StructrApp.key(File.class, "name"), name);
    props.put(StructrApp.key(File.class, "size"), 0L);
    props.put(StructrApp.key(File.class, "owner"), ftpUser);
    File file = (File) createTestNodes(File.class, 1, props).get(0);
    if (StringUtils.isNotBlank(path)) {
        AbstractFile parent = FileHelper.getFileByAbsolutePath(securityContext, path);
        if (parent != null && parent instanceof Folder) {
            Folder parentFolder = (Folder) parent;
            file.setParent(parentFolder);
        }
    }
    logger.info("FTP file {} created successfully.", file);
    return file;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) AbstractFile(org.structr.web.entity.AbstractFile) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 25 with Folder

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

the class SSHTest method createFTPDirectory.

protected Folder createFTPDirectory(final String path, final String name) throws FrameworkException {
    PropertyMap props = new PropertyMap();
    props.put(Folder.name, name);
    props.put(Folder.owner, ftpUser);
    Folder dir = (Folder) createTestNodes(Folder.class, 1, props).get(0);
    if (StringUtils.isNotBlank(path)) {
        AbstractFile parent = FileHelper.getFileByAbsolutePath(securityContext, path);
        if (parent != null && parent instanceof Folder) {
            Folder parentFolder = (Folder) parent;
            dir.setParent(parentFolder);
        }
    }
    logger.info("FTP directory {} created successfully.", dir);
    return dir;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) AbstractFile(org.structr.web.entity.AbstractFile) Folder(org.structr.web.entity.Folder)

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