Search in sources :

Example 31 with VFSMediaResource

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

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)

Example 32 with VFSMediaResource

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

the class CPPrintMapper method deliverFile.

protected MediaResource deliverFile(HttpServletRequest httpRequest, String path) {
    // if directory gets renamed root becomes null
    if (rootDir == null) {
        return new NotFoundMediaResource();
    }
    VFSLeaf vfsLeaf = null;
    VFSItem vfsItem = rootDir.resolve(path);
    // only files are allowed, but somehow it happened that folders showed up here
    if (vfsItem instanceof VFSLeaf) {
        vfsLeaf = (VFSLeaf) vfsItem;
    } else {
        return new NotFoundMediaResource();
    }
    MediaResource mr;
    // and accept positions of this string at length-3 or length-4
    if (path.toLowerCase().lastIndexOf(FILE_SUFFIX_HTM) >= (path.length() - 4)) {
        // set the http content-type and the encoding
        Page page = loadPageWithGuess(vfsLeaf);
        g_encoding = page.getEncoding();
        if (page.isUseLoadedPageString()) {
            mr = prepareMediaResource(httpRequest, page.getPage(), g_encoding, page.getContentType());
        } else {
            // found a new charset other than iso-8859-1, load string with proper encoding
            String content = FileUtils.load(vfsLeaf.getInputStream(), g_encoding);
            mr = prepareMediaResource(httpRequest, content, g_encoding, page.getContentType());
        }
    } else if (path.toLowerCase().lastIndexOf(FILE_SUFFIX_CSS) >= (path.length() - 4)) {
        // set the http content-type and the encoding
        mr = deliverCssFile(vfsLeaf, httpRequest);
    } else if (path.endsWith(FILE_SUFFIX_JS)) {
        // a javascript library
        VFSMediaResource vmr = new VFSMediaResource(vfsLeaf);
        // that loads the .js file
        if (jsEncoding != null)
            vmr.setEncoding(jsEncoding);
        else if (g_encoding != null)
            vmr.setEncoding(g_encoding);
        mr = vmr;
    } else {
        // binary data: not .html, not .htm, not .js -> treated as is
        mr = new VFSMediaResource(vfsLeaf);
    }
    return mr;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) VFSItem(org.olat.core.util.vfs.VFSItem) MediaResource(org.olat.core.gui.media.MediaResource) StringMediaResource(org.olat.core.gui.media.StringMediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 33 with VFSMediaResource

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

the class FeedMediaDispatcher method deliverFile.

/**
 * Dispatch and deliver the requested file given in the path.
 *
 * @param request
 * @param response
 * @param feed
 * @param path
 */
private void deliverFile(HttpServletRequest request, HttpServletResponse response, OLATResourceable feed, Path path) {
    // OLAT-5243 related: deliverFile can last arbitrary long, which can cause the open db connection to timeout and cause errors,
    // hence we need to do an intermediateCommit here
    DBFactory.getInstance().intermediateCommit();
    // Create the resource to be delivered
    MediaResource resource = null;
    FeedManager manager = FeedManager.getInstance();
    if (path.isFeedType()) {
        // Only create feed if modified. Send not modified response else.
        Identity identity = getIdentity(path.getIdentityKey());
        long sinceModifiedMillis = request.getDateHeader("If-Modified-Since");
        Feed feedLight = manager.loadFeed(feed);
        long lastModifiedMillis = -1;
        if (feedLight != null) {
            lastModifiedMillis = feedLight.getLastModified().getTime();
        }
        if (sinceModifiedMillis >= (lastModifiedMillis / 1000L) * 1000L) {
            // Send not modified response
            response.setDateHeader("last-modified", lastModifiedMillis);
            try {
                response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
                return;
            } catch (IOException e) {
                // Send not modified failed
                log.error("Send not modified failed", e);
                return;
            }
        } else {
            resource = manager.createFeedFile(feed, identity, path.getCourseId(), path.getNodeId());
        }
    } else if (path.isItemType()) {
        resource = manager.createItemMediaFile(feed, path.getItemId(), path.getItemFileName());
    } else if (path.isIconType()) {
        Size thumbnailSize = null;
        String thumbnail = request.getParameter("thumbnail");
        if (StringHelper.containsNonWhitespace(thumbnail)) {
            thumbnailSize = Size.parseString(thumbnail);
        }
        VFSLeaf resourceFile = manager.createFeedMediaFile(feed, path.getIconFileName(), thumbnailSize);
        if (resourceFile != null) {
            resource = new VFSMediaResource(resourceFile);
        }
    }
    // Eventually deliver the requested resource
    ServletUtil.serveResource(request, response, resource);
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) FeedManager(org.olat.modules.webFeed.manager.FeedManager) Size(org.olat.core.commons.services.image.Size) MediaResource(org.olat.core.gui.media.MediaResource) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) IOException(java.io.IOException) Identity(org.olat.core.id.Identity) Feed(org.olat.modules.webFeed.Feed) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 34 with VFSMediaResource

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

the class RepositoryEntryImageMapper method handle.

@Override
public MediaResource handle(String relPath, HttpServletRequest request) {
    if (rootContainer == null) {
        rootContainer = new LocalFolderImpl(new File(FolderConfig.getCanonicalRepositoryHome()));
    }
    if (relPath.startsWith("/")) {
        relPath = relPath.substring(1, relPath.length());
    }
    MediaResource resource = null;
    VFSItem image = rootContainer.resolve(relPath);
    if (image instanceof VFSLeaf) {
        if (image instanceof MetaTagged) {
            MetaInfo info = ((MetaTagged) image).getMetaInfo();
            if (info != null) {
                // 121 is needed to fill the div
                VFSLeaf thumbnail = info.getThumbnail(180, 120, true);
                if (thumbnail != null) {
                    resource = new VFSMediaResource(thumbnail);
                }
            }
        }
        if (resource == null) {
            resource = new VFSMediaResource((VFSLeaf) image);
        }
    }
    return resource;
}
Also used : 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) VFSItem(org.olat.core.util.vfs.VFSItem) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) File(java.io.File) LocalFolderImpl(org.olat.core.util.vfs.LocalFolderImpl) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource)

Example 35 with VFSMediaResource

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

the class CatalogEntryImageMapper method handle.

@Override
public MediaResource handle(String relPath, HttpServletRequest request) {
    if (relPath.startsWith("/")) {
        relPath = relPath.substring(1, relPath.length());
    }
    VFSContainer categoryResources = catalogManager.getCatalogResourcesHome();
    VFSItem image = categoryResources.resolve(relPath);
    MediaResource resource = null;
    if (image instanceof VFSLeaf) {
        if (image instanceof MetaTagged) {
            MetaInfo info = ((MetaTagged) image).getMetaInfo();
            if (info != null) {
                VFSLeaf thumbnail = info.getThumbnail(180, 180, true);
                if (thumbnail != null) {
                    resource = new VFSMediaResource(thumbnail);
                }
            }
        }
        if (resource == null) {
            resource = new VFSMediaResource((VFSLeaf) image);
        }
    } else {
        resource = new NotFoundMediaResource();
    }
    return resource;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) 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) VFSMediaResource(org.olat.core.util.vfs.VFSMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) 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