Search in sources :

Example 56 with LocalFileImpl

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

the class HTMLEditorController method initEditorForm.

private void initEditorForm(VFSContainer bContainer, String relFilePath, CustomLinkTreeModel linkTreeModel, String mPath, boolean editorCheck, boolean versions, boolean withButtons) {
    this.baseContainer = bContainer;
    this.fileRelPath = relFilePath;
    this.mediaPath = mPath;
    this.versionsEnabled = versions;
    this.buttonsEnabled = withButtons;
    this.customLinkTreeModel = linkTreeModel;
    this.editorCheckEnabled = editorCheck;
    // make sure the filename doesn't start with a slash
    this.fileName = ((relFilePath.charAt(0) == '/') ? relFilePath.substring(1) : relFilePath);
    this.fileLeaf = (VFSLeaf) bContainer.resolve(fileName);
    if (fileLeaf == null)
        throw new AssertException("file::" + getFileDebuggingPath(bContainer, relFilePath) + " does not exist!");
    long size = fileLeaf.getSize();
    if (size > FolderConfig.getMaxEditSizeLimit()) {
        // limit to reasonable size, see OO-57
        fileToLargeError = translate("plaintext.error.tolarge", new String[] { (size / 1000) + "", (FolderConfig.getMaxEditSizeLimit() / 1000) + "" });
        this.body = "";
        this.editable = false;
        return;
    }
    // check if someone else is already editing the file
    if (fileLeaf instanceof LocalFileImpl) {
        // Cast to LocalFile necessary because the VFSItem is missing some
        // ID mechanism that identifies an item within the system
        OLATResourceable lockResourceable = createLockResourceable(fileLeaf);
        // OLAT-5066: the use of "fileName" gives users the (false) impression that the file they wish to access
        // is already locked by someone else. Since the lock token must be smaller than 50 characters we us an
        // MD5 hash of the absolute file path which will always be 32 characters long and virtually unique.
        String lockToken = createLockToken(bContainer, relFilePath);
        lock = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(lockResourceable, getIdentity(), lockToken);
        VelocityContainer vc = (VelocityContainer) flc.getComponent();
        if (!lock.isSuccess()) {
            vc.contextPut("locked", Boolean.TRUE);
            String fullname = UserManager.getInstance().getUserDisplayName(lock.getOwner());
            vc.contextPut("lockOwner", fullname);
            editable = false;
            return;
        } else {
            vc.contextPut("locked", Boolean.FALSE);
        }
    }
    // Parse the content of the page
    this.body = parsePage(fileLeaf);
}
Also used : AssertException(org.olat.core.logging.AssertException) OLATResourceable(org.olat.core.id.OLATResourceable) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) VelocityContainer(org.olat.core.gui.components.velocity.VelocityContainer)

Example 57 with LocalFileImpl

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

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)

Example 58 with LocalFileImpl

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

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 59 with LocalFileImpl

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

the class MovieServiceImpl method isMP4.

@Override
public boolean isMP4(VFSLeaf media, String fileName) {
    File file = null;
    if (media instanceof VFSCPNamedItem) {
        media = ((VFSCPNamedItem) media).getDelegate();
    }
    if (media instanceof LocalFileImpl) {
        file = ((LocalFileImpl) media).getBasefile();
    }
    if (file == null) {
        return false;
    }
    String suffix = FileUtils.getFileSuffix(fileName);
    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);
            String fourCC = demuxer1.getVideoTrack().getFourcc();
            if (fourCCs.contains(fourCC.toLowerCase())) {
                return true;
            }
            log.info("Movie file::" + fileName + " has correct suffix::" + suffix + " but fourCC::" + fourCC + " not in our list of supported codecs.");
        } catch (Exception | Error e) {
        // anticipated exception, is not an mp4 file
        }
    }
    return false;
}
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 60 with LocalFileImpl

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

the class MovieServiceImpl method getSize.

@Override
public Size getSize(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 null;
    }
    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);
            org.jcodec.common.model.Size size = demuxer1.getMovie().getDisplaySize();
            // Case 1: standard case, get dimension from movie
            int w = size.getWidth();
            int h = size.getHeight();
            // Case 2: landscape movie from iOS: width and height is negative, no dunny why
            if (w < 0 && h < 0) {
                w = 0 - w;
                h = 0 - h;
            }
            if (w == 0) {
                // something in the track box.
                try {
                    // This code is the way it is just because I don't know
                    // how to safely read the rotation/portrait/landscape
                    // flag of the movie. Those mp4 guys are really
                    // secretive folks, did not find any documentation about
                    // this. Best guess.
                    org.jcodec.common.model.Size size2 = demuxer1.getVideoTrack().getBox().getCodedSize();
                    w = size2.getHeight();
                    h = size2.getWidth();
                } catch (Exception e) {
                    log.debug("can not get size from box " + e.getMessage());
                }
            }
            return new Size(w, h, false);
        } catch (Exception | AssertionError e) {
            log.error("Cannot extract size of: " + media, e);
        }
    } else if (suffix.equals("flv")) {
        try (InputStream stream = new FileInputStream(file)) {
            FLVParser infos = new FLVParser();
            infos.parse(stream);
            if (infos.getWidth() > 0 && infos.getHeight() > 0) {
                int w = infos.getWidth();
                int h = infos.getHeight();
                return new Size(w, h, false);
            }
        } catch (Exception e) {
            log.error("Cannot extract size of: " + media, e);
        }
    }
    return null;
}
Also used : MP4Demuxer(org.jcodec.containers.mp4.demuxer.MP4Demuxer) FileChannel(java.nio.channels.FileChannel) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) Size(org.olat.core.commons.services.image.Size) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileChannelWrapper(org.jcodec.common.FileChannelWrapper) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) FileInputStream(java.io.FileInputStream) VFSCPNamedItem(org.olat.ims.cp.ui.VFSCPNamedItem) RandomAccessFile(java.io.RandomAccessFile) FLVParser(org.olat.core.commons.services.video.spi.FLVParser) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

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