From dab6f4b9d1684e7eacae48dc33af88f55acc7ab6 Mon Sep 17 00:00:00 2001 From: jendib Date: Sun, 18 Aug 2013 01:26:34 +0200 Subject: [PATCH] Intermediate thumbnail size more web-friendly --- .../async/FileDeletedAsyncListener.java | 12 ++++-- .../com/sismics/docs/core/util/FileUtil.java | 30 ++++++++++++-- .../main/java/com/sismics/util/FileUtil.java | 40 ------------------- docs-parent/TODO | 7 +++- .../docs/rest/resource/AppResource.java | 3 ++ .../docs/rest/resource/FileResource.java | 15 +++++-- .../sismics/docs/rest/TestFileResource.java | 25 ++++++++---- 7 files changed, 71 insertions(+), 61 deletions(-) delete mode 100644 docs-core/src/main/java/com/sismics/util/FileUtil.java diff --git a/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileDeletedAsyncListener.java b/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileDeletedAsyncListener.java index 5df9caf1..639b6e46 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileDeletedAsyncListener.java +++ b/docs-core/src/main/java/com/sismics/docs/core/listener/async/FileDeletedAsyncListener.java @@ -36,15 +36,19 @@ public class FileDeletedAsyncListener { // Delete the file from storage 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 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()) { storedFile.delete(); } + if (webFile.exists()) { + webFile.delete(); + } + if (thumbnailFile.exists()) { + thumbnailFile.delete(); + } // Update Lucene index LuceneDao luceneDao = new LuceneDao(); diff --git a/docs-core/src/main/java/com/sismics/docs/core/util/FileUtil.java b/docs-core/src/main/java/com/sismics/docs/core/util/FileUtil.java index 89e5c2d6..6cf7e99c 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/util/FileUtil.java +++ b/docs-core/src/main/java/com/sismics/docs/core/util/FileUtil.java @@ -1,9 +1,10 @@ package com.sismics.docs.core.util; -import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; -import java.awt.image.ColorConvertOp; import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import javax.imageio.ImageIO; @@ -55,8 +56,7 @@ public class FileUtil { } // Upscale and grayscale the image - BufferedImage resizedImage = Scalr.resize(image, Method.AUTOMATIC, Mode.AUTOMATIC, 3500, - new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null)); + BufferedImage resizedImage = Scalr.resize(image, Method.AUTOMATIC, Mode.AUTOMATIC, 3500, Scalr.OP_ANTIALIAS, Scalr.OP_GRAYSCALE); image.flush(); image = resizedImage; @@ -71,4 +71,26 @@ public class FileUtil { 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()); + } + } } diff --git a/docs-core/src/main/java/com/sismics/util/FileUtil.java b/docs-core/src/main/java/com/sismics/util/FileUtil.java deleted file mode 100644 index f6d4527c..00000000 --- a/docs-core/src/main/java/com/sismics/util/FileUtil.java +++ /dev/null @@ -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()); - } - } -} diff --git a/docs-parent/TODO b/docs-parent/TODO index b4b67624..a1c1191a 100644 --- a/docs-parent/TODO +++ b/docs-parent/TODO @@ -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) - 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) - Show help on special criterias (client) -- Disable Add/Edit button while uploading (client) \ No newline at end of file +- 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) \ No newline at end of file diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java index 98cbd081..65cd0cd0 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/AppResource.java @@ -220,6 +220,9 @@ public class AppResource extends BaseResource { java.io.File[] storedFileList = DirectoryUtil.getStorageDirectory().listFiles(); for (java.io.File storedFile : storedFileList) { String fileName = storedFile.getName(); + if (fileName.endsWith("_web")) { + fileName = fileName.replace("_web", ""); + } if (fileName.endsWith("_thumb")) { fileName = fileName.replace("_thumb", ""); } diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java index db3eafa7..207d1fc6 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/FileResource.java @@ -26,6 +26,7 @@ import javax.ws.rs.core.Response; import org.codehaus.jettison.json.JSONException; 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.FileDao; 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.File; 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.ForbiddenClientException; import com.sismics.rest.exception.ServerException; import com.sismics.rest.util.ValidationUtil; -import com.sismics.util.FileUtil; import com.sismics.util.ImageUtil; import com.sismics.util.mime.MimeTypeUtil; import com.sun.jersey.multipart.FormDataBodyPart; @@ -275,9 +276,15 @@ public class FileResource extends BaseResource { public Response data( @PathParam("id") final String fileId, @QueryParam("share") String shareId, - @QueryParam("thumbnail") boolean thumbnail) throws JSONException { + @QueryParam("size") String size) throws JSONException { authenticate(); + if (size != null) { + if (!Lists.newArrayList("web", "thumb").contains(size)) { + throw new ClientException("SizeError", "Size must be web or thumb"); + } + } + // Get the file FileDao fileDao = new FileDao(); DocumentDao documentDao = new DocumentDao(); @@ -298,9 +305,9 @@ public class FileResource extends BaseResource { // Get the stored file java.io.File storedfile; - if (thumbnail) { + if (size != null) { if (ImageUtil.isImage(file.getMimeType())) { - storedfile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), fileId + "_thumb").toFile(); + storedfile = Paths.get(DirectoryUtil.getStorageDirectory().getPath(), fileId + "_" + size).toFile(); } else { storedfile = new java.io.File(getClass().getResource("/image/file.png").getFile()); } diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java index 556e884a..b2d759f6 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestFileResource.java @@ -84,9 +84,7 @@ public class TestFileResource extends BaseJerseyTest { // Get the file data fileResource = resource().path("/file/" + file1Id + "/data"); fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken)); - MultivaluedMapImpl getParams = new MultivaluedMapImpl(); - getParams.putSingle("thumbnail", false); - response = fileResource.queryParams(getParams).get(ClientResponse.class); + response = fileResource.get(ClientResponse.class); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); InputStream is = response.getEntityInputStream(); byte[] fileBytes = ByteStreams.toByteArray(is); @@ -95,14 +93,25 @@ public class TestFileResource extends BaseJerseyTest { // Get the thumbnail data fileResource = resource().path("/file/" + file1Id + "/data"); fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken)); - getParams = new MultivaluedMapImpl(); - getParams.putSingle("thumbnail", true); + MultivaluedMapImpl getParams = new MultivaluedMapImpl(); + getParams.putSingle("size", "thumb"); response = fileResource.queryParams(getParams).get(ClientResponse.class); Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); is = response.getEntityInputStream(); fileBytes = ByteStreams.toByteArray(is); 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 fileResource = resource().path("/file/list"); fileResource.addFilter(new CookieAuthenticationFilter(file1AuthenticationToken)); @@ -148,10 +157,12 @@ public class TestFileResource extends BaseJerseyTest { Assert.assertEquals("ok", json.getString("status")); // 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(); - 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(webFile.exists()); + Assert.assertFalse(thumbnailFile.exists()); // Get all files from a document fileResource = resource().path("/file/list");