Search in sources :

Example 91 with Folder

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

the class DeployCommand method exportFilesAndFolders.

private void exportFilesAndFolders(final Path target, final Folder folder, final Map<String, Object> config) throws IOException {
    // ignore folders with mounted content
    if (folder.isMounted()) {
        return;
    }
    final String name = folder.getName();
    final Path path = target.resolve(name);
    // types and those with relationships to user data.
    if (DeployCommand.okToExport(folder)) {
        final Map<String, Object> properties = new TreeMap<>();
        Files.createDirectories(path);
        exportFileConfiguration(folder, properties);
        if (!properties.isEmpty()) {
            config.put(folder.getPath(), properties);
        }
    }
    final List<Folder> folders = Iterables.toList(folder.getFolders());
    Collections.sort(folders, new GraphObjectComparator(AbstractNode.name, false));
    for (final Folder child : folders) {
        exportFilesAndFolders(path, child, config);
    }
    final List<File> files = Iterables.toList(folder.getFiles());
    Collections.sort(files, new GraphObjectComparator(AbstractNode.name, false));
    for (final File file : files) {
        exportFile(path, file, config);
    }
}
Also used : Path(java.nio.file.Path) GraphObjectComparator(org.structr.common.GraphObjectComparator) TreeMap(java.util.TreeMap) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) AbstractMinifiedFile(org.structr.web.entity.AbstractMinifiedFile) MinifiedCssFile(org.structr.web.entity.MinifiedCssFile) File(org.structr.web.entity.File) MinifiedJavaScriptFile(org.structr.web.entity.MinifiedJavaScriptFile)

Example 92 with Folder

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

the class FileImportVisitor method createFile.

