Search in sources :

Example 1 with LocalFileImpl

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

the class MetaInfoFileImpl method getThumbnailInfo.

private Thumbnail getThumbnailInfo(int maxWidth, int maxHeight, boolean fill) {
    for (Thumbnail thumbnail : thumbnails) {
        if (maxHeight == thumbnail.getMaxHeight() && maxWidth == thumbnail.getMaxWidth()) {
            if (thumbnail.exists()) {
                return thumbnail;
            }
        }
    }
    // generate a file name
    File metaLoc = metaFile.getParentFile();
    String name = originFile.getName();
    String extension = FileUtils.getFileSuffix(name);
    String nameOnly = name.substring(0, name.length() - extension.length() - 1);
    String randuuid = UUID.randomUUID().toString();
    String thumbnailExtension = preferedThumbnailType(extension);
    File thumbnailFile = new File(metaLoc, nameOnly + "_" + randuuid + "_" + maxHeight + "x" + maxWidth + (fill ? "xfill" : "") + "." + thumbnailExtension);
    // generate thumbnail
    long start = 0l;
    if (log.isDebug())
        start = System.currentTimeMillis();
    VFSLeaf thumbnailLeaf = new LocalFileImpl(thumbnailFile);
    VFSLeaf originLeaf = new LocalFileImpl(originFile);
    if (thumbnailService != null && thumbnailService.isThumbnailPossible(thumbnailLeaf)) {
        try {
            if (thumbnails.isEmpty()) {
                // be paranoid
                cannotGenerateThumbnail = true;
                write();
            }
            log.info("Start thumbnail: " + thumbnailLeaf);
            FinalSize finalSize = thumbnailService.generateThumbnail(originLeaf, thumbnailLeaf, maxWidth, maxHeight, fill);
            if (finalSize == null) {
                return null;
            } else {
                Thumbnail thumbnail = new Thumbnail();
                thumbnail.setMaxHeight(maxHeight);
                thumbnail.setMaxWidth(maxWidth);
                thumbnail.setFinalHeight(finalSize.getHeight());
                thumbnail.setFinalWidth(finalSize.getWidth());
                thumbnail.setFill(true);
                thumbnail.setThumbnailFile(thumbnailFile);
                thumbnails.add(thumbnail);
                cannotGenerateThumbnail = false;
                write();
                log.info("Create thumbnail: " + thumbnailLeaf);
                if (log.isDebug()) {
                    log.debug("Creation of thumbnail takes (ms): " + (System.currentTimeMillis() - start));
                }
                return thumbnail;
            }
        } catch (CannotGenerateThumbnailException e) {
            // don't try every time to create the thumbnail.
            cannotGenerateThumbnail = true;
            write();
            return null;
        }
    }
    return null;
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) File(java.io.File)

Example 2 with LocalFileImpl

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

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)

Example 3 with LocalFileImpl

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

the class MovieServiceImpl method generateThumbnail.

@Override
public FinalSize generateThumbnail(VFSLeaf file, VFSLeaf thumbnailFile, int maxWidth, int maxHeight, boolean fill) throws CannotGenerateThumbnailException {
    FinalSize size = null;
    if (file instanceof LocalFileImpl && thumbnailFile instanceof LocalFileImpl) {
        try {
            WorkThreadInformations.setInfoFiles(null, file);
            WorkThreadInformations.set("Generate thumbnail (video) VFSLeaf=" + file);
            File baseFile = ((LocalFileImpl) file).getBasefile();
            File scaledImage = ((LocalFileImpl) thumbnailFile).getBasefile();
            BufferedImage frame = FrameGrab.getFrame(baseFile, 20);
            Size scaledSize = ImageHelperImpl.calcScaledSize(frame, maxWidth, maxHeight);
            if (ImageHelperImpl.writeTo(frame, scaledImage, scaledSize, "jpeg")) {
                size = new FinalSize(scaledSize.getWidth(), scaledSize.getHeight());
            }
        // NullPointerException can be thrown if the jcodec cannot handle the codec of the movie
        // ArrayIndexOutOfBoundsException
        } catch (Exception | AssertionError e) {
            log.error("", e);
        } finally {
            WorkThreadInformations.unset();
        }
    }
    return size;
}
Also used : FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) Size(org.olat.core.commons.services.image.Size) FinalSize(org.olat.core.commons.services.thumbnail.FinalSize) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)

Example 4 with LocalFileImpl

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

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

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

the class FileElementImpl method evalFormRequest.

@Override
public void evalFormRequest(UserRequest ureq) {
    Form form = getRootForm();
    String dispatchuri = form.getRequestParameter("dispatchuri");
    if (dispatchuri != null && dispatchuri.equals(component.getFormDispatchId())) {
        if ("delete".equals(form.getRequestParameter("delete"))) {
            if (isConfirmDelete()) {
                doConfirmDelete(ureq);
            } else {
                getRootForm().fireFormEvent(ureq, new FileElementEvent(FileElementEvent.DELETE, this, FormEvent.ONCLICK));
            }
        }
    }
    Set<String> keys = form.getRequestMultipartFilesSet();
    if (keys.size() > 0 && keys.contains(component.getFormDispatchId())) {
        // Remove old files first
        if (tempUploadFile != null && tempUploadFile.exists()) {
            tempUploadFile.delete();
        }
        // Move file from a temporary request scope location to a location
        // with a
        // temporary form item scope. The file must be moved later using the
        // moveUploadFileTo() method to the final destination.
        tempUploadFile = new File(WebappHelper.getTmpDir(), CodeHelper.getUniqueID());
        File tmpRequestFile = form.getRequestMultipartFile(component.getFormDispatchId());
        // Move file to internal temp location
        boolean success = tmpRequestFile.renameTo(tempUploadFile);
        if (!success) {
            // try to move file by copying it, command above might fail
            // when source and target are on different volumes
            FileUtils.copyFileToFile(tmpRequestFile, tempUploadFile, true);
        }
        uploadFilename = form.getRequestMultipartFileName(component.getFormDispatchId());
        // prevent an issue with Firefox
        uploadFilename = FileUtils.normalizeFilenameWithSuffix(uploadFilename);
        // use mime-type from file name to have deterministic mime types
        uploadMimeType = WebappHelper.getMimeType(uploadFilename);
        if (uploadMimeType == null) {
            // use browser mime type as fallback if unknown
            uploadMimeType = form.getRequestMultipartFileMimeType(component.getFormDispatchId());
        }
        if (uploadMimeType == null) {
            // use application fallback for worst case
            uploadMimeType = "application/octet-stream";
        }
        if (previewEl != null && uploadMimeType != null && (uploadMimeType.startsWith("image/") || uploadMimeType.startsWith("video/"))) {
            VFSLeaf media = new LocalFileImpl(tempUploadFile);
            previewEl.setMedia(media, uploadMimeType);
            previewEl.setCropSelectionEnabled(cropSelectionEnabled);
            previewEl.setMaxWithAndHeightToFitWithin(300, 200);
            previewEl.setVisible(true);
        } else if (previewEl != null) {
            previewEl.setVisible(false);
        }
        // Mark associated component dirty, that it gets rerendered
        component.setDirty(true);
    }
}
Also used : VFSLeaf(org.olat.core.util.vfs.VFSLeaf) Form(org.olat.core.gui.components.form.flexible.impl.Form) LocalFileImpl(org.olat.core.util.vfs.LocalFileImpl) 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