Esempio n. 1
0
 private boolean supportWebp(HttpServletRequest request, String name) {
   Cookie cookie = WebUtils.getCookie(request, WEBP_SUPPORT_COOKIE);
   if (cookie != null && "true".equalsIgnoreCase(cookie.getValue())) {
     String ext = Files.getFileExtension(name);
     return "jpg".equalsIgnoreCase(ext)
         || "jpeg".equalsIgnoreCase(ext)
         || "png".equalsIgnoreCase(ext);
   }
   return false;
 }
Esempio n. 2
0
 @RequestMapping(value = "{storeId}/{y}/{m}/{d}/{name}/{ext}/{size}", method = RequestMethod.GET)
 public void write(
     @PathVariable("storeId") int storeId,
     @PathVariable("y") String y,
     @PathVariable("m") String m,
     @PathVariable("d") String d,
     @PathVariable("name") String name,
     @PathVariable("ext") String ext,
     @PathVariable("size") String size,
     ServletWebRequest request,
     HttpServletResponse response)
     throws MyFileNotFoundException {
   String path =
       File.separator
           + y
           + File.separator
           + m
           + File.separator
           + d
           + File.separator
           + name
           + "."
           + ext;
   if (!Webs.isSafeFilePath(path)) {
     throw new InvalidParamException();
   }
   FileWriteConfig config = getFileWriteConfig(name);
   RequestMatcher matcher = config.getRequestMatcher();
   // 防盗链
   if (matcher != null && !matcher.matches(request.getRequest())) {
     throw new MyFileNotFoundException();
   }
   LocalFileStorage store = seek(storeId);
   File seek = store.seek(path);
   boolean isImage = Webs.isWebImage("." + ext);
   if (!isImage) {
     response.setContentLength((int) seek.length());
     response.setStatus(HttpServletResponse.SC_OK);
     response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
     response.setHeader("Content-Disposition", "attachment;filename=" + seek.getName());
     try {
       OutputStream out = response.getOutputStream();
       FileUtils.copyFile(seek, out);
     } catch (IOException e) {
     }
     return;
   }
   if (!isModified(request)) {
     response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
     return;
   }
   StringBuilder sb = new StringBuilder(imageCacheDir);
   Resize format = parseSize(size);
   ImageZoomMatcher zm = config.getZoomMatcher();
   boolean zoom = format != null && zm.zoom(format.getSize(), seek) && needZoom(seek, format);
   String _path = path;
   if (zoom) {
     _path = Files.appendFilename(_path, "_", format.getSize());
   }
   sb.append(_path);
   boolean supportWebp = (this.supportWebp && supportWebp(request.getRequest(), path));
   if (supportWebp) {
     sb.append(".").append(WEBP);
   }
   String absPath = sb.toString();
   String etag = Webs.generatorETag(absPath);
   if (request.checkNotModified(etag)) {
     response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
     return;
   }
   File file = new File(absPath);
   if (!file.exists()) {
     Files.forceMkdir(file.getParentFile());
     response.setContentType(URLConnection.guessContentTypeFromName(path));
     if (!zoom && !supportWebp) {
       file = seek;
     } else {
       synchronized (this) {
         if (!file.exists()) {
           if (supportWebp) {
             response.setContentType(WEBP_CONTENT_TYPE);
             try {
               im4javas.format(seek, new File(Files.getFilename(absPath)), WEBP);
             } catch (Exception e) {
               throw new SystemException(e);
             }
             seek = file;
           }
           if (zoom) {
             try {
               im4javas.zoom(seek, file, format);
             } catch (Exception e) {
               throw new SystemException(e);
             }
           }
         }
       }
     }
   }
   response.setContentLength((int) file.length());
   response.addDateHeader("Last-Modified", System.currentTimeMillis());
   response.addDateHeader("Expires", System.currentTimeMillis() + maxAge * 1000);
   response.setHeader("Cache-Control", "max-age=" + maxAge);
   response.setStatus(HttpServletResponse.SC_OK);
   try {
     OutputStream out = response.getOutputStream();
     FileUtils.copyFile(file, out);
   } catch (IOException e) {
   }
 }