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 mir.config.MirPropertiesConfiguration;
39 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
40 import mir.entity.Entity;
41 import mir.log.LoggerWrapper;
42 import mir.media.MirMedia;
43 import mir.media.MirMediaException;
44 import mir.misc.FileUtil;
45 import mir.misc.StringUtil;
46 import mir.storage.StorageObjectFailure;
47 import mircoders.entity.EntityImages;
48 import freemarker.template.SimpleList;
51 * This class handles saving, fetching creating representations
52 * for all images. The image content is stored in the DB. The content is
53 * written out to a file at the ProducerImages level.
54 * Remember that Handlers for specific image types, Gif, Jpeg, etc..
56 * It implements the MirMedia interface.
58 * slowly starting to look better, a next step would be to have the
59 * representation stuff (WebdbImage) happen here.
62 * @see mir.media.MirMedia
64 * @version $Id: MediaHandlerImages.java,v 1.19 2003/03/05 19:23:16 idfx Exp $
68 public abstract class MediaHandlerImages implements MirMedia
70 protected static MirPropertiesConfiguration configuration;
71 protected static final String PNG = "PNG";
72 protected static final String JPEG = "JPEG";
74 protected LoggerWrapper logger;
78 configuration = MirPropertiesConfiguration.instance();
80 catch (PropertiesConfigExc e) {
81 throw new RuntimeException("Can't get configuration: " + e.getMessage());
85 abstract String getType();
87 public MediaHandlerImages() {
88 logger = new LoggerWrapper("Media.Images");
91 public InputStream getMedia(Entity ent, Entity mediaTypeEnt)
92 throws MirMediaException
96 in = ((EntityImages)ent).getImage();
98 catch ( StorageObjectFailure e) {
99 logger.error("MediaHandlerImages.getImage: "+e.toString());
100 throw new MirMediaException(e.toString());
106 public void set(InputStream in, Entity ent, Entity mediaTypeEnt)
107 throws MirMediaException {
110 ((EntityImages)ent).setImage(in, getType());
112 catch ( StorageObjectFailure e) {
113 logger.error("MediaHandlerImages.set: "+e.getMessage());
114 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
115 throw new MirMediaException(e.getMessage());
119 public void produce(Entity ent, Entity mediaTypeEnt) throws MirMediaException
121 String date = ent.getValue("date");
122 String datePath = StringUtil.webdbDate2path(date);
123 String ext = "."+mediaTypeEnt.getValue("name");
124 String filepath = datePath+ent.getId()+ext;
125 String iconFilePath = configuration.getString("Producer.StorageRoot")
126 +getIconStoragePath() + filepath;
127 String productionFilePath = getStoragePath() + File.separator + filepath;
130 if (ent.getValue("icon_data")!= null &&
131 ent.getValue("image_data")!= null) {
134 InputStream in = ((EntityImages)ent).getIcon();
135 FileUtil.write(iconFilePath, in);
136 in = ((EntityImages)ent).getImage();
137 FileUtil.write(productionFilePath, in);
138 ent.setValueForProperty("icon_path",getIconStoragePath()+filepath);
139 ent.setValueForProperty("publish_path",filepath);
142 catch (Throwable e) {
143 logger.error("MediaHandlerImages.produce: " + e.toString());
144 throw new MirMediaException("MediaHandlerImages.produce: " + e.toString());
148 logger.error("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
149 throw new MirMediaException("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
154 public InputStream getIcon(Entity ent) throws MirMediaException
158 in = ((EntityImages)ent).getIcon();
160 catch (Throwable e) {
161 logger.error("MediaHandlerImages.getIcon: " + e.toString());
162 throw new MirMediaException(e.toString());
168 public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
170 SimpleList theList = new SimpleList();
175 public String getStoragePath()
177 return configuration.getString("Producer.Image.Path");
180 public String getIconStoragePath()
182 return configuration.getString("Producer.Image.IconPath");
185 public String getPublishHost()
187 return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
190 public String getTinyIconName()
192 return configuration.getString("Producer.Icon.TinyImage");
195 public String getBigIconName()
197 return configuration.getString("Producer.Icon.BigImage");
200 public String getIconAltName()
205 public boolean isVideo()
210 public boolean isAudio()
215 public boolean isImage ()
220 public String getDescr(Entity mediaType)