organizing imports
[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 mir.config.MirPropertiesConfiguration;\r
40 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
41 import mir.entity.Entity;\r
42 import mir.log.LoggerWrapper;\r
43 import mir.media.MirMedia;\r
44 import mir.media.MirMediaException;\r
45 import mir.misc.FileUtil;\r
46 import mir.misc.StringUtil;\r
47 import freemarker.template.SimpleList;\r
48 \r
49 \r
50 /**\r
51  * This is the Generic MediaHandler. It stores the media data on\r
52  * the filesystem and keeps basic metadata  (size, type...) in the\r
53  * DB. Usually only representation needs to be overridden.\r
54  * See the MediaHandlerAudio class to see an example of how one\r
55  * could override it.\r
56  * <p>\r
57  * Most media handlers should override this class.\r
58  * <p>\r
59  * In theory, it could be used to handle miscellaneous media that\r
60  * we don't have entered in the media_type table, (like RTF documents,\r
61  * PS, PDF, etc..)\r
62  * <p>\r
63  * Of course it implements the MirMedia interface.\r
64  *\r
65  * @see mir.media.MirMedia\r
66  * @author mh <mh@nadir.org>\r
67  * @version $Id: MediaHandlerGeneric.java,v 1.15 2003/03/05 19:23:16 idfx Exp $\r
68  */\r
69 \r
70 public class MediaHandlerGeneric implements MirMedia\r
71 {\r
72     protected static MirPropertiesConfiguration configuration;\r
73     protected static String imageHost;\r
74     protected static String imageRoot;\r
75 \r
76     protected LoggerWrapper logger;\r
77 \r
78     static {\r
79       try {\r
80         configuration = MirPropertiesConfiguration.instance();\r
81       } catch (PropertiesConfigExc e) {\r
82         e.printStackTrace();\r
83       }\r
84       imageHost = configuration.getString("Producer.Image.Host");\r
85       imageRoot = configuration.getString("Producer.ImageRoot");\r
86     }\r
87 \r
88     public MediaHandlerGeneric() {\r
89       logger = new LoggerWrapper("Media.Generic");\r
90     }\r
91 \r
92     public void set (InputStream in, Entity ent, Entity mediaTypeEnt ) throws MirMediaException {\r
93       String ext = mediaTypeEnt.getValue("name");\r
94       String mediaFname = ent.getId() + "." + ext;\r
95       String date = ent.getValue("date");\r
96       String datePath = StringUtil.webdbDate2path(date);\r
97       try {\r
98         long size = FileUtil.write(getStoragePath() + File.separator + datePath +\r
99                                    File.separator + mediaFname, in);\r
100         ent.setValueForProperty("publish_path", datePath + mediaFname);\r
101         ent.setValueForProperty("size", new Long(size).toString());\r
102         ent.update();\r
103       }\r
104       catch (Throwable e) {\r
105         logger.error("MediaHandlerGeneric.set: " + e.toString());\r
106         throw new MirMediaException(e.toString());\r
107       }\r
108     }\r
109 \r
110     public void produce (Entity ent, Entity mediaTypeEnt )\r
111       throws MirMediaException {\r
112 \r
113       //check first if the media file exist since produced\r
114       //location is also the storage location\r
115       String date = ent.getValue("date");\r
116       String datePath = StringUtil.webdbDate2path(date);\r
117       String relPath = datePath+ent.getId()+"."+mediaTypeEnt.getValue("name");\r
118       String fname = getStoragePath()+relPath;\r
119       if(! new File(fname).exists())\r
120         throw new MirMediaException("error in MirMedia.produce(): "+relPath+\r
121                                     " does not exist!");\r
122     }\r
123 \r
124     public InputStream getMedia (Entity ent, Entity mediaTypeEnt)\r
125       throws MirMediaException {\r
126       String publishPath = ent.getValue("publish_path");\r
127       String fname = getStoragePath()+publishPath;\r
128       File f = new File(fname);\r
129       if(! f.exists())\r
130         throw new MirMediaException("error in MirMedia.getMedia(): "+fname+\r
131                                     " does not exist!");\r
132       FileInputStream in;\r
133       try {\r
134         in = new FileInputStream(f);\r
135       } catch (IOException e) {\r
136         throw new MirMediaException("getMedia(): "+e.toString());\r
137       }\r
138       return in;\r
139     }\r
140 \r
141     public InputStream getIcon (Entity ent) throws MirMediaException {\r
142         return null;\r
143     }\r
144 \r
145     public String getStoragePath()\r
146     {\r
147         return configuration.getString("Producer.Media.Path");\r
148     }\r
149 \r
150     public String getIconStoragePath()\r
151     {\r
152         return configuration.getString("Producer.Image.IconPath");\r
153     }\r
154 \r
155     public String getPublishHost()\r
156     {\r
157         return StringUtil.removeSlash(configuration.getString("Producer.Media.Host"));\r
158     }\r
159 \r
160     public String getTinyIconName()\r
161     {\r
162         return configuration.getString("Producer.Icon.TinyText");\r
163     }\r
164 \r
165     public String getBigIconName()\r
166     {\r
167         return configuration.getString("Producer.Icon.BigText");\r
168     }\r
169 \r
170     public String getIconAltName()\r
171     {\r
172         return "Generic media";\r
173     }\r
174 \r
175     public SimpleList getURL(Entity ent, Entity mediaTypeEnt)\r
176     {\r
177       SimpleList theList = new SimpleList();\r
178       theList.add(ent);\r
179       return theList;\r
180     }\r
181 \r
182     public boolean isVideo()\r
183     {\r
184       return false;\r
185     }\r
186 \r
187     public boolean isAudio()\r
188     {\r
189       return false;\r
190     }\r
191 \r
192     public boolean isImage()\r
193     {\r
194       return false;\r
195     }\r
196 \r
197     public String getDescr( Entity mediaType)\r
198     {\r
199       return mediaType.getValue("mime_type");\r
200     }\r
201 \r
202 }\r
203 \r
204 \r
205 \r