Search in sources :

Example 81 with LocalFileImpl

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

the class ForumWebService method getAttachment.

/**
 * Retrieves the attachment of the message
 * @response.representation.200.mediaType application/octet-stream
 * @response.representation.200.doc The portrait as image
 * @response.representation.404.doc The identity or the portrait not found
 * @param messageKey The identity key of the user being searched
 * @param filename The name of the attachment
 * @param request The REST request
 * @return The attachment
 */
@GET
@Path("posts/{messageKey}/attachments/{filename}")
@Produces({ "*/*", MediaType.APPLICATION_OCTET_STREAM })
public Response getAttachment(@PathParam("messageKey") Long messageKey, @PathParam("filename") String filename, @Context Request request) {
    // load message
    Message mess = fom.loadMessage(messageKey);
    if (mess == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!forum.equalsByPersistableKey(mess.getForum())) {
        return Response.serverError().status(Status.CONFLICT).build();
    }
    VFSContainer container = fom.getMessageContainer(mess.getForum().getKey(), mess.getKey());
    VFSItem item = container.resolve(filename);
    if (item instanceof LocalFileImpl) {
        // local file -> the length is given to the client which is good
        Date lastModified = new Date(item.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            File attachment = ((LocalFileImpl) item).getBasefile();
            String mimeType = WebappHelper.getMimeType(attachment.getName());
            if (mimeType == null)
                mimeType = "application/octet-stream";
            response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
        }
        return response.build();
    } else if (item instanceof VFSLeaf) {
        // stream -> the length is not given to the client which is not nice
        Date lastModified = new Date(item.getLastModified());
        Response.ResponseBuilder response = request.evaluatePreconditions(lastModified);
        if (response == null) {
            StreamingOutput attachment = new VFSStreamingOutput((VFSLeaf) item);
            String mimeType = WebappHelper.getMimeType(item.getName());
            if (mimeType == null)
                mimeType = "application/octet-stream";
            response = Response.ok(attachment).lastModified(lastModified).type(mimeType).cacheControl(cc);
        }
        return response.build();
    }
    return Response.serverError().status(Status.NOT_FOUND).build();
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Message(org.olat.modules.fo.Message) VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) VFSStreamingOutput(org.olat.core.util.vfs.restapi.VFSStreamingOutput) VFSStreamingOutput(org.olat.core.util.vfs.restapi.VFSStreamingOutput) StreamingOutput(javax.ws.rs.core.StreamingOutput) Date(java.util.Date) Response(javax.ws.rs.core.Response) File(java.io.File) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 82 with LocalFileImpl

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

the class GlossaryManagerImpl method getAsMediaResource.

/**
 * Export the glossary as a media resource. The resource name is set to the
 * resources display name
 *
 * @param res
 * @return
 */
@Override
public MediaResource getAsMediaResource(OLATResourceable res) {
    RepositoryEntry repoEntry = RepositoryManager.getInstance().lookupRepositoryEntry(res, false);
    String exportFileName = repoEntry.getDisplayname();
    // OO-135 check for special / illegal chars in filename
    exportFileName = StringHelper.transformDisplayNameToFileSystemName(exportFileName);
    try {
        File tmpDir = new File(WebappHelper.getTmpDir());
        File fExportZIP = File.createTempFile(exportFileName, ".zip", tmpDir);
        VFSContainer glossaryRoot = getGlossaryRootFolder(res);
        ZipUtil.zip(glossaryRoot.getItems(), new LocalFileImpl(fExportZIP), false);
        return new CleanupAfterDeliveryFileMediaResource(fExportZIP);
    } catch (IOException e) {
        logError("Cannot export glossar: " + res, e);
        return null;
    }
}
Also used : CleanupAfterDeliveryFileMediaResource(org.olat.core.gui.media.CleanupAfterDeliveryFileMediaResource) VFSContainer(org.olat.core.util.vfs.VFSContainer) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) RepositoryEntry(org.olat.repository.RepositoryEntry) IOException(java.io.IOException) File(java.io.File)

Example 83 with LocalFileImpl

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

the class MovieServiceImpl method getFrameCount.

