Search in sources :

Example 1 with VFSItem

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

the class CmdDelete method event.

public void event(UserRequest ureq, Controller source, Event event) {
    if (source == dialogCtr) {
        if (DialogBoxUIFactory.isYesEvent(event)) {
            // do delete
            VFSContainer currentContainer = folderComponent.getCurrentContainer();
            List<String> files = fileSelection.getFiles();
            if (files.size() == 0) {
                // sometimes, browser sends empty form data...
                getWindowControl().setError(translator.translate("failed"));
                status = FolderCommandStatus.STATUS_FAILED;
                fireEvent(ureq, FOLDERCOMMAND_FINISHED);
            }
            for (String file : files) {
                VFSItem item = currentContainer.resolve(file);
                if (item != null && (item.canDelete() == VFSConstants.YES)) {
                    if (item instanceof MetaTagged) {
                        // delete all meta info
                        MetaInfo meta = ((MetaTagged) item).getMetaInfo();
                        if (meta != null) {
                            meta.deleteAll();
                        }
                    }
                    // delete the item itself
                    item.delete();
                } else {
                    getWindowControl().setWarning(translator.translate("del.partial"));
                }
            }
            String confirmationText = fileSelection.renderAsHtml();
            fireEvent(ureq, new FolderEvent(FolderEvent.DELETE_EVENT, confirmationText));
            fireEvent(ureq, FOLDERCOMMAND_FINISHED);
        } else {
            // abort
            status = FolderCommandStatus.STATUS_CANCELED;
            fireEvent(ureq, FOLDERCOMMAND_FINISHED);
        }
    }
}
Also used : 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) FolderEvent(org.olat.core.commons.modules.bc.FolderEvent)

Example 2 with VFSItem

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

the class CmdServeResource method execute.

public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wControl, Translator translator) {
    VFSSecurityCallback inheritedSecCallback = VFSManager.findInheritedSecurityCallback(folderComponent.getCurrentContainer());
    if (inheritedSecCallback != null && !inheritedSecCallback.canRead())
        throw new RuntimeException("Illegal read attempt: " + folderComponent.getCurrentContainerPath());
    // extract file
    String path = ureq.getModuleURI();
    MediaResource mr = null;
    VFSItem vfsitem = folderComponent.getRootContainer().resolve(path);
    if (vfsitem == null) {
        // double decoding of ++
        vfsitem = FolderCommandHelper.tryDoubleDecoding(ureq, folderComponent);
    }
    if (vfsitem == null) {
        mr = new NotFoundMediaResource();
    } else if (!(vfsitem instanceof VFSLeaf)) {
        mr = new NotFoundMediaResource();
    } else {
        VFSLeaf vfsfile = (VFSLeaf) vfsitem;
        boolean forceDownload = FolderManager.isDownloadForcedFileType(vfsfile.getName());
        if (path.toLowerCase().endsWith(".html") || path.toLowerCase().endsWith(".htm")) {
            // setCurrentURI(path);
            // set the http content-type and the encoding
            // try to load in iso-8859-1
            InputStream is = vfsfile.getInputStream();
            if (is == null) {
                mr = new NotFoundMediaResource();
            } else {
                String page = FileUtils.load(is, DEFAULT_ENCODING);
                // search for the <meta content="text/html; charset=utf-8"
                // http-equiv="Content-Type" /> tag
                // if none found, assume iso-8859-1
                String enc = DEFAULT_ENCODING;
                boolean useLoaded = false;
                // <meta.*charset=([^"]*)"
                Matcher m = PATTERN_ENCTYPE.matcher(page);
                boolean found = m.find();
                if (found) {
                    String htmlcharset = m.group(1);
                    enc = htmlcharset;
                    if (htmlcharset.equals(DEFAULT_ENCODING)) {
                        useLoaded = true;
                    }
                } else {
                    useLoaded = true;
                }
                // set the new encoding to remember for any following .js file loads
                g_encoding = enc;
                if (useLoaded) {
                    StringMediaResource smr = new StringMediaResource();
                    String mimetype = forceDownload ? VFSMediaResource.MIME_TYPE_FORCE_DOWNLOAD : "text/html;charset=" + enc;
                    smr.setContentType(mimetype);
                    smr.setEncoding(enc);
                    smr.setData(page);
                    if (forceDownload) {
                        smr.setDownloadable(true, vfsfile.getName());
                    }
                    mr = smr;
                } else {
                    // found a new charset other than iso-8859-1 -> let it load again
                    // as bytes (so we do not need to convert to string and back
                    // again)
                    VFSMediaResource vmr = new VFSMediaResource(vfsfile);
                    vmr.setEncoding(enc);
                    if (forceDownload) {
                        vmr.setDownloadable(true);
                    }
                    mr = vmr;
                }
            }
        } else if (path.endsWith(".js")) {
            // a javascript library
            VFSMediaResource vmr = new VFSMediaResource(vfsfile);
            // that loads the .js file
            if (g_encoding != null) {
                vmr.setEncoding(g_encoding);
            }
            if (forceDownload) {
                vmr.setDownloadable(true);
            }
            mr = vmr;
        } else if (path.endsWith(".txt")) {
            // text files created in OpenOLAT are utf-8, prefer this encoding
            VFSMediaResource vmr = new VFSMediaResource(vfsfile);
            vmr.setEncoding("utf-8");
            if (forceDownload) {
                vmr.setDownloadable(true);
            }
            mr = vmr;
        } else {
            // binary data: not .html, not .htm, not .js -> treated as is
            VFSMediaResource vmr = new VFSMediaResource(vfsfile);
            if (forceDownload) {
                vmr.setDownloadable(true);
            }
            mr = vmr;
        }
    }
    ThreadLocalUserActivityLogger.log(FolderLoggingAction.BC_FILE_READ, getClass(), CoreLoggingResourceable.wrapBCFile(path));
    ureq.getDispatchResult().setResultingMediaResource(mr);
    // update download counter
    if (vfsitem instanceof MetaTagged) {
        MetaTagged itemWithMeta = (MetaTagged) vfsitem;
        MetaInfo meta = itemWithMeta.getMetaInfo();
        meta.increaseDownloadCount();
        meta.write();
    }
    return null;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) 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) StringMediaResource(org.olat.core.gui.media.StringMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) StringMediaResource(org.olat.core.gui.media.StringMediaResource) VFSSecurityCallback(org.olat.core.util.vfs.callbacks.VFSSecurityCallback) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 3 with VFSItem

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

