001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.controllers.files;
034:
035: import com.sun.image.codec.jpeg.JPEGCodec;
036: import com.sun.image.codec.jpeg.JPEGEncodeParam;
037: import com.sun.image.codec.jpeg.JPEGImageEncoder;
038:
039: import org.libresource.Libresource;
040:
041: import org.libresource.core.CoreConstants;
042: import org.libresource.core.FileData;
043: import org.libresource.core.interfaces.LibresourceCoreService;
044:
045: import org.libresource.files.FileConstants;
046: import org.libresource.files.ejb.model.FileResourceValue;
047: import org.libresource.files.interfaces.LibresourceFilesService;
048:
049: import org.libresource.web.Controller;
050:
051: import java.awt.Container;
052: import java.awt.Graphics2D;
053: import java.awt.Image;
054: import java.awt.MediaTracker;
055: import java.awt.RenderingHints;
056: import java.awt.Toolkit;
057: import java.awt.image.BufferedImage;
058:
059: import java.io.BufferedOutputStream;
060: import java.io.File;
061: import java.io.FileInputStream;
062: import java.io.FileOutputStream;
063: import java.io.InputStream;
064: import java.io.OutputStream;
065:
066: import java.net.URI;
067:
068: import java.text.SimpleDateFormat;
069:
070: import java.util.HashMap;
071:
072: import javax.servlet.http.HttpServletRequest;
073: import javax.servlet.http.HttpServletResponse;
074:
075: public class DownloadFileController implements Controller {
076: public Object process(URI uri, HttpServletRequest request,
077: HttpServletResponse response) throws Exception {
078: LibresourceCoreService libresourceCoreService = (LibresourceCoreService) Libresource
079: .getService(CoreConstants.SERVICE);
080: LibresourceFilesService libresourceFilesService = (LibresourceFilesService) Libresource
081: .getService(FileConstants.SERVICE);
082:
083: HashMap infos = new HashMap();
084: infos.put("ip", request.getRemoteAddr());
085:
086: FileResourceValue file = libresourceFilesService.getFile(uri);
087:
088: String fileName = file.getName().replaceAll(" ", "_");
089: InputStream in = null;
090: int size = 0;
091:
092: // get thumbnail
093: if (request.getParameter("thumbnail") != null) {
094: String filePath = libresourceCoreService.getTempFilePath(
095: "files_content_", file.getId());
096: File tn = new File(filePath + "_tn");
097:
098: try {
099: if (!tn.exists()) {
100: // generate thumbnail
101: makeThumbnail(filePath);
102: }
103:
104: in = new FileInputStream(tn.getAbsolutePath());
105: size = (int) tn.length();
106: } catch (Exception e) {
107: FileData fileData = libresourceFilesService
108: .getFileContent(uri, infos);
109: in = fileData.getInputStream();
110: size = (int) fileData.getSize();
111: }
112: } else {
113: // get file content
114: FileData fileData = libresourceFilesService.getFileContent(
115: uri, infos);
116: size = (int) file.getSize();
117: in = fileData.getInputStream();
118: }
119:
120: // send file as attachment
121: OutputStream out = response.getOutputStream();
122: response.addHeader("Content-Disposition",
123: "attachment; filename=" + fileName);
124: response.addHeader("Last-Modified", new SimpleDateFormat(
125: "E, d MMM yyyy hh:mm:ss 'GMT'").format(file.getDate()));
126: response.addHeader("Expire", new SimpleDateFormat(
127: "E, d MMM yyyy hh:mm:ss 'GMT'")
128: .format(new SimpleDateFormat("dd-MM-yy")
129: .parse("01-01-2010")));
130: response.setContentType(file.getType());
131: response.setContentLength(size);
132:
133: int byteCount = -1;
134: byte[] data = new byte[100000];
135:
136: while ((byteCount = in.read(data, 0, 100000)) > 0) {
137: out.write(data, 0, byteCount);
138: }
139:
140: in.close();
141: out.flush();
142: out.close();
143:
144: return null;
145: }
146:
147: private void makeThumbnail(String filePath) throws Exception {
148: try {
149: // load image
150: Image image = Toolkit.getDefaultToolkit()
151: .getImage(filePath);
152: MediaTracker mediaTracker = new MediaTracker(
153: new Container());
154: mediaTracker.addImage(image, 0);
155: mediaTracker.waitForID(0);
156:
157: // determine thumbnail size from WIDTH and HEIGHT
158: int thumbWidth = Integer.parseInt("100");
159: int thumbHeight = Integer.parseInt("100");
160: double thumbRatio = (double) thumbWidth
161: / (double) thumbHeight;
162: int imageWidth = image.getWidth(null);
163: int imageHeight = image.getHeight(null);
164: double imageRatio = (double) imageWidth
165: / (double) imageHeight;
166:
167: if (thumbRatio < imageRatio) {
168: thumbHeight = (int) (thumbWidth / imageRatio);
169: } else {
170: thumbWidth = (int) (thumbHeight * imageRatio);
171: }
172:
173: // draw original image to thumbnail image object and
174: // scale it to the new size on-the-fly
175: BufferedImage thumbImage = new BufferedImage(thumbWidth,
176: thumbHeight, BufferedImage.TYPE_INT_RGB);
177: Graphics2D graphics2D = thumbImage.createGraphics();
178: graphics2D.setRenderingHint(
179: RenderingHints.KEY_INTERPOLATION,
180: RenderingHints.VALUE_INTERPOLATION_BILINEAR);
181: graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight,
182: null);
183:
184: // save thumbnail image
185: String tnPath = filePath + "_tn";
186: BufferedOutputStream out = new BufferedOutputStream(
187: new FileOutputStream(tnPath));
188: JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
189: JPEGEncodeParam param = encoder
190: .getDefaultJPEGEncodeParam(thumbImage);
191: int quality = Integer.parseInt("100");
192: quality = Math.max(0, Math.min(quality, 100));
193: param.setQuality((float) quality / 100.0f, false);
194: encoder.setJPEGEncodeParam(param);
195: encoder.encode(thumbImage);
196: out.flush();
197: out.close();
198: } catch (Exception e) {
199: throw e;
200: }
201: }
202: }
|