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 the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
33 package mircoders.media;
36 import java.io.InputStream;
38 import freemarker.template.SimpleList;
40 import mir.config.MirPropertiesConfiguration;
41 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
42 import mir.entity.Entity;
43 import mir.media.MirMedia;
44 import mir.media.MirMediaException;
45 import mir.misc.FileUtil;
46 import mir.misc.StringUtil;
47 import mir.storage.StorageObjectFailure;
48 import mir.log.LoggerWrapper;
50 import mircoders.entity.EntityImages;
53 * This class handles saving, fetching creating representations
54 * for all images. The image content is stored in the DB. The content is
55 * written out to a file at the ProducerImages level.
56 * Remember that Handlers for specific image types, Gif, Jpeg, etc..
58 * It implements the MirMedia interface.
60 * slowly starting to look better, a next step would be to have the
61 * representation stuff (WebdbImage) happen here.
64 * @see mir.media.MirMedia
66 * @version $Id: MediaHandlerImages.java,v 1.17 2003/02/23 05:00:14 zapata Exp $
70 public abstract class MediaHandlerImages implements MirMedia
72 protected static MirPropertiesConfiguration configuration;
73 protected static final String PNG = "PNG";
74 protected static final String JPEG = "JPEG";
76 protected LoggerWrapper logger;
80 configuration = MirPropertiesConfiguration.instance();
82 catch (PropertiesConfigExc e) {
83 throw new RuntimeException("Can't get configuration: " + e.getMessage());
87 abstract String getType();
89 public MediaHandlerImages() {
90 logger = new LoggerWrapper("Media.Images");
93 public InputStream getMedia(Entity ent, Entity mediaTypeEnt)
94 throws MirMediaException
98 in = ((EntityImages)ent).getImage();
100 catch ( StorageObjectFailure e) {
101 logger.error("MediaHandlerImages.getImage: "+e.toString());
102 throw new MirMediaException(e.toString());
108 public void set(InputStream in, Entity ent, Entity mediaTypeEnt)
109 throws MirMediaException {
112 ((EntityImages)ent).setImage(in, getType());
114 catch ( StorageObjectFailure e) {
115 logger.error("MediaHandlerImages.set: "+e.getMessage());
116 e.printStackTrace(logger.asPrintWriter(logger.DEBUG_MESSAGE));
117 throw new MirMediaException(e.getMessage());
121 public void produce(Entity ent, Entity mediaTypeEnt) throws MirMediaException
123 String date = ent.getValue("date");
124 String datePath = StringUtil.webdbDate2path(date);
125 String ext = "."+mediaTypeEnt.getValue("name");
126 String filepath = datePath+ent.getId()+ext;
127 String iconFilePath = configuration.getString("Producer.StorageRoot")
128 +getIconStoragePath() + filepath;
129 String productionFilePath = getStoragePath() + File.separator + filepath;
132 if (ent.getValue("icon_data")!= null &&
133 ent.getValue("image_data")!= null) {
136 InputStream in = ((EntityImages)ent).getIcon();
137 FileUtil.write(iconFilePath, in);
138 in = ((EntityImages)ent).getImage();
139 FileUtil.write(productionFilePath, in);
140 ent.setValueForProperty("icon_path",getIconStoragePath()+filepath);
141 ent.setValueForProperty("publish_path",filepath);
144 catch (Throwable e) {
145 logger.error("MediaHandlerImages.produce: " + e.toString());
146 throw new MirMediaException("MediaHandlerImages.produce: " + e.toString());
150 logger.error("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
151 throw new MirMediaException("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
156 public InputStream getIcon(Entity ent) throws MirMediaException
160 in = ((EntityImages)ent).getIcon();
162 catch (Throwable e) {
163 logger.error("MediaHandlerImages.getIcon: " + e.toString());
164 throw new MirMediaException(e.toString());
170 public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
172 SimpleList theList = new SimpleList();
177 public String getStoragePath()
179 return configuration.getString("Producer.Image.Path");
182 public String getIconStoragePath()
184 return configuration.getString("Producer.Image.IconPath");
187 public String getPublishHost()
189 return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
192 public String getTinyIconName()
194 return configuration.getString("Producer.Icon.TinyImage");
197 public String getBigIconName()
199 return configuration.getString("Producer.Icon.BigImage");
202 public String getIconAltName()
207 public boolean isVideo()
212 public boolean isAudio()
217 public boolean isImage ()
222 public String getDescr(Entity mediaType)