2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
30 package mircoders.media;
33 import java.io.BufferedInputStream;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
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;
49 * Image handler that stores images outside of the database.
55 public class MediaHandlerImagesExternScaling extends MediaHandlerGeneric
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");
71 public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
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;
79 String imageFilePath = getBaseStoragePath() + File.separator + filePath;
80 File imageFile = new File(imageFilePath);
82 if (!imageFile.exists()) {
83 throw new MediaExc("error in MediaHandlerImagesExtern.execute(): "
84 + filePath + " does not exist!");
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);
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()));
99 anImageEntity.setFieldValue("publish_path", filePath);
100 anImageEntity.update();
101 reportChange(iconStoragePath);
102 reportChange(imageFilePath);
106 logger.error("MediaHandlerImagesExtern.execute: " + t.getMessage(), t);
107 throw new MediaFailure(t.getMessage(), t);
112 * Scale an icon image and write it to the file system.
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()) {
126 processor.writeScaledData(iconFile, "JPEG");
127 return iconStoragePath;
131 * Make the resized file.
137 * @throws IOException
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();
151 if (!originalFile.exists()) {
152 FileRoutines.copy(imageFile, originalFile);
153 reportChange(originalFile.getAbsolutePath());
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
158 if (processor.getScaledFileSize() != imageFile.length()) {
159 processor.writeScaledData(imageFile, "JPEG");
161 return imageOriginalRelPath;
165 public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure {
167 File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path"));
169 if (!file.exists()) {
171 file = new File(configuration.getHome(), "../img/photo_big.gif");
174 return new BufferedInputStream(
175 new FileInputStream(file),8192);
177 catch (Throwable t) {
182 public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {
186 public String getBaseStoragePath()
188 return configuration.getString("Producer.Image.Path");
191 public String getBaseIconStoragePath()
193 return configuration.getString("Producer.Image.IconPath");
196 public String getPublishHost()
198 return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
201 public String getTinyIconName()
203 return configuration.getString("Producer.Icon.TinyImage");
206 public String getBigIconName()
208 return configuration.getString("Producer.Icon.BigImage");
211 public String getIconAltName()
216 public String getDescr(Entity mediaType)