Intermediate thumbnail size more web-friendly

This commit is contained in:
jendib 2013-08-18 01:26:34 +02:00
parent 6b5c1b2b51
commit dab6f4b9d1
7 changed files with 71 additions and 61 deletions

View File

@ -36,15 +36,19 @@ public class FileDeletedAsyncListener {
// Delete the file from storage // Delete the file from storage
File file = fileDeletedAsyncEvent.getFile(); File file = fileDeletedAsyncEvent.getFile();
java.io.File thumbnailFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_thumb").toFile();
java.io.File storedFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId()).toFile(); 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 (thumbnailFile.exists()) {
thumbnailFile.delete();
}
if (storedFile.exists()) { if (storedFile.exists()) {
storedFile.delete(); storedFile.delete();
} }
if (webFile.exists()) {
webFile.delete();
}
if (thumbnailFile.exists()) {
thumbnailFile.delete();
}
// Update Lucene index // Update Lucene index
LuceneDao luceneDao = new LuceneDao(); LuceneDao luceneDao = new LuceneDao();

View File

@ -1,9 +1,10 @@
package com.sismics.docs.core.util; package com.sismics.docs.core.util;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
@ -55,8 +56,7 @@ public class FileUtil {
} }
// Upscale and grayscale the image // Upscale and grayscale the image
BufferedImage resizedImage = Scalr.resize(image, Method.AUTOMATIC, Mode.AUTOMATIC, 3500, BufferedImage resizedImage = Scalr.resize(image, Method.AUTOMATIC, Mode.AUTOMATIC, 3500, Scalr.OP_ANTIALIAS, Scalr.OP_GRAYSCALE);
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null));
image.flush(); image.flush();
image = resizedImage; image = resizedImage;
@ -71,4 +71,26 @@ public class FileUtil {
return content; return content;
} }
/**
* 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 thumbnails
if (ImageUtil.isImage(file.getMimeType())) {
BufferedImage image = ImageIO.read(path.toFile());
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();
ImageUtil.writeJpeg(web, Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_web").toFile());
ImageUtil.writeJpeg(thumbnail, Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file.getId() + "_thumb").toFile());
}
}
} }

View File

@ -1,40 +0,0 @@
package com.sismics.util;
import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.util.DirectoryUtil;
import org.imgscalr.Scalr;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* 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());
}
}
}

View File

@ -1,6 +1,9 @@
- New image rescale between thumbnail and original (client/server) - New image rescale between thumbnail and original (client)
- Batch to regenerate all thumbnails (server) - Batch to regenerate all thumbnails (server)
- Special criteria to search inside OCR-ed content (eg. full:uranium) (server) - Special criteria to search inside OCR-ed content (eg. full:uranium) (server)
- Special criteria to search on a specific time span (eg. at:2013-06) (server) - Special criteria to search on a specific time span (eg. at:2013-06) (server)
- Show help on special criterias (client) - Show help on special criterias (client)
- Disable Add/Edit button while uploading (client) - Disable Add/Edit button while uploading (client)
- Extract text from PDF for indexing, see PDFBox (server)
- Make thumbnail of the first page of PDF, see PDFBox (server)

View File

@ -220,6 +220,9 @@ public class AppResource extends BaseResource {
java.io.File[] storedFileList = DirectoryUtil.getStorageDirectory().listFiles(); java.io.File[] storedFileList = DirectoryUtil.getStorageDirectory().listFiles();
for (java.io.File storedFile : storedFileList) { for (java.io.File storedFile : storedFileList) {
String fileName = storedFile.getName(); String fileName = storedFile.getName();
if (fileName.endsWith("_web")) {
fileName = fileName.replace("_web", "");
}
if (fileName.endsWith("_thumb")) { if (fileName.endsWith("_thumb")) {
fileName = fileName.replace("_thumb", ""); fileName = fileName.replace("_thumb", "");
} }

View File

@ -26,6 +26,7 @@ import javax.ws.rs.core.Response;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import com.google.common.collect.Lists;
import com.sismics.docs.core.dao.jpa.DocumentDao; import com.sismics.docs.core.dao.jpa.DocumentDao;
import com.sismics.docs.core.dao.jpa.FileDao; import com.sismics.docs.core.dao.jpa.FileDao;
import com.sismics.docs.core.dao.jpa.ShareDao; import com.sismics.docs.core.dao.jpa.ShareDao;
@ -35,11 +36,11 @@ import com.sismics.docs.core.model.context.AppContext;
import com.sismics.docs.core.model.jpa.Document; import com.sismics.docs.core.model.jpa.Document;
import com.sismics.docs.core.model.jpa.File; import com.sismics.docs.core.model.jpa.File;
import com.sismics.docs.core.util.DirectoryUtil; import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.docs.core.util.FileUtil;
import com.sismics.rest.exception.ClientException; import com.sismics.rest.exception.ClientException;
import com.sismics.rest.exception.ForbiddenClientException; import com.sismics.rest.exception.ForbiddenClientException;
import com.sismics.rest.exception.ServerException; import com.sismics.rest.exception.ServerException;
import com.sismics.rest.util.ValidationUtil; import com.sismics.rest.util.ValidationUtil;
import com.sismics.util.FileUtil;
import com.sismics.util.ImageUtil; import com.sismics.util.ImageUtil;
import com.sismics.util.mime.MimeTypeUtil; import com.sismics.util.mime.MimeTypeUtil;
import com.sun.jersey.multipart.FormDataBodyPart; import com.sun.jersey.multipart.FormDataBodyPart;
@ -275,9 +276,15 @@ public class FileResource extends BaseResource {
public Response data( public Response data(
@PathParam("id") final String fileId, @PathParam("id") final String fileId,
@QueryParam("share") String shareId, @QueryParam("share") String shareId,
@QueryParam("thumbnail") boolean thumbnail) throws JSONException { @QueryParam("size") String size) throws JSONException {
authenticate(); authenticate();
if (size != null) {
if (!Lists.newArrayList("web", "thumb").contains(size)) {
throw new ClientException("SizeError", "Size must be web or thumb");
}
}
// Get the file // Get the file
FileDao fileDao = new FileDao(); FileDao fileDao = new FileDao();
DocumentDao documentDao = new DocumentDao(); DocumentDao documentDao = new DocumentDao();
@ -298,9 +305,9 @@ public class FileResource extends BaseResource {
// Get the stored file // Get the stored file
java.io.File storedfile; java.io.File storedfile;
if (thumbnail) { if (size != null) {
if (ImageUtil.isImage(file.getMimeType())) { if (ImageUtil.isImage(file.getMimeType())) {
storedfile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), fileId + "_thumb").toFile(); storedfile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), fileId + "_" + size).toFile();
} else { } else {
storedfile = new java.io.File(getClass().getResource("/image/file.png").getFile()); storedfile = new java.io.File(getClass().getResource("/image/file.png").getFile());
} }

View File

@ -84,9 +84,7 @@ public class TestFileResource extends BaseJerseyTest {
// Get the file data // Get the file data
fileResource = resource().path("/file/" + file1Id + "/data"); fileResource = resource().path("/file/" + file1Id + "/data");
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken)); fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));
MultivaluedMapImpl getParams = new MultivaluedMapImpl(); response = fileResource.get(ClientResponse.class);
getParams.putSingle("thumbnail", false);
response = fileResource.queryParams(getParams).get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
InputStream is = response.getEntityInputStream(); InputStream is = response.getEntityInputStream();
byte[] fileBytes = ByteStreams.toByteArray(is); byte[] fileBytes = ByteStreams.toByteArray(is);
@ -95,14 +93,25 @@ public class TestFileResource extends BaseJerseyTest {
// Get the thumbnail data // Get the thumbnail data
fileResource = resource().path("/file/" + file1Id + "/data"); fileResource = resource().path("/file/" + file1Id + "/data");
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken)); fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));
getParams = new MultivaluedMapImpl(); MultivaluedMapImpl getParams = new MultivaluedMapImpl();
getParams.putSingle("thumbnail", true); getParams.putSingle("size", "thumb");
response = fileResource.queryParams(getParams).get(ClientResponse.class); response = fileResource.queryParams(getParams).get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
is = response.getEntityInputStream(); is = response.getEntityInputStream();
fileBytes = ByteStreams.toByteArray(is); fileBytes = ByteStreams.toByteArray(is);
Assert.assertEquals(41935, fileBytes.length); Assert.assertEquals(41935, fileBytes.length);
// Get the web data
fileResource = resource().path("/file/" + file1Id + "/data");
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));
getParams = new MultivaluedMapImpl();
getParams.putSingle("size", "web");
response = fileResource.queryParams(getParams).get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
is = response.getEntityInputStream();
fileBytes = ByteStreams.toByteArray(is);
Assert.assertEquals(551084, fileBytes.length);
// Get all files from a document // Get all files from a document
fileResource = resource().path("/file/list"); fileResource = resource().path("/file/list");
fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken)); fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken));
@ -148,10 +157,12 @@ public class TestFileResource extends BaseJerseyTest {
Assert.assertEquals("ok", json.getString("status")); Assert.assertEquals("ok", json.getString("status"));
// Check that files are deleted from FS // Check that files are deleted from FS
java.io.File thumbnailFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file1Id + "_thumb").toFile();
java.io.File storedFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file1Id).toFile(); java.io.File storedFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file1Id).toFile();
Assert.assertFalse(thumbnailFile.exists()); java.io.File webFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file1Id + "_web").toFile();
java.io.File thumbnailFile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), file1Id + "_thumb").toFile();
Assert.assertFalse(storedFile.exists()); Assert.assertFalse(storedFile.exists());
Assert.assertFalse(webFile.exists());
Assert.assertFalse(thumbnailFile.exists());
// Get all files from a document // Get all files from a document
fileResource = resource().path("/file/list"); fileResource = resource().path("/file/list");