mirror of
https://github.com/sismics/docs.git
synced 2024-11-25 07:07:57 +01:00
Default thumbnail for PDF
This commit is contained in:
parent
471933ca8c
commit
51434752f5
42
docs-core/src/main/java/com/sismics/util/FileUtil.java
Normal file
42
docs-core/src/main/java/com/sismics/util/FileUtil.java
Normal file
@ -0,0 +1,42 @@
|
||||
package com.sismics.util;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.imgscalr.Scalr;
|
||||
|
||||
import com.sismics.docs.core.model.jpa.File;
|
||||
import com.sismics.docs.core.util.DirectoryUtil;
|
||||
|
||||
/**
|
||||
* File utilities.
|
||||
*
|
||||
* @author bgamard
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
/**
|
||||
* Save a file on the storage filesystem.
|
||||
*
|
||||
* @param is InputStream
|
||||
* @param file File to save
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void save(InputStream is, File file) throws Exception {
|
||||
Path path = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId());
|
||||
Files.copy(is, path);
|
||||
|
||||
// In case of image, save a thumbnail
|
||||
if (ImageUtil.isImage(file.getMimeType())) {
|
||||
BufferedImage image = ImageIO.read(path.toFile());
|
||||
BufferedImage resizedImage = Scalr.resize(image, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, 256, Scalr.OP_ANTIALIAS);
|
||||
image.flush();
|
||||
ImageUtil.writeJpeg(resizedImage, Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_thumb").toFile());
|
||||
}
|
||||
}
|
||||
}
|
65
docs-core/src/main/java/com/sismics/util/ImageUtil.java
Normal file
65
docs-core/src/main/java/com/sismics/util/ImageUtil.java
Normal file
@ -0,0 +1,65 @@
|
||||
package com.sismics.util;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
import javax.imageio.IIOImage;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageWriteParam;
|
||||
import javax.imageio.ImageWriter;
|
||||
import javax.imageio.stream.FileImageOutputStream;
|
||||
|
||||
import com.sismics.util.mime.MimeType;
|
||||
|
||||
/**
|
||||
* Image processing utilities.
|
||||
*
|
||||
* @author jtremeaux
|
||||
*/
|
||||
public class ImageUtil {
|
||||
|
||||
/**
|
||||
* Write a high quality JPEG.
|
||||
*
|
||||
* @param image
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void writeJpeg(BufferedImage image, File file) throws IOException {
|
||||
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
|
||||
ImageWriter writer = null;
|
||||
FileImageOutputStream output = null;
|
||||
try {
|
||||
writer = (ImageWriter) iter.next();
|
||||
ImageWriteParam iwp = writer.getDefaultWriteParam();
|
||||
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
|
||||
iwp.setCompressionQuality(1.f);
|
||||
output = new FileImageOutputStream(file);
|
||||
writer.setOutput(output);
|
||||
IIOImage iioImage = new IIOImage(image, null, null);
|
||||
writer.write(null, iioImage, iwp);
|
||||
} finally {
|
||||
if (output != null) {
|
||||
try {
|
||||
output.close();
|
||||
} catch (Exception inner) {
|
||||
// NOP
|
||||
}
|
||||
}
|
||||
if (writer != null) {
|
||||
writer.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this MIME type is an image.
|
||||
* @param mimeType MIME type
|
||||
* @return True if image
|
||||
*/
|
||||
public static boolean isImage(String mimeType) {
|
||||
return mimeType.equals(MimeType.IMAGE_GIF) || mimeType.equals(MimeType.IMAGE_PNG) || mimeType.equals(MimeType.IMAGE_JPEG);
|
||||
}
|
||||
}
|
BIN
docs-core/src/main/resources/image/file.png
Normal file
BIN
docs-core/src/main/resources/image/file.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.4 KiB |
Loading…
Reference in New Issue
Block a user