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
34 import java.io.File;
\r
35 import java.io.FileInputStream;
\r
36 import java.io.IOException;
\r
37 import java.io.InputStream;
\r
39 import freemarker.template.SimpleList;
\r
41 import mir.log.LoggerWrapper;
\r
43 import mir.config.MirPropertiesConfiguration;
\r
44 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
\r
45 import mir.entity.Entity;
\r
46 import mir.media.MirMedia;
\r
47 import mir.media.MirMediaException;
\r
48 import mir.misc.FileUtil;
\r
49 import mir.misc.StringUtil;
\r
53 * This is the Generic MediaHandler. It stores the media data on
\r
54 * the filesystem and keeps basic metadata (size, type...) in the
\r
55 * DB. Usually only representation needs to be overridden.
\r
56 * See the MediaHandlerAudio class to see an example of how one
\r
57 * could override it.
\r
59 * Most media handlers should override this class.
\r
61 * In theory, it could be used to handle miscellaneous media that
\r
62 * we don't have entered in the media_type table, (like RTF documents,
\r
65 * Of course it implements the MirMedia interface.
\r
67 * @see mir.media.MirMedia
\r
68 * @author mh <mh@nadir.org>
\r
69 * @version $Id: MediaHandlerGeneric.java,v 1.14 2003/02/23 05:00:14 zapata Exp $
\r
72 public class MediaHandlerGeneric implements MirMedia
\r
74 protected static MirPropertiesConfiguration configuration;
\r
75 protected static String imageHost;
\r
76 protected static String imageRoot;
\r
78 protected LoggerWrapper logger;
\r
82 configuration = MirPropertiesConfiguration.instance();
\r
83 } catch (PropertiesConfigExc e) {
\r
84 e.printStackTrace();
\r
86 imageHost = configuration.getString("Producer.Image.Host");
\r
87 imageRoot = configuration.getString("Producer.ImageRoot");
\r
90 public MediaHandlerGeneric() {
\r
91 logger = new LoggerWrapper("Media.Generic");
\r
94 public void set (InputStream in, Entity ent, Entity mediaTypeEnt ) throws MirMediaException {
\r
95 String ext = mediaTypeEnt.getValue("name");
\r
96 String mediaFname = ent.getId() + "." + ext;
\r
97 String date = ent.getValue("date");
\r
98 String datePath = StringUtil.webdbDate2path(date);
\r
100 long size = FileUtil.write(getStoragePath() + File.separator + datePath +
\r
101 File.separator + mediaFname, in);
\r
102 ent.setValueForProperty("publish_path", datePath + mediaFname);
\r
103 ent.setValueForProperty("size", new Long(size).toString());
\r
106 catch (Throwable e) {
\r
107 logger.error("MediaHandlerGeneric.set: " + e.toString());
\r
108 throw new MirMediaException(e.toString());
\r
112 public void produce (Entity ent, Entity mediaTypeEnt )
\r
113 throws MirMediaException {
\r
115 //check first if the media file exist since produced
\r
116 //location is also the storage location
\r
117 String date = ent.getValue("date");
\r
118 String datePath = StringUtil.webdbDate2path(date);
\r
119 String relPath = datePath+ent.getId()+"."+mediaTypeEnt.getValue("name");
\r
120 String fname = getStoragePath()+relPath;
\r
121 if(! new File(fname).exists())
\r
122 throw new MirMediaException("error in MirMedia.produce(): "+relPath+
\r
123 " does not exist!");
\r
126 public InputStream getMedia (Entity ent, Entity mediaTypeEnt)
\r
127 throws MirMediaException {
\r
128 String publishPath = ent.getValue("publish_path");
\r
129 String fname = getStoragePath()+publishPath;
\r
130 File f = new File(fname);
\r
132 throw new MirMediaException("error in MirMedia.getMedia(): "+fname+
\r
133 " does not exist!");
\r
134 FileInputStream in;
\r
136 in = new FileInputStream(f);
\r
137 } catch (IOException e) {
\r
138 throw new MirMediaException("getMedia(): "+e.toString());
\r
143 public InputStream getIcon (Entity ent) throws MirMediaException {
\r
147 public String getStoragePath()
\r
149 return configuration.getString("Producer.Media.Path");
\r
152 public String getIconStoragePath()
\r
154 return configuration.getString("Producer.Image.IconPath");
\r
157 public String getPublishHost()
\r
159 return StringUtil.removeSlash(configuration.getString("Producer.Media.Host"));
\r
162 public String getTinyIconName()
\r
164 return configuration.getString("Producer.Icon.TinyText");
\r
167 public String getBigIconName()
\r
169 return configuration.getString("Producer.Icon.BigText");
\r
172 public String getIconAltName()
\r
174 return "Generic media";
\r
177 public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
\r
179 SimpleList theList = new SimpleList();
\r
184 public boolean isVideo()
\r
189 public boolean isAudio()
\r
194 public boolean isImage()
\r
199 public String getDescr( Entity mediaType)
\r
201 return mediaType.getValue("mime_type");
\r