various fixes/cleanup: old producers are now completely gone, old logfile class too
[mir.git] / source / mircoders / media / MediaHandlerGeneric.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
30  */\r
31 \r
32 package  mircoders.media;\r
33 \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
38 \r
39 import freemarker.template.SimpleList;\r
40 \r
41 import mir.log.LoggerWrapper;\r
42 \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
50 \r
51 \r
52 /**\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
58  * <p>\r
59  * Most media handlers should override this class.\r
60  * <p>\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
63  * PS, PDF, etc..)\r
64  * <p>\r
65  * Of course it implements the MirMedia interface.\r
66  *\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
70  */\r
71 \r
72 public class MediaHandlerGeneric implements MirMedia\r
73 {\r
74     protected static MirPropertiesConfiguration configuration;\r
75     protected static String imageHost;\r
76     protected static String imageRoot;\r
77 \r
78     protected LoggerWrapper logger;\r
79 \r
80     static {\r
81       try {\r
82         configuration = MirPropertiesConfiguration.instance();\r
83       } catch (PropertiesConfigExc e) {\r
84         e.printStackTrace();\r
85       }\r
86       imageHost = configuration.getString("Producer.Image.Host");\r
87       imageRoot = configuration.getString("Producer.ImageRoot");\r
88     }\r
89 \r
90     public MediaHandlerGeneric() {\r
91       logger = new LoggerWrapper("Media.Generic");\r
92     }\r
93 \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
99       try {\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
104         ent.update();\r
105       }\r
106       catch (Throwable e) {\r
107         logger.error("MediaHandlerGeneric.set: " + e.toString());\r
108         throw new MirMediaException(e.toString());\r
109       }\r
110     }\r
111 \r
112     public void produce (Entity ent, Entity mediaTypeEnt )\r
113       throws MirMediaException {\r
114 \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
124     }\r
125 \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
131       if(! f.exists())\r
132         throw new MirMediaException("error in MirMedia.getMedia(): "+fname+\r
133                                     " does not exist!");\r
134       FileInputStream in;\r
135       try {\r
136         in = new FileInputStream(f);\r
137       } catch (IOException e) {\r
138         throw new MirMediaException("getMedia(): "+e.toString());\r
139       }\r
140       return in;\r
141     }\r
142 \r
143     public InputStream getIcon (Entity ent) throws MirMediaException {\r
144         return null;\r
145     }\r
146 \r
147     public String getStoragePath()\r
148     {\r
149         return configuration.getString("Producer.Media.Path");\r
150     }\r
151 \r
152     public String getIconStoragePath()\r
153     {\r
154         return configuration.getString("Producer.Image.IconPath");\r
155     }\r
156 \r
157     public String getPublishHost()\r
158     {\r
159         return StringUtil.removeSlash(configuration.getString("Producer.Media.Host"));\r
160     }\r
161 \r
162     public String getTinyIconName()\r
163     {\r
164         return configuration.getString("Producer.Icon.TinyText");\r
165     }\r
166 \r
167     public String getBigIconName()\r
168     {\r
169         return configuration.getString("Producer.Icon.BigText");\r
170     }\r
171 \r
172     public String getIconAltName()\r
173     {\r
174         return "Generic media";\r
175     }\r
176 \r
177     public SimpleList getURL(Entity ent, Entity mediaTypeEnt)\r
178     {\r
179       SimpleList theList = new SimpleList();\r
180       theList.add(ent);\r
181       return theList;\r
182     }\r
183 \r
184     public boolean isVideo()\r
185     {\r
186       return false;\r
187     }\r
188 \r
189     public boolean isAudio()\r
190     {\r
191       return false;\r
192     }\r
193 \r
194     public boolean isImage()\r
195     {\r
196       return false;\r
197     }\r
198 \r
199     public String getDescr( Entity mediaType)\r
200     {\r
201       return mediaType.getValue("mime_type");\r
202     }\r
203 \r
204 }\r
205 \r
206 \r
207 \r