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.FileOutputStream;
37 import java.io.InputStream;
38 import java.io.OutputStream;
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 import mir.util.FileRoutines;
51 * Image handler that stores images outside of the database.
57 public class MediaHandlerImagesExtern extends MediaHandlerGeneric
60 private int maxIconSize;
61 private float minDescaleRatio;
62 private int minDescaleReduction;
63 private boolean scaleImages;
65 public MediaHandlerImagesExtern() {
66 logger = new LoggerWrapper("Media.Images.Extern");
67 maxSize = configuration.getInt("Producer.Image.MaxSize");
68 maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
69 minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
70 minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
71 scaleImages = configuration.getBoolean("Producer.Image.ScaleImages");
74 public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
75 String imagesOriginalDir = configuration.getString("Producer.ImagesOriginalDir.Path"); ;
76 String imagesOriginalDirRelPath = configuration.getString("Producer.ImagesOriginalDir.RelPath");;
77 String imageOriginalFilePath;
78 String imageOriginalRelPath;
80 String date = anImageEntity.getFieldValue("date");
81 String datePath = StringUtil.webdbDate2path(date);
82 String ext = "." + mediaTypeEnt.getFieldValue("name");
83 String fileBasePath = datePath + anImageEntity.getId();
84 String filePath = fileBasePath + ext;
85 String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg";
86 String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath;
87 String imageFilePath = getBaseStoragePath() + File.separator + filePath;
89 // yoss: get a file path where the the original image should be saved if image resizing is turned on
90 imageOriginalFilePath = imagesOriginalDir + filePath;
91 imageOriginalRelPath = imagesOriginalDirRelPath + filePath;
93 // yoss:make a new File object for the originalFile
94 File originalFile = new File(imageOriginalFilePath);
95 logger.info("imageOriginalFilePath:" + imageOriginalFilePath);
96 File imageFile = new File(imageFilePath);
97 logger.info("******************************************");
98 logger.info("imageFile exists: " + imageFile.exists());
99 logger.info("imageFile: " + imageFile.getAbsolutePath());
100 File iconFile = new File(iconStoragePath);
101 logger.info("iconStoragePath:"+ iconStoragePath);
104 if (!imageFile.exists()) {
105 throw new MediaExc("error in MediaHandlerImagesExtern.execute(): "
106 + filePath + " does not exist!");
108 ImageProcessor processor = new ImageMagickImageProcessor(imageFile);
109 processor.descaleImage(maxIconSize, minDescaleRatio,
110 minDescaleReduction);
111 File dir = new File(iconFile.getParent());
112 if (dir != null && !dir.exists()) {
115 processor.writeScaledData(iconFile, "JPEG");
117 // yoss: if the config is set so that images should be scaled, make the
120 logger.info("entered scaleImages");
121 ImageProcessor originalProcessor = new ImageMagickImageProcessor(
123 originalProcessor.descaleImage(maxSize, minDescaleRatio,
124 minDescaleReduction);
125 File originalDir = new File(originalFile.getParent());
126 if (originalDir != null && !originalDir.exists()) {
127 originalDir.mkdirs();
129 if (!originalFile.exists()) {
130 logger.debug("the original image file doesn't exist, copying to originalFile");
131 FileRoutines.copy(imageFile, originalFile);
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
136 logger.info("about to write scaled data, byte length: "
137 + originalProcessor.getScaledFileSize());
138 if (originalProcessor.getScaledFileSize() != imageFile.length()) {
139 originalProcessor.writeScaledData(imageFile, "JPEG");
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()));
148 originalProcessor.cleanup();
151 anImageEntity.setFieldValue("img_height", new Integer(processor
152 .getHeight()).toString());
153 anImageEntity.setFieldValue("img_width", new Integer(processor
154 .getWidth()).toString());
157 anImageEntity.setFieldValue("icon_height", new Integer(processor
158 .getScaledHeight()).toString());
159 anImageEntity.setFieldValue("icon_width", new Integer(processor
160 .getScaledWidth()).toString());
164 anImageEntity.setFieldValue("icon_path", iconPath);
165 anImageEntity.setFieldValue("publish_path", filePath);
167 anImageEntity.update();
172 logger.error("MediaHandlerImagesExtern.execute: " + t.getMessage(), t);
173 throw new MediaFailure(t.getMessage(), t);
179 public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure {
181 File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path"));
183 if (!file.exists()) {
185 file = new File(configuration.getHome(), "../img/photo_big.gif");
188 return new BufferedInputStream(
189 new FileInputStream(file),8192);
191 catch (Throwable t) {
196 public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {
200 public String getBaseStoragePath()
202 return configuration.getString("Producer.Image.Path");
205 public String getBaseIconStoragePath()
207 return configuration.getString("Producer.Image.IconPath");
210 public String getPublishHost()
212 return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
215 public String getTinyIconName()
217 return configuration.getString("Producer.Icon.TinyImage");
220 public String getBigIconName()
222 return configuration.getString("Producer.Icon.BigImage");
225 public String getIconAltName()
230 public String getDescr(Entity mediaType)