1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 17:56:20 +00:00

Batch to regenerate all file variations

This commit is contained in:
jendib
2013-08-18 02:12:48 +02:00
parent dab6f4b9d1
commit fd95ecc4cb
5 changed files with 117 additions and 30 deletions

View File

@@ -1,7 +1,5 @@
package com.sismics.docs.core.listener.async;
import java.nio.file.Paths;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -9,7 +7,7 @@ import com.google.common.eventbus.Subscribe;
import com.sismics.docs.core.dao.lucene.LuceneDao;
import com.sismics.docs.core.event.FileDeletedAsyncEvent;
import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.docs.core.util.FileUtil;
/**
* Listener on file deleted.
@@ -36,19 +34,7 @@ public class FileDeletedAsyncListener {
// Delete the file from storage
File file = fileDeletedAsyncEvent.getFile();
java.io.File storedFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId()).toFile();
java.io.File webFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_web").toFile();
java.io.File thumbnailFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_thumb").toFile();
if (storedFile.exists()) {
storedFile.delete();
}
if (webFile.exists()) {
webFile.delete();
}
if (thumbnailFile.exists()) {
thumbnailFile.delete();
}
FileUtil.delete(file);
// Update Lucene index
LuceneDao luceneDao = new LuceneDao();

View File

@@ -77,15 +77,26 @@ public class FileUtil {
*
* @param is InputStream
* @param file File to save
* @throws Exception
* @throws IOException
*/
public static void save(InputStream is, File file) throws Exception {
public static void save(InputStream is, File file) throws IOException {
Path path = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId());
Files.copy(is, path);
// In case of image, save thumbnails
// Generate file variations
saveVariations(file, path.toFile());
}
/**
* Generate file variations.
*
* @param file File from database
* @param originalFile Original file
* @throws IOException
*/
public static void saveVariations(File file, java.io.File originalFile) throws IOException {
if (ImageUtil.isImage(file.getMimeType())) {
BufferedImage image = ImageIO.read(path.toFile());
BufferedImage image = ImageIO.read(originalFile);
BufferedImage web = Scalr.resize(image, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, 1280, Scalr.OP_ANTIALIAS);
BufferedImage thumbnail = Scalr.resize(image, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, 256, Scalr.OP_ANTIALIAS);
image.flush();
@@ -93,4 +104,25 @@ public class FileUtil {
ImageUtil.writeJpeg(thumbnail, Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_thumb").toFile());
}
}
/**
* Remove a file from the storage filesystem.
*
* @param file File to delete
*/
public static void delete(File file) {
java.io.File storedFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId()).toFile();
java.io.File webFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_web").toFile();
java.io.File thumbnailFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_thumb").toFile();
if (storedFile.exists()) {
storedFile.delete();
}
if (webFile.exists()) {
webFile.delete();
}
if (thumbnailFile.exists()) {
thumbnailFile.delete();
}
}
}