@Override
public long getFrameCount(VFSLeaf media, String suffix) {
    File file = null;
    if (media instanceof VFSCPNamedItem) {
        media = ((VFSCPNamedItem) media).getDelegate();
    }
    if (media instanceof LocalFileImpl) {
        file = ((LocalFileImpl) media).getBasefile();
    }
    if (file == null) {
        return -1;
    }
    if (extensions.contains(suffix)) {
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "r")) {
            FileChannel ch = accessFile.getChannel();
            FileChannelWrapper in = new FileChannelWrapper(ch);
            MP4Demuxer demuxer1 = new MP4Demuxer(in);
            return demuxer1.getVideoTrack().getFrameCount();
        } catch (Exception | AssertionError e) {
            log.error("Cannot extract num. of frames of: " + media, e);
        }
    }
    return -1;
}
Also used : VFSCPNamedItem(org.olat.ims.cp.ui.VFSCPNamedItem) RandomAccessFile(java.io.RandomAccessFile) MP4Demuxer(org.jcodec.containers.mp4.demuxer.MP4Demuxer) FileChannel(java.nio.channels.FileChannel) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)

Example 84 with LocalFileImpl

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

the class MovieServiceImpl method getDuration.

@Override
public long getDuration(VFSLeaf media, String suffix) {
    File file = null;
    if (media instanceof VFSCPNamedItem) {
        media = ((VFSCPNamedItem) media).getDelegate();
    }
    if (media instanceof LocalFileImpl) {
        file = ((LocalFileImpl) media).getBasefile();
    }
    if (file == null) {
        return -1;
    }
    if (extensions.contains(suffix)) {
        try (RandomAccessFile accessFile = new RandomAccessFile(file, "r")) {
            FileChannel ch = accessFile.getChannel();
            FileChannelWrapper in = new FileChannelWrapper(ch);
            MP4Demuxer demuxer1 = new MP4Demuxer(in);
            MovieBox movie = demuxer1.getMovie();
            long duration = movie.getDuration();
            int timescale = movie.getTimescale();
            if (timescale < 1) {
                timescale = 1;
            }
            // Simple calculation. Ignore NTSC and other issues for now
            return duration / timescale * 1000;
        } catch (Exception | AssertionError e) {
            log.error("Cannot extract duration of: " + media, e);
        }
    }
    return -1;
}
Also used : MP4Demuxer(org.jcodec.containers.mp4.demuxer.MP4Demuxer) FileChannel(java.nio.channels.FileChannel) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) VFSCPNamedItem(org.olat.ims.cp.ui.VFSCPNamedItem) RandomAccessFile(java.io.RandomAccessFile) MovieBox(org.jcodec.containers.mp4.boxes.MovieBox) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 85 with LocalFileImpl

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

the class HTMLEditorController method getFileDebuggingPath.

/**
 * Helper method to get a meaningful debugging filename from the vfs
 * container and the file path
 *
 * @param root
 * @param relPath
 * @return
 */
private static String getFileDebuggingPath(VFSContainer root, String relPath) {
    String path = relPath;
    VFSItem item = root.resolve(relPath);
    if (item instanceof LocalFileImpl) {
        LocalFileImpl file = (LocalFileImpl) item;
        path = file.getBasefile().getAbsolutePath();
    } else {
        VFSContainer dir = root;
        while (dir != null) {
            path = "/" + dir.getName() + path;
            dir = dir.getParentContainer();
        }
    }
    return path;
}
Also used : VFSContainer(org.olat.core.util.vfs.VFSContainer) VFSItem(org.olat.core.util.vfs.VFSItem) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl)

Aggregations

LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)126 File (java.io.File)70 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)52 VFSContainer (org.olat.core.util.vfs.VFSContainer)32 Size (org.olat.core.commons.services.image.Size)22 ArrayList (java.util.ArrayList)20 IOException (java.io.IOException)18 VFSItem (org.olat.core.util.vfs.VFSItem)18 RandomAccessFile (java.io.RandomAccessFile)14 FileChannel (java.nio.channels.FileChannel)12 FileChannelWrapper (org.jcodec.common.FileChannelWrapper)12 CannotGenerateThumbnailException (org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)12 OLATResource (org.olat.resource.OLATResource)10 Date (java.util.Date)8 MP4Demuxer (org.jcodec.containers.mp4.demuxer.MP4Demuxer)8 FormLayoutContainer (org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer)8 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)8 VFSCPNamedItem (org.olat.ims.cp.ui.VFSCPNamedItem)8 BufferedImage (java.awt.image.BufferedImage)6 InputStream (java.io.InputStream)6