Search in sources :

Example 1 with CannotGenerateThumbnailException

use of org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException 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 CannotGenerateThumbnailException

use of org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException 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 3 with CannotGenerateThumbnailException

use of org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException in project openolat by klemens.

the class ImageHelperImpl method thumbnailPDF.

@Override
public Size thumbnailPDF(VFSLeaf pdfFile, VFSLeaf thumbnailFile, int maxWidth, int maxHeight) {
    InputStream in = null;
    PDDocument document = null;
    try {
        WorkThreadInformations.setInfoFiles(null, pdfFile);
        WorkThreadInformations.set("Generate thumbnail VFSLeaf=" + pdfFile);
        in = pdfFile.getInputStream();
        document = PDDocument.load(in);
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (Exception e) {
                log.info("PDF document is encrypted: " + pdfFile);
                throw new CannotGenerateThumbnailException("PDF document is encrypted: " + pdfFile);
            }
        }
        List pages = document.getDocumentCatalog().getAllPages();
        PDPage page = (PDPage) pages.get(0);
        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_BGR, 72);
        Size size = scaleImage(image, thumbnailFile, maxWidth, maxHeight);
        if (size != null) {
            return size;
        }
        return null;
    } catch (CannotGenerateThumbnailException e) {
        return null;
    } catch (Exception e) {
        log.warn("Unable to create image from pdf file.", e);
        return null;
    } finally {
        WorkThreadInformations.unset();
        FileUtils.closeSafely(in);
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
            // only a try, fail silently
            }
        }
    }
}
Also used : CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) PDPage(org.apache.pdfbox.pdmodel.PDPage) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileInputStream(java.io.FileInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) Size(org.olat.core.commons.services.image.Size) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) List(java.util.List) IOException(java.io.IOException) CMMException(java.awt.color.CMMException) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 4 with CannotGenerateThumbnailException

use of org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException in project openolat by klemens.

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

use of org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException in project OpenOLAT by OpenOLAT.

the class ImageHelperImpl method thumbnailPDF.

@Override
public Size thumbnailPDF(VFSLeaf pdfFile, VFSLeaf thumbnailFile, int maxWidth, int maxHeight) {
    InputStream in = null;
    PDDocument document = null;
    try {
        WorkThreadInformations.setInfoFiles(null, pdfFile);
        WorkThreadInformations.set("Generate thumbnail VFSLeaf=" + pdfFile);
        in = pdfFile.getInputStream();
        document = PDDocument.load(in);
        if (document.isEncrypted()) {
            try {
                document.decrypt("");
            } catch (Exception e) {
                log.info("PDF document is encrypted: " + pdfFile);
                throw new CannotGenerateThumbnailException("PDF document is encrypted: " + pdfFile);
            }
        }
        List pages = document.getDocumentCatalog().getAllPages();
        PDPage page = (PDPage) pages.get(0);
        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_BGR, 72);
        Size size = scaleImage(image, thumbnailFile, maxWidth, maxHeight);
        if (size != null) {
            return size;
        }
        return null;
    } catch (CannotGenerateThumbnailException e) {
        return null;
    } catch (Exception e) {
        log.warn("Unable to create image from pdf file.", e);
        return null;
    } finally {
        WorkThreadInformations.unset();
        FileUtils.closeSafely(in);
        if (document != null) {
            try {
                document.close();
            } catch (IOException e) {
            // only a try, fail silently
            }
        }
    }
}
Also used : CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) PDPage(org.apache.pdfbox.pdmodel.PDPage) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileInputStream(java.io.FileInputStream) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) Size(org.olat.core.commons.services.image.Size) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) List(java.util.List) IOException(java.io.IOException) CMMException(java.awt.color.CMMException) CannotGenerateThumbnailException(org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Aggregations

CannotGenerateThumbnailException (org.olat.core.commons.services.thumbnail.CannotGenerateThumbnailException)6 BufferedImage (java.awt.image.BufferedImage)4 File (java.io.File)4 Size (org.olat.core.commons.services.image.Size)4 FinalSize (org.olat.core.commons.services.thumbnail.FinalSize)4 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)4 CMMException (java.awt.color.CMMException)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 List (java.util.List)2 FileImageInputStream (javax.imageio.stream.FileImageInputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)2