Search in sources :

Example 21 with Versionable

use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.

the class CourseResourceFolderWebService method attachFileToCourseFolder.

private Response attachFileToCourseFolder(Long courseId, List<PathSegment> path, String filename, InputStream file, HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    ICourse course = CoursesWebService.loadCourse(courseId);
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    VFSContainer container = course.getCourseFolderContainer();
    for (PathSegment segment : path) {
        VFSItem item = container.resolve(segment.getPath());
        if (item instanceof VFSContainer) {
            container = (VFSContainer) item;
        } else if (item == null) {
            // create the folder
            container = container.createChildContainer(segment.getPath());
        }
    }
    VFSItem newFile;
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    if (container.resolve(filename) != null) {
        VFSItem existingVFSItem = container.resolve(filename);
        if (existingVFSItem instanceof VFSContainer) {
            // already exists
            return Response.ok().build();
        }
        // check if it's locked
        boolean locked = CoreSpringFactory.getImpl(VFSLockManager.class).isLockedForMe(existingVFSItem, ureq.getIdentity(), ureq.getUserSession().getRoles());
        if (locked) {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
        if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
            Versionable existingVersionableItem = (Versionable) existingVFSItem;
            boolean ok = existingVersionableItem.getVersions().addVersion(ureq.getIdentity(), "REST upload", file);
            if (ok) {
                log.audit("");
            }
            newFile = (VFSLeaf) existingVersionableItem;
        } else {
            existingVFSItem.delete();
            newFile = container.createChildLeaf(filename);
            OutputStream out = ((VFSLeaf) newFile).getOutputStream(false);
            FileUtils.copy(file, out);
            FileUtils.closeSafely(out);
            FileUtils.closeSafely(file);
        }
    } else if (file != null) {
        newFile = container.createChildLeaf(filename);
        OutputStream out = ((VFSLeaf) newFile).getOutputStream(false);
        FileUtils.copy(file, out);
        FileUtils.closeSafely(out);
        FileUtils.closeSafely(file);
    } else {
        newFile = container.createChildContainer(filename);
    }
    if (newFile instanceof MetaTagged && ((MetaTagged) newFile).getMetaInfo() != null) {
        MetaInfo infos = ((MetaTagged) newFile).getMetaInfo();
        infos.setAuthor(ureq.getIdentity());
        infos.write();
    }
    return Response.ok().build();
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) OutputStream(java.io.OutputStream) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) VFSItem(org.olat.core.util.vfs.VFSItem) ICourse(org.olat.course.ICourse) PathSegment(javax.ws.rs.core.PathSegment) UserRequest(org.olat.core.gui.UserRequest) VFSLockManager(org.olat.core.util.vfs.VFSLockManager)

Example 22 with Versionable

use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.

the class ZipUtil method unzip.

/**
 * Unzip an inputstream to a directory using the versioning system of VFS
 * @param zipLeaf	The file to unzip
 * @param targetDir	The directory to unzip the file to
 * @param the identity of who unzip the file
 * @param versioning enabled or not
 * @return	True if successfull, false otherwise
 */
private static boolean unzip(InputStream in, VFSContainer targetDir, Identity identity, boolean versioning) {
    ZipInputStream oZip = new ZipInputStream(in);
    try {
        // unzip files
        ZipEntry oEntr = oZip.getNextEntry();
        while (oEntr != null) {
            if (oEntr.getName() != null && !oEntr.getName().startsWith(DIR_NAME__MACOSX)) {
                if (oEntr.isDirectory()) {
                    // skip MacOSX specific metadata directory
                    // create directories
                    getAllSubdirs(targetDir, oEntr.getName(), identity, true);
                } else {
                    // create file
                    VFSContainer createIn = targetDir;
                    String name = oEntr.getName();
                    // check if entry has directories which did not show up as
                    // directories above
                    int dirSepIndex = name.lastIndexOf('/');
                    if (dirSepIndex == -1) {
                        // try it windows style, backslash is also valid format
                        dirSepIndex = name.lastIndexOf('\\');
                    }
                    if (dirSepIndex > 0) {
                        // create subdirs
                        createIn = getAllSubdirs(targetDir, name.substring(0, dirSepIndex), identity, true);
                        if (createIn == null) {
                            if (log.isDebug())
                                log.debug("Error creating directory structure for zip entry: " + oEntr.getName());
                            return false;
                        }
                        name = name.substring(dirSepIndex + 1);
                    }
                    if (versioning) {
                        VFSLeaf newEntry = (VFSLeaf) createIn.resolve(name);
                        if (newEntry == null) {
                            newEntry = createIn.createChildLeaf(name);
                            OutputStream out = newEntry.getOutputStream(false);
                            if (!FileUtils.copy(oZip, out))
                                return false;
                            FileUtils.closeSafely(out);
                        } else if (newEntry instanceof Versionable) {
                            Versionable versionable = (Versionable) newEntry;
                            if (versionable.getVersions().isVersioned()) {
                                versionable.getVersions().addVersion(identity, "", oZip);
                            }
                        }
                        if (newEntry instanceof MetaTagged) {
                            MetaInfo info = ((MetaTagged) newEntry).getMetaInfo();
                            if (info != null) {
                                info.setAuthor(identity);
                                info.write();
                            }
                        }
                    } else {
                        VFSLeaf newEntry = createIn.createChildLeaf(name);
                        if (newEntry != null) {
                            OutputStream out = newEntry.getOutputStream(false);
                            if (!FileUtils.copy(oZip, out))
                                return false;
                            FileUtils.closeSafely(out);
                        }
                        if (newEntry instanceof MetaTagged) {
                            MetaInfo info = ((MetaTagged) newEntry).getMetaInfo();
                            if (info != null && identity != null) {
                                info.setAuthor(identity);
                                info.write();
                            }
                        }
                    }
                }
            }
            oZip.closeEntry();
            oEntr = oZip.getNextEntry();
        }
    } catch (IOException e) {
        return false;
    } finally {
        FileUtils.closeSafely(oZip);
    }
    return true;
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) VFSContainer(org.olat.core.util.vfs.VFSContainer) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) IOException(java.io.IOException)

