- package com.r9.harmony.controllers;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import javax.activation.MimetypesFileTypeMap;
- import javax.mail.MessagingException;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import com.r9.nucleus.commons.ImageFile;
- import com.r9.nucleus.commons.ImageUtils;
- import com.r9.nucleus.db.DBMessage;
- import com.r9.nucleus.eh.ErrorHandler;
- import com.r9.nucleus.performance.IPerformanceMonitorFactory;
- import com.r9.nucleus.performance.IPerformanceTimeMonitor;
- import com.r9.nucleus.performance.PerformanceMonitorFactory;
- import com.r9.nucleus.web.CommonController;
- @Controller
- public class ImageResizeApiController extends CommonController {
- ErrorHandler eh = ErrorHandler.getInstance();
- @RequestMapping("/api/image")
- public void returnImage(HttpServletResponse resp,
- @RequestParam("url") String url,
- @RequestParam(value="width", required=false) Integer width,
- @RequestParam(value="height", required=false) Integer height,
- @RequestParam(value="crop", required=false, defaultValue="false") Boolean crop)
- throws MessagingException, IOException {
- IPerformanceMonitorFactory factory = PerformanceMonitorFactory.getInstance();
- IPerformanceTimeMonitor monitor = factory.getTimeMonitor("ImageResizeApiController.returnImage", "ms");
- monitor.start();
- try {
- // Fetch image from disk
- boolean constrain = false;
- // Add a beginning / if the user didn't put one in
- if (url.charAt(0) != '/') {
- url = "/" + url;
- }
- String docRoot = config.getHome() + "/webapps/ROOT";
- File img = new File(docRoot + url);
- if (!img.getCanonicalPath().startsWith(docRoot)) {
- // if the file isn't in a subdirectory of the docRoot (someone passed )
- return;
- }
- ImageFile orig = new ImageFile(img);
- int[] dimensions = ImageUtils.getImageDimensions(docRoot + url);
- orig.setWidth(dimensions[0]);
- orig.setHeight(dimensions[1]);
- // If no height/width specified, return image in same size
- if (width == null && height == null) {
- width = orig.getWidth();
- height = orig.getHeight();
- } else {
- // if only one dimension specified, figure out aspect ratio for the other one
- if (width == null) {
- constrain = true;
- double aspectRatio = (double)orig.getWidth() / (double)orig.getHeight();
- width = (int)(height * aspectRatio);
- } else if (height == null) {
- constrain = true;
- double aspectRatio = (double)orig.getHeight() / (double)orig.getWidth();
- height = (int)(width * aspectRatio);
- }
- }
- File resized = new File("/tmp/" + orig.getFileName() + ".resized" + width + "x" + height);
- // Check if new file already exists
- if (!resized.exists()) {
- ImageFile newImage = new ImageFile("/tmp/" + orig.getFileName() + ".resized" + width + "x" + height, width, height);
- try {
- ImageUtils.scaleImage(orig, newImage, constrain, false);
- }
- catch (Exception e) {
- eh.vhandleUnexpected(ImageResizeApiController.class, DBMessage.MISSING_PARAMETERS, "a", "b");
- }
- resized = newImage.getFile();
- }
- resp.setContentType(new MimetypesFileTypeMap().getContentType(resized));
- FileInputStream fis = new FileInputStream(resized);
- byte[] b = new byte[(int)resized.length()];
- fis.read(b);
- resp.addHeader("Content-Disposition", "inline; filename=\"" + orig.getFileName() + '"');
- OutputStream os = resp.getOutputStream();
- for (int i=0; i < b.length; i++) {
- os.write(b[i]);
- }
- os.flush();
- os.close();
- }
- finally {
- monitor.stop();
- }
- }
- }