Search in sources :

Example 26 with MediaResource

use of org.olat.core.gui.media.MediaResource in project OpenOLAT by OpenOLAT.

the class RepositoryEntryResource method getRepoFileById.

/**
 * Download the export zip file of a repository entry.
 * @response.representation.mediaType multipart/form-data
 * @response.representation.doc Download the resource file
 * @response.representation.200.mediaType application/zip
 * @response.representation.200.doc Download the repository entry as export zip file
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_REPOENTRYVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The resource could not found
 * @response.representation.406.doc Download of this resource is not possible
 * @response.representation.409.doc The resource is locked
 * @param repoEntryKey
 * @param request The HTTP request
 * @return
 */
@GET
@Path("file")
@Produces({ "application/zip", MediaType.APPLICATION_OCTET_STREAM })
public Response getRepoFileById(@PathParam("repoEntryKey") String repoEntryKey, @Context HttpServletRequest request) {
    RepositoryEntry re = lookupRepositoryEntry(repoEntryKey);
    if (re == null)
        return Response.serverError().status(Status.NOT_FOUND).build();
    RepositoryHandler typeToDownload = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re);
    if (typeToDownload == null)
        return Response.serverError().status(Status.NOT_FOUND).build();
    OLATResource ores = OLATResourceManager.getInstance().findResourceable(re.getOlatResource());
    if (ores == null)
        return Response.serverError().status(Status.NOT_FOUND).build();
    Identity identity = getIdentity(request);
    boolean isAuthor = RestSecurityHelper.isAuthor(request);
    boolean isOwner = repositoryManager.isOwnerOfRepositoryEntry(identity, re);
    if (!(isAuthor | isOwner))
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    boolean canDownload = re.getCanDownload() && typeToDownload.supportsDownload();
    if (!canDownload)
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    boolean isAlreadyLocked = typeToDownload.isLocked(ores);
    LockResult lockResult = null;
    try {
        lockResult = typeToDownload.acquireLock(ores, identity);
        if (lockResult == null || (lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
            MediaResource mr = typeToDownload.getAsMediaResource(ores, false);
            if (mr != null) {
                repositoryService.incrementDownloadCounter(re);
                // success
                return Response.ok(mr.getInputStream()).cacheControl(cc).build();
            } else
                return Response.serverError().status(Status.NO_CONTENT).build();
        } else
            return Response.serverError().status(Status.CONFLICT).build();
    } finally {
        if ((lockResult != null && lockResult.isSuccess() && !isAlreadyLocked))
            typeToDownload.releaseLock(lockResult);
    }
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) OLATResource(org.olat.resource.OLATResource) MediaResource(org.olat.core.gui.media.MediaResource) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 27 with MediaResource

use of org.olat.core.gui.media.MediaResource in project OpenOLAT by OpenOLAT.

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 28 with MediaResource

use of org.olat.core.gui.media.MediaResource in project OpenOLAT by OpenOLAT.

the class RepositoryEntryRuntimeController method doDownload.

private void doDownload(UserRequest ureq) {
    if (handler == null) {
        StringBuilder sb = new StringBuilder(translate("error.download"));
        sb.append(": No download handler for repository entry: ").append(re.getKey());
        showError(sb.toString());
        return;
    }
    RepositoryEntry entry = repositoryService.loadByKey(re.getKey());
    OLATResourceable ores = entry.getOlatResource();
    if (ores == null) {
        showError("error.download");
        return;
    }
    boolean isAlreadyLocked = handler.isLocked(ores);
    try {
        lockResult = handler.acquireLock(ores, ureq.getIdentity());
        if (lockResult == null || (lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
            MediaResource mr = handler.getAsMediaResource(ores, false);
            if (mr != null) {
                repositoryService.incrementDownloadCounter(entry);
                ureq.getDispatchResult().setResultingMediaResource(mr);
            } else {
                showError("error.export");
                fireEvent(ureq, Event.FAILED_EVENT);
            }
        } else if (lockResult != null && lockResult.isSuccess() && isAlreadyLocked) {
            String fullName = userManager.getUserDisplayName(lockResult.getOwner());
            showInfo("warning.course.alreadylocked.bySameUser", fullName);
            // invalid lock, it was already locked
            lockResult = null;
        } else {
            String fullName = userManager.getUserDisplayName(lockResult.getOwner());
            showInfo("warning.course.alreadylocked", fullName);
        }
    } finally {
        if ((lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
            handler.releaseLock(lockResult);
            lockResult = null;
        }
    }
}
Also used : OLATResourceable(org.olat.core.id.OLATResourceable) MediaResource(org.olat.core.gui.media.MediaResource) RepositoryEntry(org.olat.repository.RepositoryEntry)

Example 29 with MediaResource

use of org.olat.core.gui.media.MediaResource in project OpenOLAT by OpenOLAT.

the class GoToRecordingController method event.

@Override
protected void event(UserRequest ureq, Component source, Event event) {
    if (source == downloadLink) {
        String url = recording.getDownloadUrl();
        MediaResource downloadUrl = new RedirectMediaResource(url);
        ureq.getDispatchResult().setResultingMediaResource(downloadUrl);
    }
}
Also used : RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource)

Example 30 with MediaResource

use of org.olat.core.gui.media.MediaResource in project OpenOLAT by OpenOLAT.

the class DisplayPortraitManager method getPortraitResource.

/**
 * Get the portrait media resource by identity name (username)
 * @param identity
 * @return imageResource portrait
 */
private MediaResource getPortraitResource(String username, String portraitName) {
    MediaResource imageResource = null;
    File imgFile = getPortraitFile(username, portraitName);
    if (imgFile != null && imgFile.exists()) {
        imageResource = new FileMediaResource(imgFile);
    }
    return imageResource;
}
Also used : FileMediaResource(org.olat.core.gui.media.FileMediaResource) FileMediaResource(org.olat.core.gui.media.FileMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) File(java.io.File)

Aggregations

MediaResource (org.olat.core.gui.media.MediaResource)141 VFSMediaResource (org.olat.core.util.vfs.VFSMediaResource)36 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)32 NotFoundMediaResource (org.olat.core.gui.media.NotFoundMediaResource)30 Identity (org.olat.core.id.Identity)18 VFSItem (org.olat.core.util.vfs.VFSItem)18 File (java.io.File)16 ArrayList (java.util.ArrayList)16 RedirectMediaResource (org.olat.core.gui.media.RedirectMediaResource)16 IOException (java.io.IOException)13 FileMediaResource (org.olat.core.gui.media.FileMediaResource)12 List (java.util.List)10 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)8 AssertException (org.olat.core.logging.AssertException)8 RepositoryEntry (org.olat.repository.RepositoryEntry)8 RepositoryHandler (org.olat.repository.handlers.RepositoryHandler)8 InputStream (java.io.InputStream)6 OLATResourceable (org.olat.core.id.OLATResourceable)6