the class FolderComponent method setCurrentContainerPath.

public boolean setCurrentContainerPath(String relPath) {
    // get the container
    setDirty(true);
    if (relPath == null)
        relPath = "/";
    if (!(relPath.charAt(0) == '/'))
        relPath = "/" + relPath;
    VFSItem vfsItem = rootContainer.resolve(relPath);
    if (vfsItem == null || !(vfsItem instanceof VFSContainer)) {
        // unknown path, reset to root contaner...
        currentContainer = rootContainer;
        relPath = "";
        return false;
    }
    this.currentContainer = (VFSContainer) vfsItem;
    this.currentContainerPath = relPath;
    updateChildren();
    return true;
}
Also used : VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem)

Example 4 with VFSItem

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

the class ListRenderer method render.

/**
 * Render contents of directory to a html table.
 *
 * @param dir
 * @param secCallback
 * @param ubu
 * @param translator
 * @param iframePostEnabled
 * @return Render results.
 */
public void render(FolderComponent fc, StringOutput sb, URLBuilder ubu, Translator translator, boolean iframePostEnabled) {
    if (lockManager == null) {
        lockManager = CoreSpringFactory.getImpl(VFSLockManager.class);
    }
    if (userManager == null) {
        userManager = CoreSpringFactory.getImpl(UserManager.class);
    }
    LicenseModule licenseModule = CoreSpringFactory.getImpl(LicenseModule.class);
    LicenseHandler licenseHandler = CoreSpringFactory.getImpl(FolderLicenseHandler.class);
    licensesEnabled = licenseModule.isEnabled(licenseHandler);
    List<VFSItem> children = fc.getCurrentContainerChildren();
    // folder empty?
    if (children.size() == 0) {
        sb.append("<div class=\"o_bc_empty\"><i class='o_icon o_icon_warn'></i> ").append(translator.translate("NoFiles")).append("</div>");
        return;
    }
    boolean canVersion = FolderConfig.versionsEnabled(fc.getCurrentContainer());
    String sortOrder = fc.getCurrentSortOrder();
    boolean sortAsc = fc.isCurrentSortAsc();
    String sortCss = (sortAsc ? "o_orderby_asc" : "o_orderby_desc");
    sb.append("<table class=\"table table-condensed table-striped table-hover o_bc_table\">").append("<thead><tr><th><a class='o_orderby ").append(sortCss, FolderComponent.SORT_NAME.equals(sortOrder)).append("' ");
    ubu.buildHrefAndOnclick(sb, null, iframePostEnabled, false, false, new NameValuePair(PARAM_SORTID, FolderComponent.SORT_NAME)).append(">").append(translator.translate("header.Name")).append("</a>").append("</th>");
    sb.append("<th><a class='o_orderby ").append(sortCss, FolderComponent.SORT_SIZE.equals(sortOrder)).append("' ");
    ubu.buildHrefAndOnclick(sb, null, iframePostEnabled, false, false, new NameValuePair(PARAM_SORTID, FolderComponent.SORT_SIZE)).append(">").append(translator.translate("header.Size")).append("</a>").append("</th><th><a class='o_orderby ").append(sortCss, FolderComponent.SORT_DATE.equals(sortOrder)).append("' ");
    ubu.buildHrefAndOnclick(sb, null, iframePostEnabled, false, false, new NameValuePair(PARAM_SORTID, FolderComponent.SORT_DATE)).append(">").append(translator.translate("header.Modified")).append("</a>");
    if (licensesEnabled) {
        sb.append("<th>").append(translator.translate("header.license")).append("</th>");
    }
    if (canVersion) {
        sb.append("</th><th><a class='o_orderby ").append(sortCss, FolderComponent.SORT_REV.equals(sortOrder)).append("' ");
        // file size column
        ubu.buildHrefAndOnclick(sb, null, iframePostEnabled, false, false, new NameValuePair(PARAM_SORTID, FolderComponent.SORT_REV)).append("><i class=\"o_icon o_icon_version  o_icon-lg\" title=\"").append(translator.translate("versions")).append("\"></i></a>");
    }
    sb.append("</th><th><a class='o_orderby ").append(sortCss, FolderComponent.SORT_LOCK.equals(sortOrder)).append("' ");
    ubu.buildHrefAndOnclick(sb, null, iframePostEnabled, false, false, new NameValuePair(PARAM_SORTID, FolderComponent.SORT_LOCK)).append("><i class=\"o_icon o_icon_locked  o_icon-lg\" title=\"").append(translator.translate("lock.title")).append("\"></i></a>").append("</th><th><i class=\"o_icon o_icon_edit_metadata o_icon-lg\" title=\"").append(translator.translate("mf.edit")).append("\"></i></th></tr></thead>");
    // render directory contents
    String currentContainerPath = fc.getCurrentContainerPath();
    if (currentContainerPath.length() > 0 && currentContainerPath.charAt(0) == '/') {
        currentContainerPath = currentContainerPath.substring(1);
    }
    sb.append("<tbody>");
    for (int i = 0; i < children.size(); i++) {
        VFSItem child = children.get(i);
        appendRenderedFile(fc, child, currentContainerPath, sb, ubu, translator, iframePostEnabled, canVersion, i);
    }
    sb.append("</tbody></table>");
}
Also used : NameValuePair(org.olat.core.gui.components.form.flexible.impl.NameValuePair) FolderLicenseHandler(org.olat.core.commons.modules.bc.FolderLicenseHandler) LicenseHandler(org.olat.core.commons.services.license.LicenseHandler) UserManager(org.olat.user.UserManager) VFSItem(org.olat.core.util.vfs.VFSItem) VFSLockManager(org.olat.core.util.vfs.VFSLockManager) LicenseModule(org.olat.core.commons.services.license.LicenseModule)

