Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 4.51 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.r9.harmony.controllers;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7.  
  8. import javax.activation.MimetypesFileTypeMap;
  9. import javax.mail.MessagingException;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15.  
  16. import com.r9.nucleus.commons.ImageFile;
  17. import com.r9.nucleus.commons.ImageUtils;
  18. import com.r9.nucleus.db.DBMessage;
  19. import com.r9.nucleus.eh.ErrorHandler;
  20. import com.r9.nucleus.performance.IPerformanceMonitorFactory;
  21. import com.r9.nucleus.performance.IPerformanceTimeMonitor;
  22. import com.r9.nucleus.performance.PerformanceMonitorFactory;
  23. import com.r9.nucleus.web.CommonController;
  24.  
  25. @Controller
  26. public class ImageResizeApiController extends CommonController {
  27.     ErrorHandler eh = ErrorHandler.getInstance();
  28.     @RequestMapping("/api/image")
  29.     public void returnImage(HttpServletResponse resp,
  30.                             @RequestParam("url") String url,
  31.                             @RequestParam(value="width", required=false) Integer width,
  32.                             @RequestParam(value="height", required=false) Integer height,
  33.                             @RequestParam(value="crop", required=false, defaultValue="false") Boolean crop)
  34.         throws MessagingException, IOException {
  35.         IPerformanceMonitorFactory factory = PerformanceMonitorFactory.getInstance();
  36.         IPerformanceTimeMonitor monitor = factory.getTimeMonitor("ImageResizeApiController.returnImage", "ms");
  37.         monitor.start();
  38.         try {
  39.             // Fetch image from disk
  40.             boolean constrain = false;
  41.  
  42.             // Add a beginning / if the user didn't put one in
  43.             if (url.charAt(0) != '/') {
  44.                 url = "/" + url;
  45.             }
  46.             String docRoot = config.getHome() + "/webapps/ROOT";
  47.             File img = new File(docRoot + url);
  48.             if (!img.getCanonicalPath().startsWith(docRoot)) {
  49.                 // if the file isn't in a subdirectory of the docRoot (someone passed )
  50.                 return;
  51.             }
  52.             ImageFile orig = new ImageFile(img);
  53.             int[] dimensions = ImageUtils.getImageDimensions(docRoot + url);
  54.             orig.setWidth(dimensions[0]);
  55.             orig.setHeight(dimensions[1]);
  56.  
  57.             // If no height/width specified, return image in same size
  58.             if (width == null && height == null) {
  59.                 width = orig.getWidth();
  60.                 height = orig.getHeight();
  61.             } else {
  62.                 // if only one dimension specified, figure out aspect ratio for the other one
  63.                 if (width == null) {
  64.                     constrain = true;
  65.                     double aspectRatio = (double)orig.getWidth() / (double)orig.getHeight();
  66.                     width = (int)(height * aspectRatio);
  67.                 } else if (height == null) {
  68.                     constrain = true;
  69.                     double aspectRatio = (double)orig.getHeight() / (double)orig.getWidth();
  70.                     height = (int)(width * aspectRatio);
  71.                 }
  72.             }
  73.  
  74.             File resized  = new File("/tmp/" + orig.getFileName() + ".resized" + width + "x" + height);
  75.             // Check if new file already exists
  76.             if (!resized.exists()) {
  77.                 ImageFile newImage = new ImageFile("/tmp/" + orig.getFileName() + ".resized"  + width + "x" + height, width, height);
  78.                 try {
  79.                     ImageUtils.scaleImage(orig, newImage, constrain, false);
  80.                 }
  81.                 catch (Exception e) {
  82.                     eh.vhandleUnexpected(ImageResizeApiController.class, DBMessage.MISSING_PARAMETERS, "a", "b");
  83.                 }
  84.                 resized = newImage.getFile();
  85.             }
  86.  
  87.             resp.setContentType(new MimetypesFileTypeMap().getContentType(resized));
  88.             FileInputStream fis = new FileInputStream(resized);
  89.             byte[] b = new byte[(int)resized.length()];
  90.             fis.read(b);
  91.             resp.addHeader("Content-Disposition", "inline; filename=\"" + orig.getFileName() + '"');
  92.             OutputStream os = resp.getOutputStream();
  93.             for (int i=0; i < b.length; i++) {
  94.                 os.write(b[i]);
  95.             }
  96.             os.flush();
  97.             os.close();
  98.         }
  99.         finally {
  100.             monitor.stop();
  101.         }
  102.     }
  103. }