Search in sources :

Example 11 with Versionable

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

the class CmdMoveCopy method doMove.

private void doMove(UserRequest ureq) {
    FolderTreeModel ftm = (FolderTreeModel) selTree.getTreeModel();
    String selectedPath = ftm.getSelectedPath(selTree.getSelectedNode());
    if (selectedPath == null) {
        abortFailed(ureq, "failed");
        return;
    }
    VFSStatus vfsStatus = VFSConstants.SUCCESS;
    VFSContainer rootContainer = folderComponent.getRootContainer();
    VFSItem vfsItem = rootContainer.resolve(selectedPath);
    if (vfsItem == null || (vfsItem.canWrite() != VFSConstants.YES)) {
        abortFailed(ureq, "failed");
        return;
    }
    // copy the files
    VFSContainer target = (VFSContainer) vfsItem;
    List<VFSItem> sources = getSanityCheckedSourceItems(target, ureq);
    if (sources == null)
        return;
    boolean targetIsRelPath = (target instanceof OlatRelPathImpl);
    for (VFSItem vfsSource : sources) {
        if (targetIsRelPath && (target instanceof OlatRelPathImpl) && (vfsSource instanceof MetaTagged)) {
            // copy the metainfo first
            MetaInfo meta = ((MetaTagged) vfsSource).getMetaInfo();
            if (meta != null) {
                meta.moveCopyToDir((OlatRelPathImpl) target, move);
            }
        }
        VFSItem targetFile = target.resolve(vfsSource.getName());
        if (vfsSource instanceof VFSLeaf && targetFile != null && targetFile instanceof Versionable && ((Versionable) targetFile).getVersions().isVersioned()) {
            // add a new version to the file
            ((Versionable) targetFile).getVersions().addVersion(null, "", ((VFSLeaf) vfsSource).getInputStream());
        } else {
            vfsStatus = target.copyFrom(vfsSource);
        }
        if (vfsStatus != VFSConstants.SUCCESS) {
            String errorKey = "failed";
            if (vfsStatus == VFSConstants.ERROR_QUOTA_EXCEEDED)
                errorKey = "QuotaExceeded";
            abortFailed(ureq, errorKey);
            return;
        }
        if (move) {
            // if move, delete the source. Note that meta source
            // has already been delete (i.e. moved)
            vfsSource.delete();
        }
    }
    // after a copy or a move, notify the subscribers
    VFSSecurityCallback secCallback = VFSManager.findInheritedSecurityCallback(folderComponent.getCurrentContainer());
    if (secCallback != null) {
        SubscriptionContext subsContext = secCallback.getSubscriptionContext();
        if (subsContext != null) {
            NotificationsManager.getInstance().markPublisherNews(subsContext, ureq.getIdentity(), true);
        }
    }
    fireEvent(ureq, new FolderEvent(move ? FolderEvent.MOVE_EVENT : FolderEvent.COPY_EVENT, fileSelection.renderAsHtml()));
    notifyFinished(ureq);
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) FolderTreeModel(org.olat.core.gui.control.generic.folder.FolderTreeModel) OlatRelPathImpl(org.olat.core.util.vfs.OlatRelPathImpl) VFSContainer(org.olat.core.util.vfs.VFSContainer) 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) Versionable(org.olat.core.util.vfs.version.Versionable) VFSStatus(org.olat.core.util.vfs.VFSStatus) FolderEvent(org.olat.core.commons.modules.bc.FolderEvent) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) VFSSecurityCallback(org.olat.core.util.vfs.callbacks.VFSSecurityCallback)

Example 12 with Versionable

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

the class CourseConfigManagerImpl method saveConfigTo.

/**
 * @see org.olat.course.config.CourseConfigManager#saveConfigTo(org.olat.course.ICourse,
 *      org.olat.course.config.CourseConfig)
 */
public void saveConfigTo(ICourse course, CourseConfig courseConfig) {
    VFSLeaf configFile = getConfigFile(course);
    if (configFile == null) {
        // create new config file
        configFile = course.getCourseBaseContainer().createChildLeaf(COURSECONFIG_XML);
    } else if (configFile.exists() && configFile instanceof Versionable) {
        try {
            VersionsFileManager.getInstance().addToRevisions((Versionable) configFile, null, "");
        } catch (Exception e) {
            log.error("Cannot versioned CourseConfig.xml", e);
        }
    }
    XStreamHelper.writeObject(configFile, courseConfig);
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) VFSLeaf(org.olat.core.util.vfs.VFSLeaf)

Example 13 with Versionable

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

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 14 with Versionable

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

the class HTMLEditorController method doSaveData.

/**
 * Event implementation for savedata
 *
 * @param ureq
 */
