2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
32 package mircoders.media;
\r
35 import java.io.File;
\r
36 import java.io.FileInputStream;
\r
37 import java.io.InputStream;
\r
39 import mir.config.MirPropertiesConfiguration;
\r
40 import mir.entity.Entity;
\r
41 import mir.log.LoggerWrapper;
\r
42 import mir.media.MediaExc;
\r
43 import mir.media.MediaFailure;
\r
44 import mir.misc.StringUtil;
\r
45 import mircoders.storage.DatabaseUploadedMedia;
\r
49 * Image handler that stores images outside of the database. Will be replaced by the new
\r
50 * media handling system.
\r
55 public class MediaHandlerImagesExtern extends MediaHandlerGeneric
\r
57 private int maxIconSize;
\r
58 private float minDescaleRatio;
\r
59 private int minDescaleReduction;
\r
61 public MediaHandlerImagesExtern() {
\r
63 logger = new LoggerWrapper("Media.Images.Extern");
\r
65 MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
\r
67 catch (Throwable t) {
\r
68 logger.fatal("MediaHandlerImagesExtern: can't get configuration");
\r
70 throw new RuntimeException(t.toString());
\r
73 maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
\r
74 minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
\r
75 minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
\r
78 public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
\r
80 String date = anImageEntity.getValue("date");
\r
81 String datePath = StringUtil.webdbDate2path(date);
\r
82 String ext = "." + mediaTypeEnt.getValue("name");
\r
83 String filePath = datePath + anImageEntity.getId() + ext;
\r
84 String iconFilePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") + getIconStoragePath() + filePath;
\r
85 String imageFilePath = getStoragePath() + File.separator + filePath;
\r
87 File imageFile = new File(imageFilePath);
\r
88 File iconFile = new File(iconFilePath);
\r
90 if (!imageFile.exists()) {
\r
91 throw new MediaExc("error in MediaHandlerImagesExtern.produce(): " + filePath + " does not exist!");
\r
94 ImageProcessor processor = new ImageProcessor(imageFile, "JPEG");
\r
96 processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
\r
97 File dir = new File(iconFile.getParent());
\r
98 if (dir!=null && !dir.exists()){
\r
101 processor.writeScaledData(iconFile);
\r
103 logger.info(processor.getWidth()+"x"+processor.getHeight());
\r
105 anImageEntity.setValueForProperty("img_height", new Integer(processor.getHeight()).toString());
\r
106 anImageEntity.setValueForProperty("img_width", new Integer(processor.getWidth()).toString());
\r
108 anImageEntity.setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString());
\r
109 anImageEntity.setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString());
\r
111 anImageEntity.setValueForProperty("icon_path", getIconStoragePath()+filePath);
\r
112 anImageEntity.setValueForProperty("publish_path", filePath);
\r
114 anImageEntity.update();
\r
117 catch(Throwable t) {
\r
118 logger.error("MediaHandlerImagesExtern.produce: " + t.getMessage());
\r
119 t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
120 throw new MediaFailure(t.getMessage(), t);
\r
125 public InputStream getIcon(Entity anImageEntity) throws MediaExc, MediaFailure {
\r
127 Entity mediaType = DatabaseUploadedMedia.getInstance().getMediaType(
\r
130 String date = anImageEntity.getValue("date");
\r
131 String datePath = StringUtil.webdbDate2path(date);
\r
132 String ext = "." + mediaType.getValue("name");
\r
133 String filePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") +
\r
134 getIconStoragePath() + datePath + anImageEntity.getId() + ext;
\r
136 return new FileInputStream(new File(filePath));
\r
138 catch (Throwable t) {
\r
139 throw new MediaFailure(t);
\r
143 public String getStoragePath()
\r
145 return configuration.getString("Producer.Image.Path");
\r
148 public String getIconStoragePath()
\r
150 return configuration.getString("Producer.Image.IconPath");
\r
153 public String getPublishHost()
\r
155 return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
\r
158 public String getTinyIconName()
\r
160 return configuration.getString("Producer.Icon.TinyImage");
\r
163 public String getBigIconName()
\r
165 return configuration.getString("Producer.Icon.BigImage");
\r
168 public String getIconAltName()
\r
173 public boolean isVideo()
\r
178 public boolean isAudio()
\r
183 public boolean isImage ()
\r
188 public String getDescr(Entity mediaType)
\r
190 return "image/jpeg";
\r