private File getCachedImage(File file, int size) throws IOException {
    String md5 = DigestUtils.md5Hex(file.getPath());
    File cachedImage = new File(getImageCacheDirectory(size), md5 + ".jpeg");

    // Is cache missing or obsolete?
    if (!cachedImage.exists() || FileUtil.lastModified(file) > cachedImage.lastModified()) {
      InputStream in = null;
      OutputStream out = null;
      try {
        in = getImageInputStream(file);
        out = new FileOutputStream(cachedImage);
        BufferedImage image = ImageIO.read(in);
        if (image == null) {
          throw new Exception("Unable to decode image.");
        }

        image = scale(image, size, size);
        ImageIO.write(image, "jpeg", out);

      } catch (Throwable x) {
        // Delete corrupt (probably empty) thumbnail cache.
        LOG.warn("Failed to create thumbnail for " + file, x);
        IOUtils.closeQuietly(out);
        cachedImage.delete();
        throw new IOException("Failed to create thumbnail for " + file + ". " + x.getMessage());

      } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
      }
    }
    return cachedImage;
  }
Ejemplo n.º 2
0
  public long getLastModified(HttpServletRequest request) {
    String path = request.getParameter("path");
    if (StringUtils.trimToNull(path) == null) {
      return 0;
    }

    File file = new File(path);
    if (!FileUtil.exists(file)) {
      return -1;
    }

    return FileUtil.lastModified(file);
  }
  public long getLastModified(HttpServletRequest request) {
    try {
      File file = getImageFile(request);
      if (file == null) {
        return 0; // Request for the default image.
      }
      if (!FileUtil.exists(file)) {
        return -1;
      }

      return FileUtil.lastModified(file);
    } catch (Exception e) {
      return -1;
    }
  }