Changes which allow images to be resized by Mir and saved in the filesystem if an...
[mir.git] / source / mircoders / media / MediaHandlerImagesExtern.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.FileOutputStream;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39
40 import mir.util.StreamCopier;
41 import mir.entity.Entity;
42 import mir.log.LoggerWrapper;
43 import mir.media.MediaExc;
44 import mir.media.MediaFailure;
45 import mir.media.image.ImageProcessor;
46 import mir.media.image.ImageMagickImageProcessor;
47 import mir.misc.StringUtil;
48
49 /**
50  * Image handler that stores images outside of the database.
51  * 
52  * @author Zapata
53  * @version 1.0
54  */
55
56 public class MediaHandlerImagesExtern extends MediaHandlerGeneric
57 {
58   private int maxSize;
59   private int maxIconSize;
60   private float minDescaleRatio;
61   private int minDescaleReduction;
62   private boolean scaleImages;
63   private String imagesOriginalDir;
64   private String imagesOriginalDirRelPath;
65   private String imageOriginalFilePath;
66   private String imageOriginalRelPath;
67   
68   public MediaHandlerImagesExtern() {
69     logger = new LoggerWrapper("Media.Images.Extern");
70     maxSize = configuration.getInt("Producer.Image.MaxSize");
71     maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
72     minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
73     minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
74     scaleImages = configuration.getBoolean("Producer.Image.ScaleImages");
75     imagesOriginalDir = configuration.getString("Producer.ImagesOriginalDir.Path"); 
76     imagesOriginalDirRelPath = configuration.getString("Producer.ImagesOriginalDir.RelPath");
77   }
78
79   public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
80     try {
81       String date = anImageEntity.getFieldValue("date");
82       String datePath = StringUtil.webdbDate2path(date);
83       String ext = "." + mediaTypeEnt.getFieldValue("name");
84       String fileBasePath = datePath + anImageEntity.getId();
85       String filePath = fileBasePath + ext;
86       String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg";
87       String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath;
88       String imageFilePath = getBaseStoragePath() + File.separator + filePath;
89       
90       // yoss: get a file path where the the original image should be saved if image resizing is turned on
91       imageOriginalFilePath = imagesOriginalDir + filePath;
92       imageOriginalRelPath = imagesOriginalDirRelPath +  filePath;
93       
94       // yoss:make a new File object for the originalFile
95       File originalFile = new File(imageOriginalFilePath);   
96       logger.info("imageOriginalFilePath:" + imageOriginalFilePath);
97       File imageFile = new File(imageFilePath);
98       logger.info("******************************************");
99       logger.info("imageFile exists: " + imageFile.exists());
100       logger.info("imageFile: " + imageFile.getAbsolutePath());
101       File iconFile = new File(iconStoragePath);
102       logger.info("iconStoragePath:"+ iconStoragePath);
103       
104       
105       if (!imageFile.exists()) {
106         throw new MediaExc("error in MediaHandlerImagesExtern.execute(): " + filePath + " does not exist!");
107       }
108       else {
109         ImageProcessor processor = new ImageMagickImageProcessor(imageFile);
110
111         processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
112         File dir = new File(iconFile.getParent());
113           if (dir!=null && !dir.exists()){
114             dir.mkdirs();
115         }
116         processor.writeScaledData(iconFile, "JPEG");
117         
118         // yoss: if the config is set so that images should be scaled, make the resized file.
119         if (scaleImages){ 
120                 logger.info("entered scaleImages");
121             ImageProcessor originalProcessor = new ImageMagickImageProcessor(imageFile);
122             originalProcessor.descaleImage(maxSize, minDescaleRatio, minDescaleReduction);
123                 File originalDir = new File(originalFile.getParent());
124                 if(originalDir!=null && !originalDir.exists()) {
125                         originalDir.mkdirs();
126                 }
127                 if(!originalFile.exists()) {
128                         logger.debug("the original image file doesn't exist, copying to originalFile");
129                         StreamCopier.copyFile(imageFile, originalFile);
130                 }
131                 // yoss: don't write the scaled data again if it's the same size as 
132                 // the file that's there right now.  Image producer runs are 4 times faster this way.
133                 logger.info("about to write scaled data, byte length: " + originalProcessor.getScaledFileSize());
134                 if (originalProcessor.getScaledFileSize() != imageFile.length()) {
135                         originalProcessor.writeScaledData(imageFile, "JPEG");
136                 }
137                 anImageEntity.setFieldValue("original_file_path", imageOriginalRelPath);
138             anImageEntity.setFieldValue("img_height", new Integer(originalProcessor.getScaledHeight()).toString());
139             anImageEntity.setFieldValue("img_width", new Integer(originalProcessor.getScaledWidth()).toString());
140                 
141                 originalProcessor.cleanup();
142                 
143         } else {
144                 anImageEntity.setFieldValue("img_height", new Integer(processor.getHeight()).toString());
145                 anImageEntity.setFieldValue("img_width", new Integer(processor.getWidth()).toString());
146         }
147
148         anImageEntity.setFieldValue("icon_height", new Integer(processor.getScaledHeight()).toString());
149         anImageEntity.setFieldValue("icon_width", new Integer(processor.getScaledWidth()).toString());
150
151         processor.cleanup();
152
153         anImageEntity.setFieldValue("icon_path", iconPath);
154         anImageEntity.setFieldValue("publish_path", filePath);
155
156         anImageEntity.update();
157
158       }
159     }
160     catch(Throwable t) {
161       logger.error("MediaHandlerImagesExtern.execute: " + t.getMessage(), t);
162       throw new MediaFailure(t.getMessage(), t);
163     }
164   }
165
166
167   /** {@inheritDoc} */
168   public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure {
169     try {
170       File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path"));
171
172       if (!file.exists()) {
173         // hackish
174         file = new File(configuration.getHome(), "../img/photo_big.gif");
175       }
176
177       return new BufferedInputStream(
178         new FileInputStream(file),8192);
179     }
180     catch (Throwable t) {
181       return null;
182     }
183   }
184
185   public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {
186     return "image/jpeg";
187   }
188
189   public String getBaseStoragePath()
190   {
191     return configuration.getString("Producer.Image.Path");
192   }
193
194   public String getBaseIconStoragePath()
195   {
196     return configuration.getString("Producer.Image.IconPath");
197   }
198
199   public String getPublishHost()
200   {
201     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
202   }
203
204   public String getTinyIconName()
205   {
206     return configuration.getString("Producer.Icon.TinyImage");
207   }
208
209   public String getBigIconName()
210   {
211     return configuration.getString("Producer.Icon.BigImage");
212   }
213
214   public String getIconAltName()
215   {
216     return "Image";
217   }
218
219   public String getDescr(Entity mediaType)
220   {
221      return "image/jpeg";
222   }
223 }