diff --git a/src/main/java/org/forkalsrud/album/video/MovieCoder.java b/src/main/java/org/forkalsrud/album/video/MovieCoder.java index 2a1e431..7fb7df2 100644 --- a/src/main/java/org/forkalsrud/album/video/MovieCoder.java +++ b/src/main/java/org/forkalsrud/album/video/MovieCoder.java @@ -16,8 +16,8 @@ import java.util.List; import java.util.Map; import org.apache.commons.io.IOUtils; -import org.forkalsrud.album.db.MovieDatabase; import org.forkalsrud.album.db.Chunk; +import org.forkalsrud.album.db.MovieDatabase; import org.forkalsrud.album.exif.Dimension; import org.forkalsrud.album.exif.Thumbnail; import org.forkalsrud.album.web.CachedImage; @@ -64,7 +64,7 @@ public class MovieCoder { stdin.close(); InputStream stdout = p.getInputStream(); String searchPath = IOUtils.toString(stdout); - int returnStatus = p.waitFor(); + p.waitFor(); String separator = System.getProperty("path.separator"); if (searchPath != null && separator != null && !"".equals(separator)) { @@ -100,7 +100,7 @@ public class MovieCoder { Process p = pb.start(); p.getOutputStream().close(); List lines = IOUtils.readLines(p.getInputStream()); - int returnStatus = p.waitFor(); + p.waitFor(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss"); String width = "", height = ""; for (String line : lines) { @@ -153,9 +153,7 @@ public class MovieCoder { Process p = pb.start(); p.getOutputStream().close(); log.debug(IOUtils.toString(p.getInputStream())); - int returnStatus = p.waitFor(); - - String key = file.getPath() + ":" + secondNo + ":" + size; + p.waitFor(); CachedImage ci = pictureScaler.scalePicture(frame, thumbnail, size); return ci; } finally { @@ -265,6 +263,7 @@ public class MovieCoder { * .mp4 */ + @Override public void run() { try { @@ -353,7 +352,8 @@ public class MovieCoder { public void stream(File file, Thumbnail thumbnail, String size, OutputStream out) throws IOException, InterruptedException { Dimension targetSize = thumbnail.getSize().scale(size); -// new EncodingProcess(file, thumbnail, targetSize).streamTo(out); + new EncodingProcess(file, thumbnail, targetSize).streamTo(out); + /* String key = file.getPath() + ":" + targetSize.getWidth(); int chunkNo = 0; boolean done = false; @@ -364,6 +364,7 @@ public class MovieCoder { } out.write(chunk.bits); } + */ } } diff --git a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java index 76c4c4e..21970de 100644 --- a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java +++ b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java @@ -296,11 +296,41 @@ public class AlbumServlet } void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { + + File file = entry.getPath(); + if (notModified(req, file)) { + res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); + res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days + log.info(file.getName() + " not modified (based on date)"); + return; + } + int secondNo = 3; + String size = req.getParameter("size"); + if (size == null) { + size = "250"; + } + String key = file.getPath() + ":" + secondNo + ":" + size; + CachedImage cimg = thumbDb.load(key); + if (cimg != null) { + if (cimg.lastModified == file.lastModified()) { + log.info("cache hit on " + key); + } else { + log.info(" " + key + " has changed so cache entry wil be refreshed"); + cimg = null; + } + } + if (cimg == null) { + try { + cimg = movieCoder.extractFrame(file, secondNo, entry.getThumbnail(), size); + thumbDb.store(key, cimg); + log.info(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + thumbDb.size() + " entries"); + } catch (Exception e) { + throw new RuntimeException("sadness", e); + } + } try { - String size = req.getParameter("size"); - CachedImage cimg = movieCoder.extractFrame(entry.getPath(), 3, entry.getThumbnail(), size != null ? size : "250"); res.setStatus(HttpServletResponse.SC_OK); - res.setDateHeader("Last-Modified", entry.getPath().lastModified()); + res.setDateHeader("Last-Modified", file.lastModified()); res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days res.setContentType(cimg.mimeType); res.setContentLength(cimg.bits.length); diff --git a/src/main/java/org/forkalsrud/album/web/PictureScaler.java b/src/main/java/org/forkalsrud/album/web/PictureScaler.java index 91207e3..2908a85 100644 --- a/src/main/java/org/forkalsrud/album/web/PictureScaler.java +++ b/src/main/java/org/forkalsrud/album/web/PictureScaler.java @@ -86,7 +86,8 @@ public class PictureScaler { } - public CachedImage call() throws Exception { + @Override + public CachedImage call() throws Exception { return scalePictureReally(file, thumbnail, size); } @@ -108,7 +109,8 @@ public class PictureScaler { return new Comparator() { - public int compare(PictureRequest o1, PictureRequest o2) { + @Override + public int compare(PictureRequest o1, PictureRequest o2) { return Long.signum(o1.priority - o2.priority); } };