private void createFile(final Path path, final String fileName) throws IOException {
    String newFileUuid = null;
    try (final Tx tx = app.tx(true, false, false)) {
        final String fullPath = "/" + basePath.relativize(path).toString();
        final PropertyMap fileProperties = getPropertiesForFileOrFolder(fullPath);
        if (fileProperties == null) {
            if (!fileName.startsWith(".")) {
                logger.info("Ignoring {} (not in files.json)", fullPath);
            }
        } else {
            Folder parent = null;
            if (!basePath.equals(path.getParent())) {
                final String parentPath = "/" + basePath.relativize(path.getParent()).toString();
                parent = getExistingFolder(parentPath);
            }
            boolean skipFile = false;
            File file = app.nodeQuery(File.class).and(StructrApp.key(File.class, "parent"), parent).and(File.name, fileName).getFirst();
            if (file != null) {
                final Long checksumOfExistingFile = file.getChecksum();
                final Long checksumOfNewFile = FileHelper.getChecksum(path.toFile());
                if (checksumOfExistingFile != null && checksumOfNewFile != null && checksumOfExistingFile.equals(checksumOfNewFile)) {
                    skipFile = true;
                } else {
                    // remove existing file first!
                    app.delete(file);
                }
            }
            if (!skipFile) {
                logger.info("Importing {}...", fullPath);
                try (final FileInputStream fis = new FileInputStream(path.toFile())) {
                    // create file in folder structure
                    file = FileHelper.createFile(securityContext, fis, null, File.class, fileName, parent);
                    final String contentType = file.getContentType();
                    // modify file type according to content
                    if (StringUtils.startsWith(contentType, "image") || ImageHelper.isImageType(file.getProperty(name))) {
                        file.unlockSystemPropertiesOnce();
                        file.setProperties(securityContext, new PropertyMap(NodeInterface.type, Image.class.getSimpleName()));
                    }
                    newFileUuid = file.getUuid();
                }
            }
            if (file != null) {
                if (fileProperties.containsKey(StructrApp.key(AbstractMinifiedFile.class, "minificationSources"))) {
                    deferredFiles.add(file);
                } else {
                    file.unlockSystemPropertiesOnce();
                    file.setProperties(securityContext, fileProperties);
                }
            }
            if (newFileUuid != null) {
                final File createdFile = app.get(File.class, newFileUuid);
                String type = createdFile.getType();
                boolean isImage = createdFile instanceof Image;
                logger.debug("File {}: {}, isImage? {}", new Object[] { createdFile.getName(), type, isImage });
                if (isImage) {
                    try {
                        ImageHelper.updateMetadata(createdFile);
                        handleThumbnails((Image) createdFile);
                    } catch (Throwable t) {
                        logger.warn("Unable to update metadata: {}", t.getMessage());
                    }
                }
            }
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("Error occured while reading file properties " + fileName, ex);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractMinifiedFile(org.structr.web.entity.AbstractMinifiedFile) Folder(org.structr.web.entity.Folder) Image(org.structr.web.entity.Image) AbstractFile(org.structr.web.entity.AbstractFile) AbstractMinifiedFile(org.structr.web.entity.AbstractMinifiedFile) File(org.structr.web.entity.File) FileInputStream(java.io.FileInputStream)

Example 93 with Folder

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

the class FileImportVisitor method createFolder.

private void createFolder(final Path folderObj) {
    final String folderPath = "/" + basePath.relativize(folderObj).toString();
    try (final Tx tx = app.tx(true, false, false)) {
        if (getExistingFolder(folderPath) == null) {
            final PropertyMap folderProperties = new PropertyMap(AbstractNode.name, folderObj.getFileName().toString());
            if (!basePath.equals(folderObj.getParent())) {
                final String parentPath = "/" + basePath.relativize(folderObj.getParent()).toString();
                folderProperties.put(StructrApp.key(Folder.class, "parent"), getExistingFolder(parentPath));
            }
            // set properties from files.json
            final PropertyMap properties = getPropertiesForFileOrFolder(folderPath);
            if (properties != null) {
                folderProperties.putAll(properties);
            }
            final Folder newFolder = app.create(Folder.class, folderProperties);
            this.folderCache.put(folderPath, newFolder);
        }
        tx.success();
    } catch (Exception ex) {
        logger.error("Error occured while importing folder " + folderObj, ex);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) Folder(org.structr.web.entity.Folder) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException)

Example 94 with Folder

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

the class UploadServlet method doPost.

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
    try {
        if (!ServletFileUpload.isMultipartContent(request)) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getOutputStream().write("ERROR (400): Request does not contain multipart content.\n".getBytes("UTF-8"));
            return;
        }
    } catch (IOException ioex) {
        logger.warn("Unable to send response", ioex);
    }
    SecurityContext securityContext = null;
    String redirectUrl = null;
    boolean appendUuidOnRedirect = false;
    String path = null;
    // isolate request authentication in a transaction
    try (final Tx tx = StructrApp.getInstance().tx()) {
        try {
            securityContext = getConfig().getAuthenticator().initializeAndExamineRequest(request, response);
        } catch (AuthenticationException ae) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getOutputStream().write("ERROR (401): Invalid user or password.\n".getBytes("UTF-8"));
            return;
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("Unable to examine request", fex);
    } catch (IOException ioex) {
        logger.warn("Unable to send response", ioex);
    }
    // something went wrong, but we don't know what...
    if (securityContext == null) {
        logger.warn("No SecurityContext, aborting.");
        return;
    }
    try {
        if (securityContext.getUser(false) == null && !Settings.UploadAllowAnonymous.getValue()) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getOutputStream().write("ERROR (401): Anonymous uploads forbidden.\n".getBytes("UTF-8"));
            return;
        }
        // Ensure access mode is frontend
        securityContext.setAccessMode(AccessMode.Frontend);
        request.setCharacterEncoding("UTF-8");
        // Important: Set character encoding before calling response.getWriter() !!, see Servlet Spec 5.4
        response.setCharacterEncoding("UTF-8");
        // don't continue on redirects
        if (response.getStatus() == 302) {
            return;
        }
        final String pathInfo = request.getPathInfo();
        String type = null;
        if (StringUtils.isNotBlank(pathInfo)) {
            type = SchemaHelper.normalizeEntityName(StringUtils.stripStart(pathInfo.trim(), "/"));
        }
        uploader.setFileSizeMax((long) MEGABYTE * Settings.UploadMaxFileSize.getValue());
        uploader.setSizeMax((long) MEGABYTE * Settings.UploadMaxRequestSize.getValue());
        response.setContentType("text/html");
        FileItemIterator fileItemsIterator = uploader.getItemIterator(request);
        final Map<String, Object> params = new HashMap<>();
        while (fileItemsIterator.hasNext()) {
            final FileItemStream item = fileItemsIterator.next();
            if (item.isFormField()) {
                final String fieldName = item.getFieldName();
                final String fieldValue = IOUtils.toString(item.openStream(), "UTF-8");
                if (REDIRECT_AFTER_UPLOAD_PARAMETER.equals(fieldName)) {
                    redirectUrl = fieldValue;
                } else if (APPEND_UUID_ON_REDIRECT_PARAMETER.equals(fieldName)) {
                    appendUuidOnRedirect = "true".equalsIgnoreCase(fieldValue);
                } else if (UPLOAD_FOLDER_PATH_PARAMETER.equals(fieldName)) {
                    path = fieldValue;
                } else {
                    params.put(fieldName, fieldValue);
                }
            } else {
                try {
                    final String contentType = item.getContentType();
                    boolean isImage = (contentType != null && contentType.startsWith("image"));
                    boolean isVideo = (contentType != null && contentType.startsWith("video"));
                    // Override type from path info
                    if (params.containsKey(NodeInterface.type.jsonName())) {
                        type = (String) params.get(NodeInterface.type.jsonName());
                    }
                    Class cls = null;
                    if (type != null) {
                        cls = SchemaHelper.getEntityClassForRawType(type);
                    }
                    if (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;
                        }
                    }
                    if (cls != null) {
                        type = cls.getSimpleName();
                    }
                    final String name = item.getName().replaceAll("\\\\", "/");
                    File newFile = null;
                    String uuid = null;
                    boolean retry = true;
                    while (retry) {
                        retry = false;
                        Folder uploadFolder = null;
                        final String defaultUploadFolderConfigValue = Settings.DefaultUploadFolder.getValue();
                        // If a path attribute was sent, create all folders on the fly.
                        if (path != null) {
                            uploadFolder = getOrCreateFolderPath(securityContext, path);
                        } else if (StringUtils.isNotBlank(defaultUploadFolderConfigValue)) {
                            uploadFolder = getOrCreateFolderPath(SecurityContext.getSuperUserInstance(), defaultUploadFolderConfigValue);
                        }
                        try (final Tx tx = StructrApp.getInstance(securityContext).tx()) {
                            try (final InputStream is = item.openStream()) {
                                newFile = FileHelper.createFile(securityContext, is, contentType, cls, name, uploadFolder);
                                AbstractFile.validateAndRenameFileOnce(newFile, securityContext, null);
                                final PropertyMap changedProperties = new PropertyMap();
                                changedProperties.putAll(PropertyMap.inputTypeToJavaType(securityContext, cls, params));
                                // Update type as it could have changed
                                changedProperties.put(AbstractNode.type, type);
                                newFile.unlockSystemPropertiesOnce();
                                newFile.setProperties(securityContext, changedProperties);
                                uuid = newFile.getUuid();
                            }
                            tx.success();
                        } catch (RetryException rex) {
                            retry = true;
                        }
                    }
                    // only the actual existing file creates a UUID output
                    if (newFile != null) {
                        // upload trigger
                        newFile.notifyUploadCompletion();
                        // send redirect to allow form-based file upload without JavaScript..
                        if (StringUtils.isNotBlank(redirectUrl)) {
                            if (appendUuidOnRedirect) {
                                response.sendRedirect(redirectUrl + uuid);
                            } else {
                                response.sendRedirect(redirectUrl);
                            }
                        } else {
                            // Just write out the uuids of the new files
                            response.getWriter().write(uuid);
                        }
                    }
                } catch (IOException ex) {
                    logger.warn("Could not upload file", ex);
                }
            }
        }
    } catch (Throwable t) {
        final String content;
        if (t instanceof FrameworkException) {
            final FrameworkException fex = (FrameworkException) t;
            logger.error(fex.toString());
            content = errorPage(fex);
        } else {
            logger.error("Exception while processing upload request", t);
            content = errorPage(t);
        }
        try {
            final ServletOutputStream out = response.getOutputStream();
            IOUtils.write(content, out);
        } catch (IOException ex) {
            logger.error("Could not write to response", ex);
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AuthenticationException(org.structr.core.auth.exception.AuthenticationException) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) RetryException(org.structr.api.RetryException) PropertyMap(org.structr.core.property.PropertyMap) FileItemStream(org.apache.commons.fileupload.FileItemStream) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File)

Example 95 with Folder

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

the class UploadServlet method getOrCreateFolderPath.

private synchronized Folder getOrCreateFolderPath(SecurityContext securityContext, String path) {
    try (final Tx tx = StructrApp.getInstance().tx()) {
        Folder folder = FileHelper.createFolderPath(securityContext, path);
        tx.success();
        return folder;
    } catch (FrameworkException ex) {
        logger.warn("", ex);
    }
    return null;
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) 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