Example 5 with VFSItem

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

the class CmdUnzip method checkLockedFiles.

private List<String> checkLockedFiles(VFSLeaf vfsItem, VFSContainer currentContainer, Identity identity, Roles roles) {
    String name = vfsItem.getName();
    if (!name.toLowerCase().endsWith(".zip")) {
        return Collections.emptyList();
    }
    boolean versioning = FolderConfig.versionsEnabled(currentContainer);
    if (!versioning) {
        // this command don't overwrite existing folders
        return Collections.emptyList();
    }
    String sZipContainer = name.substring(0, name.length() - 4);
    VFSItem zipContainer = currentContainer.resolve(sZipContainer);
    if (zipContainer == null) {
        return Collections.emptyList();
    } else if (zipContainer instanceof VFSContainer) {
        return ZipUtil.checkLockedFileBeforeUnzipNonStrict(vfsItem, (VFSContainer) zipContainer, identity, roles);
    } else {
        // replace a file with a folder ???
        return Collections.emptyList();
    }
}
Also used : VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem)

Aggregations

VFSItem (org.olat.core.util.vfs.VFSItem)546 VFSContainer (org.olat.core.util.vfs.VFSContainer)356 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)272 File (java.io.File)78 Test (org.junit.Test)68 OlatRootFolderImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl)68 ArrayList (java.util.ArrayList)64 InputStream (java.io.InputStream)52 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)52 Identity (org.olat.core.id.Identity)50 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)48 IOException (java.io.IOException)42 Date (java.util.Date)40 URI (java.net.URI)38 LocalFolderImpl (org.olat.core.util.vfs.LocalFolderImpl)38 SystemItemFilter (org.olat.core.util.vfs.filters.SystemItemFilter)34 OutputStream (java.io.OutputStream)30 VFSMediaResource (org.olat.core.util.vfs.VFSMediaResource)28 HttpResponse (org.apache.http.HttpResponse)22 URL (java.net.URL)20