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