Adding class MediaHandlerImagesExternScaling to take care of scaling images (this...
[mir.git] / source / mircoders / media / MediaHandlerImagesExternScaling.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.media;
31
32
33 import java.io.BufferedInputStream;
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38
39 import mir.entity.Entity;
40 import mir.log.LoggerWrapper;
41 import mir.media.MediaExc;
42 import mir.media.MediaFailure;
43 import mir.media.image.ImageMagickImageProcessor;
44 import mir.media.image.ImageProcessor;
45 import mir.misc.StringUtil;
46 import mir.util.FileRoutines;
47
48 /**
49  * Image handler that stores images outside of the database.
50  * 
51  * @author Zapata
52  * @version 1.0
53  */
54
55 public class MediaHandlerImagesExternScaling extends MediaHandlerGeneric
56 {
57   private int maxSize;
58   private int maxIconSize;
59   private float minDescaleRatio;
60   private int minDescaleReduction;
61   public MediaHandlerImagesExternScaling() {
62     logger = new LoggerWrapper("Media.Images.Extern");
63     maxSize = configuration.getInt("Producer.Image.MaxSize");
64     maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
65     minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
66     minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
67   }
68   
69
70   
71   public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
72     try {
73       String date = anImageEntity.getFieldValue("date");
74       String datePath = StringUtil.webdbDate2path(date);
75       String ext = "." + mediaTypeEnt.getFieldValue("name");
76       String fileBasePath = datePath + anImageEntity.getId();
77       String filePath = fileBasePath + ext;
78
79       String imageFilePath = getBaseStoragePath() + File.separator + filePath;
80       File imageFile = new File(imageFilePath);
81       
82       if (!imageFile.exists()) {
83         throw new MediaExc("error in MediaHandlerImagesExtern.execute(): "
84             + filePath + " does not exist!");
85       } else {
86         ImageProcessor processor = new ImageMagickImageProcessor(imageFile);
87         String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg";
88         String iconStoragePath = doIconScaling(processor, iconPath);
89         anImageEntity.setFieldValue("icon_height", new Integer(processor.getScaledHeight()).toString());
90         anImageEntity.setFieldValue("icon_width", new Integer(processor.getScaledWidth()).toString());
91         anImageEntity.setFieldValue("icon_path", iconPath);
92         
93         String imageOriginalRelPath = doImageScaling(filePath, imageFile, processor);
94         anImageEntity.setFieldValue("original_file_path", imageOriginalRelPath);
95         anImageEntity.setFieldValue("img_height", Integer.toString(processor.getScaledHeight()));
96         anImageEntity.setFieldValue("img_width", Integer.toString(processor.getScaledWidth()));
97
98         processor.cleanup();
99         anImageEntity.setFieldValue("publish_path", filePath);
100         anImageEntity.update();
101         reportChange(iconStoragePath);
102         reportChange(imageFilePath);
103       }
104     }
105     catch(Throwable t) {
106       logger.error("MediaHandlerImagesExtern.execute: " + t.getMessage(), t);
107       throw new MediaFailure(t.getMessage(), t);
108     }
109   }
110
111     /**
112      * Scale an icon image and write it to the file system.
113      * @param processor
114      * @param iconPath
115      * @return
116      * @throws MediaExc
117      */
118     private String doIconScaling(ImageProcessor processor, String iconPath) throws MediaExc {
119         String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath;
120         File iconFile = new File(iconStoragePath);
121         processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
122         File dir = new File(iconFile.getParent());
123         if (dir != null && !dir.exists()) {
124           dir.mkdirs();
125         }
126         processor.writeScaledData(iconFile, "JPEG");
127         return iconStoragePath;
128     }
129
130     /**
131      * Make the resized file.
132      * @param filePath
133      * @param imageFile
134      * @param processor
135      * @return
136      * @throws MediaExc
137      * @throws IOException
138      */
139     private String doImageScaling(String filePath, File imageFile, ImageProcessor processor) throws MediaExc, IOException {
140         // get a file path where the the original image should be saved if image resizing is turned on
141           String imagesOriginalDir = configuration.getString("Producer.ImagesOriginalDir.Path");
142           String imagesOriginalDirRelPath = configuration.getString("Producer.ImagesOriginalDir.RelPath");
143           String imageOriginalFilePath = imagesOriginalDir + filePath;
144           String imageOriginalRelPath = imagesOriginalDirRelPath +  filePath;
145           File originalFile = new File(imageOriginalFilePath);   
146           processor.descaleImage(maxSize, minDescaleRatio, minDescaleReduction);
147           File originalDir = new File(originalFile.getParent());
148           if (originalDir != null && !originalDir.exists()) {
149             originalDir.mkdirs();
150           }
151           if (!originalFile.exists()) {
152             FileRoutines.copy(imageFile, originalFile);
153             reportChange(originalFile.getAbsolutePath());
154           }
155           // yoss: don't write the scaled data again if it's the same size as
156           // the file that's there right now. Image producer runs are 4 times
157           // faster this way.
158           if (processor.getScaledFileSize() != imageFile.length()) {
159               processor.writeScaledData(imageFile, "JPEG");
160           }
161         return imageOriginalRelPath;
162     }
163
164 /** {@inheritDoc} */
165   public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure {
166     try {
167       File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path"));
168
169       if (!file.exists()) {
170         // hackish
171         file = new File(configuration.getHome(), "../img/photo_big.gif");
172       }
173
174       return new BufferedInputStream(
175         new FileInputStream(file),8192);
176     }
177     catch (Throwable t) {
178       return null;
179     }
180   }
181
182   public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {
183     return "image/jpeg";
184   }
185
186   public String getBaseStoragePath()
187   {
188     return configuration.getString("Producer.Image.Path");
189   }
190
191   public String getBaseIconStoragePath()
192   {
193     return configuration.getString("Producer.Image.IconPath");
194   }
195
196   public String getPublishHost()
197   {
198     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
199   }
200
201   public String getTinyIconName()
202   {
203     return configuration.getString("Producer.Icon.TinyImage");
204   }
205
206   public String getBigIconName()
207   {
208     return configuration.getString("Producer.Icon.BigImage");
209   }
210
211   public String getIconAltName()
212   {
213     return "Image";
214   }
215
216   public String getDescr(Entity mediaType)
217   {
218      return "image/jpeg";
219   }
220 }