Search in sources :

Example 36 with VFSMediaResource

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

the class DisclaimerController method event.

/**
 * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
 */
@Override
public void event(UserRequest ureq, Component source, Event event) {
    if (source == this.downloadLink) {
        ureq.getDispatchResult().setResultingMediaResource(new VFSMediaResource(this.downloadFile));
        // Prevent "do not press reload" message.
        this.downloadLink.setDirty(false);
    }
}
Also used : VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 37 with VFSMediaResource

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

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 38 with VFSMediaResource

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

the class CmdServeThumbnailResource method execute.

@Override
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;
    VFSLeaf vfsfile = (VFSLeaf) folderComponent.getRootContainer().resolve(path);
    if (vfsfile == null) {
        // double decoding of ++
        vfsfile = (VFSLeaf) FolderCommandHelper.tryDoubleDecoding(ureq, folderComponent);
    }
    if (vfsfile instanceof MetaTagged) {
        MetaInfo info = ((MetaTagged) vfsfile).getMetaInfo();
        if (info != null && info.isThumbnailAvailable()) {
            VFSLeaf thumbnail = info.getThumbnail(200, 200, false);
            if (thumbnail != null) {
                mr = new VFSMediaResource(thumbnail);
            }
        }
    }
    if (mr == null) {
        mr = new NotFoundMediaResource();
    }
    ureq.getDispatchResult().setResultingMediaResource(mr);
    return null;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) MetaTagged(org.olat.core.commons.modules.bc.meta.tagged.MetaTagged) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) MediaResource(org.olat.core.gui.media.MediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) VFSSecurityCallback(org.olat.core.util.vfs.callbacks.VFSSecurityCallback) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 39 with VFSMediaResource

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

the class HtmlStaticMapper method handle.

public MediaResource handle(String relPath, HttpServletRequest request) {
    if (log.isDebug())
        log.debug("CPComponent Mapper relPath=" + relPath);
    VFSItem currentItem = mapperRootContainer.resolve(relPath);
    if (currentItem == null || (currentItem instanceof VFSContainer)) {
        return new NotFoundMediaResource();
    }
    VFSMediaResource vmr = new VFSMediaResource((VFSLeaf) currentItem);
    String encoding = SimpleHtmlParser.extractHTMLCharset(((VFSLeaf) currentItem));
    if (log.isDebug())
        log.debug("CPComponent Mapper set encoding=" + encoding);
    // 
    vmr.setEncoding(encoding);
    return vmr;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 40 with VFSMediaResource

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

the class HtmlStaticPageComponent method getAsyncMediaResource.

/**
 * @see org.olat.core.gui.media.AsyncMediaResponsible#getAsyncMediaResource(org.olat.core.gui.UserRequest)
 */
public MediaResource getAsyncMediaResource(UserRequest ureq) {
    // is the path to the desired resource (put together by the webbrowser by
    // combining latesturl and relative link)
    String moduleURI = ureq.getModuleURI();
    MediaResource mr = null;
    if (moduleURI != null) {
        // 1. check for an olat command (special link to indicate a command)
        if (moduleURI.startsWith(OLAT_CMD_PREFIX)) {
            String cmdAndSub = moduleURI.substring(OLAT_CMD_PREFIX.length());
            int slpos = cmdAndSub.indexOf('/');
            if (slpos != -1) {
                String cmd = cmdAndSub.substring(0, slpos);
                String subcmd = cmdAndSub.substring(slpos + 1);
                OlatCmdEvent aev = new OlatCmdEvent(cmd, subcmd);
                fireEvent(ureq, aev);
                // Mediaresourse)
                if (aev.isAccepted())
                    return null;
            }
        // else ignore (syntax error in command
        }
        // make sure moduleURI does not contain ".." or such (hack attempt)
        // -> ok, userrequest class asserts this.
        VFSItem sourceItem = rootContainer.resolve(moduleURI);
        // return 404 if the requested file does not exist
        if (sourceItem == null || (sourceItem instanceof VFSContainer)) {
            return new NotFoundMediaResource();
        }
        // we know the file exists.
        boolean checkRegular = true;
        // check for special case: render the follwing link in a new (server) window and all following clicks as well
        if ((ureq.getParameter("olatsite") != null) || ((moduleURI.endsWith(".html") || moduleURI.endsWith(".htm")) && (ureq.getParameter("olatraw") != null))) {
            log.debug("moduleURI=" + moduleURI);
            ExternalSiteEvent ese = new ExternalSiteEvent(moduleURI);
            fireEvent(ureq, ese);
            if (ese.isAccepted()) {
                mr = ese.getResultingMediaResource();
                log.debug("ExternalSiteEvent is accepted");
                checkRegular = false;
            } else {
                // it is a html page with olatraw parameter => redirect to mapper
                Mapper mapper = new HtmlStaticMapper(rootContainer);
                // NOTE: do not deregister this mapper, since it could be used a lot later (since it is opened in a new browser window)
                // Register mapper as cacheable
                String mapperID = VFSManager.getRealPath(rootContainer);
                if (mapperID == null) {
                    // Can't cache mapper, no cacheable context available
                    amapPath = CoreSpringFactory.getImpl(MapperService.class).register(ureq.getUserSession(), mapper);
                } else {
                    // Add classname to the file path to remove conflicts with other
                    // usages of the same file path
                    mapperID = this.getClass().getSimpleName() + ":" + mapperID;
                    amapPath = CoreSpringFactory.getImpl(MapperService.class).register(ureq.getUserSession(), mapperID, mapper);
                }
                String path = amapPath.getUrl() + "/" + moduleURI;
                ese.setResultingMediaResource(new RedirectMediaResource(path));
                if (log.isDebug())
                    log.debug("RedirectMediaResource=" + path);
                ese.accept();
                mr = ese.getResultingMediaResource();
                checkRegular = false;
            }
        }
        if (checkRegular) {
            // mediaresource (raw inputstream)
            if ((moduleURI.endsWith(".html") || moduleURI.endsWith(".htm")) && (ureq.getParameter("olatraw") == null)) {
                // we remember what to render inline later and return null to indicate
                // inline rendering
                currentURI = moduleURI;
                getFileContent((VFSLeaf) sourceItem);
                fireEvent(ureq, new NewInlineUriEvent(currentURI));
            } else {
                // it is indeed an image or such -> serve it
                mr = new VFSMediaResource((VFSLeaf) sourceItem);
            }
        }
    }
    // -> do a normal inline rendering (reload)
    return mr;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) Mapper(org.olat.core.dispatcher.mapper.Mapper) RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Aggregations

VFSMediaResource (org.olat.core.util.vfs.VFSMediaResource)48 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)44 MediaResource (org.olat.core.gui.media.MediaResource)30 VFSItem (org.olat.core.util.vfs.VFSItem)28 NotFoundMediaResource (org.olat.core.gui.media.NotFoundMediaResource)24 VFSContainer (org.olat.core.util.vfs.VFSContainer)12 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)10 MetaTagged (org.olat.core.commons.modules.bc.meta.tagged.MetaTagged)10 StringMediaResource (org.olat.core.gui.media.StringMediaResource)6 OlatRootFolderImpl (org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl)4 Mapper (org.olat.core.dispatcher.mapper.Mapper)4 VFSSecurityCallback (org.olat.core.util.vfs.callbacks.VFSSecurityCallback)4 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Size (org.olat.core.commons.services.image.Size)2