Example 23 with Versionable

use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.

the class ZipUtil method unzipNonStrict.

/**
 * Unzip with jazzlib
 * @param in
 * @param targetDir
 * @param identity
 * @param versioning
 * @return
 */
private static boolean unzipNonStrict(InputStream in, VFSContainer targetDir, Identity identity, boolean versioning) {
    net.sf.jazzlib.ZipInputStream oZip = new net.sf.jazzlib.ZipInputStream(in);
    try {
        // unzip files
        net.sf.jazzlib.ZipEntry oEntr = oZip.getNextEntry();
        while (oEntr != null) {
            if (oEntr.getName() != null && !oEntr.getName().startsWith(DIR_NAME__MACOSX)) {
                if (oEntr.isDirectory()) {
                    // skip MacOSX specific metadata directory
                    // create directories
                    getAllSubdirs(targetDir, oEntr.getName(), identity, true);
                } else {
                    // create file
                    VFSContainer createIn = targetDir;
                    String name = oEntr.getName();
                    // check if entry has directories which did not show up as
                    // directories above
                    int dirSepIndex = name.lastIndexOf('/');
                    if (dirSepIndex == -1) {
                        // try it windows style, backslash is also valid format
                        dirSepIndex = name.lastIndexOf('\\');
                    }
                    if (dirSepIndex > 0) {
                        // create subdirs
                        createIn = getAllSubdirs(targetDir, name.substring(0, dirSepIndex), identity, true);
                        if (createIn == null) {
                            if (log.isDebug())
                                log.debug("Error creating directory structure for zip entry: " + oEntr.getName());
                            return false;
                        }
                        name = name.substring(dirSepIndex + 1);
                    }
                    if (versioning) {
                        VFSLeaf newEntry = (VFSLeaf) createIn.resolve(name);
                        if (newEntry == null) {
                            newEntry = createIn.createChildLeaf(name);
                            OutputStream out = newEntry.getOutputStream(false);
                            if (!FileUtils.copy(oZip, out))
                                return false;
                            FileUtils.closeSafely(out);
                        } else if (newEntry instanceof Versionable) {
                            Versionable versionable = (Versionable) newEntry;
                            if (versionable.getVersions().isVersioned()) {
                                versionable.getVersions().addVersion(identity, "", oZip);
                            }
                        }
                        if (newEntry instanceof MetaTagged) {
                            MetaInfo info = ((MetaTagged) newEntry).getMetaInfo();
                            if (info != null) {
                                info.setAuthor(identity);
                                info.write();
                            }
                        }
                    } else {
                        VFSLeaf newEntry = createIn.createChildLeaf(name);
                        if (newEntry != null) {
                            OutputStream out = newEntry.getOutputStream(false);
                            if (!FileUtils.copy(oZip, out))
                                return false;
                            FileUtils.closeSafely(out);
                        }
                        if (newEntry instanceof MetaTagged) {
                            MetaInfo info = ((MetaTagged) newEntry).getMetaInfo();
                            if (info != null && identity != null) {
                                info.setAuthor(identity);
                                info.write();
                            }
                        }
                    }
                }
            }
            oZip.closeEntry();
            oEntr = oZip.getNextEntry();
        }
    } catch (IOException e) {
        return false;
    } finally {
        FileUtils.closeSafely(oZip);
    }
    return true;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) IOException(java.io.IOException) Versionable(org.olat.core.util.vfs.version.Versionable) ZipInputStream(java.util.zip.ZipInputStream)