public boolean doSaveData() {
    // No XSS checks, are done in the HTML editor - users can upload illegal
    // stuff, JS needs to be enabled for users
    String content = htmlElement.getRawValue();
    // If preface was null -> append own head and save it in utf-8. Preface
    // is the header that was in the file when we opened the file
    StringBuilder fileContent = new StringBuilder();
    if (preface == null) {
        fileContent.append(DOCTYPE).append(OPEN_HTML).append(OPEN_HEAD);
        fileContent.append(GENERATOR_META).append(UTF8CHARSET);
        // In new documents, create empty title to be W3C conform. Title
        // is mandatory element in meta element.
        fileContent.append(EMTPY_TITLE);
        fileContent.append(CLOSE_HEAD_OPEN_BODY);
        fileContent.append(content);
        fileContent.append(CLOSE_BODY_HTML);
        // use utf-8 by default for new files
        charSet = UTF_8;
    } else {
        // existing preface, just reinsert so we don't lose stuff the user put
        // in there
        fileContent.append(preface).append(content).append(CLOSE_BODY_HTML);
    }
    int fileSize = fileContent.toString().getBytes().length;
    if (fileSize >= FolderConfig.getMaxEditSizeLimit()) {
        String msg = translate("file.too.large.server", new String[] { (fileSize / 1000) + "", (FolderConfig.getMaxEditSizeLimit() / 1000) + "" });
        getWindowControl().setError(msg);
        return false;
    }
    // save the file
    if (versionsEnabled && fileLeaf instanceof Versionable && ((Versionable) fileLeaf).getVersions().isVersioned()) {
        InputStream inStream = FileUtils.getInputStream(fileContent.toString(), charSet);
        ((Versionable) fileLeaf).getVersions().addVersion(getIdentity(), "", inStream);
    } else {
        FileUtils.save(fileLeaf.getOutputStream(false), fileContent.toString(), charSet);
    }
    // Update last modified date in view
    long lm = fileLeaf.getLastModified();
    metadataVC.contextPut("lastModified", Formatter.getInstance(getLocale()).formatDateAndTime(new Date(lm)));
    // Set new content as default value in element
    htmlElement.setNewOriginalValue(content);
    return true;
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) InputStream(java.io.InputStream) Date(java.util.Date)

Example 15 with Versionable

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

the class FileCopyController method fileAlreadyExists.

private void fileAlreadyExists(UserRequest ureq) {
    renamedFilename = proposedRenamedFilename(existingVFSItem);
    boolean locked = vfsLockManager.isLockedForMe(existingVFSItem, getIdentity(), ureq.getUserSession().getRoles());
    if (locked) {
        // the file is locked and cannot be overwritten
        removeAsListenerAndDispose(lockedFileDialog);
        lockedFileDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.lockedFile.title"), translate("ul.lockedFile.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
        listenTo(lockedFileDialog);
        lockedFileDialog.activate();
    } else if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
        Versionable versionable = (Versionable) existingVFSItem;
        Versions versions = versionable.getVersions();
        String relPath = null;
        if (existingVFSItem instanceof OlatRootFileImpl) {
            relPath = ((OlatRootFileImpl) existingVFSItem).getRelPath();
        }
        int maxNumOfRevisions = FolderConfig.versionsAllowed(relPath);
        if (maxNumOfRevisions == 0) {
            // it's possible if someone change the configuration
            // let calling method decide what to do.
            removeAsListenerAndDispose(overwriteDialog);
            overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.overwrite.threeoptions.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.overwrite"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
            listenTo(overwriteDialog);
            overwriteDialog.activate();
        } else if (versions.getRevisions().isEmpty() || maxNumOfRevisions < 0 || maxNumOfRevisions > versions.getRevisions().size()) {
            // let calling method decide what to do.
            removeAsListenerAndDispose(overwriteDialog);
            overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.versionoroverwrite", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.newVersion"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
            listenTo(overwriteDialog);
            overwriteDialog.activate();
        } else {
            String title = translate("ul.tooManyRevisions.title", new String[] { Integer.toString(maxNumOfRevisions), Integer.toString(versions.getRevisions().size()) });
            String description = translate("ul.tooManyRevisions.description", new String[] { Integer.toString(maxNumOfRevisions), Integer.toString(versions.getRevisions().size()) });
            removeAsListenerAndDispose(revisionListCtr);
            revisionListCtr = new RevisionListController(ureq, getWindowControl(), versionable, null, description, false);
            listenTo(revisionListCtr);
            removeAsListenerAndDispose(revisionListDialogBox);
            revisionListDialogBox = new CloseableModalController(getWindowControl(), translate("delete"), revisionListCtr.getInitialComponent(), true, title);
            listenTo(revisionListDialogBox);
            revisionListDialogBox.activate();
        }
    } else {
        // let calling method decide what to do.
        // for this, we put a list with "existing name" and "new name"
        overwriteDialog = DialogBoxUIFactory.createGenericDialog(ureq, getWindowControl(), translate("ul.overwrite.threeoptions.title"), translate("ul.overwrite.threeoptions.text", new String[] { existingVFSItem.getName(), renamedFilename }), asList(translate("ul.overwrite.threeoptions.overwrite"), translate("ul.overwrite.threeoptions.rename", renamedFilename), translate("ul.overwrite.threeoptions.cancel")));
        listenTo(overwriteDialog);
        overwriteDialog.activate();
    }
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) Versions(org.olat.core.util.vfs.version.Versions) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) OlatRootFileImpl(org.olat.core.commons.modules.bc.vfs.OlatRootFileImpl) RevisionListController(org.olat.core.commons.modules.bc.version.RevisionListController)

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