Example 24 with Versionable

use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.

the class FileUploadController method doUpload.

private void doUpload(UserRequest ureq) {
    // check for available space
    if (remainingQuotKB != -1 && (fileEl.getUploadFile().length() / 1024 > remainingQuotKB)) {
        fileEl.setErrorKey("QuotaExceeded", null);
        fileEl.getUploadFile().delete();
        return;
    }
    String fileName = fileEl.getUploadFileName();
    if (metaDataCtr != null && StringHelper.containsNonWhitespace(metaDataCtr.getFilename())) {
        fileName = metaDataCtr.getFilename();
    }
    File uploadedFile = fileEl.getUploadFile();
    if (resizeImg && fileName != null && imageExtPattern.matcher(fileName.toLowerCase()).find() && resizeEl.isSelected(0)) {
        String extension = FileUtils.getFileSuffix(fileName);
        File imageScaled = new File(uploadedFile.getParentFile(), "scaled_" + uploadedFile.getName() + "." + extension);
        if (imageHelper.scaleImage(uploadedFile, extension, imageScaled, 1280, 1280, false) != null) {
            // problem happen, special GIF's (see bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6358674)
            // don't try to scale if not all ok
            uploadedFile = imageScaled;
        }
    }
    // check if such a filename does already exist
    existingVFSItem = uploadVFSContainer.resolve(fileName);
    if (existingVFSItem == null) {
        uploadNewFile(ureq, uploadedFile, fileName);
    } else {
        // rename file and ask user what to do
        if (!(existingVFSItem instanceof LocalImpl)) {
            throw new AssertException("Can only LocalImpl VFS items, don't know what to do with file of type::" + existingVFSItem.getClass().getCanonicalName());
        }
        String renamedFilename = VFSManager.rename(uploadVFSContainer, existingVFSItem.getName());
        newFile = uploadVFSContainer.createChildLeaf(renamedFilename);
        // Copy content to tmp file
        boolean success = false;
        try (InputStream in = new FileInputStream(uploadedFile);
            BufferedOutputStream out = new BufferedOutputStream(newFile.getOutputStream(false))) {
            success = FileUtils.copy(in, out);
            uploadedFile.delete();
        } catch (IOException e) {
            success = false;
        }
        if (success) {
            boolean locked = vfsLockManager.isLockedForMe(existingVFSItem, getIdentity(), ureq.getUserSession().getRoles());
            if (locked) {
                // the file is locked and cannot be overwritten
                lockedFileDialog(ureq, renamedFilename);
            } else if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
                uploadVersionedFile(ureq, renamedFilename);
            } else {
                askOverwriteOrRename(ureq, renamedFilename);
            }
        } else {
            showError("failed");
            status = FolderCommandStatus.STATUS_FAILED;
            fireEvent(ureq, Event.FAILED_EVENT);
        }
    }
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) AssertException(org.olat.core.logging.AssertException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) LocalImpl(org.olat.core.util.vfs.LocalImpl) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 25 with Versionable

use of org.olat.core.util.vfs.version.Versionable in project OpenOLAT by OpenOLAT.

the class FileUploadController method uploadVersionedFile.

private void uploadVersionedFile(UserRequest ureq, String renamedFilename) {
    Versionable versionable = (Versionable) existingVFSItem;
    Versions versions = versionable.getVersions();
    int maxNumOfRevisions = getMaxNumOfRevisionsOfExistingVFSItem();
    if (maxNumOfRevisions == 0) {
        // it's possible if someone change the configuration
        // let calling method decide what to do.
        askOverwriteOrRename(ureq, renamedFilename);
    } else if (versions.getRevisions().isEmpty() || maxNumOfRevisions < 0 || maxNumOfRevisions > versions.getRevisions().size()) {
        // let calling method decide what to do.
        askNewVersionOrRename(ureq, renamedFilename);
    } else {
        // too many revisions
        askToReduceRevisionList(ureq, versionable);
    }
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) Versions(org.olat.core.util.vfs.version.Versions)

Aggregations

Versionable (org.olat.core.util.vfs.version.Versionable)40 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)20 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)12 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)12 VFSContainer (org.olat.core.util.vfs.VFSContainer)12 VFSItem (org.olat.core.util.vfs.VFSItem)12 Versions (org.olat.core.util.vfs.version.Versions)10 IOException (java.io.IOException)8 BufferedOutputStream (java.io.BufferedOutputStream)6 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 RevisionListController (org.olat.core.commons.modules.bc.version.RevisionListController)6 AssertException (org.olat.core.logging.AssertException)6 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 Date (java.util.Date)4 ZipInputStream (java.util.zip.ZipInputStream)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 OlatRootFileImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl)4 SubscriptionContext (org.olat.core.commons.services.notifications.SubscriptionContext)4