4953b85289c0cd873d86ffac7341f3a45fa2d3b4
[mir.git] / source / mircoders / media / MediaHandlerGeneric.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
30  */
31
32 package  mircoders.media;
33
34 import java.util.*;
35 import java.io.*;
36
37 import freemarker.template.SimpleList;
38
39 import mir.media.*;
40 import mir.entity.*;
41 import mir.misc.*;
42 import mir.storage.*;
43
44
45 /**
46  * This is the Generic MediaHandler. It stores the media data on
47  * the filesystem and keeps basic metadata  (size, type...) in the
48  * DB. Usually only representation needs to be overridden.
49  * See the MediaHandlerAudio class to see an example of how one
50  * could override it.
51  * <p>
52  * Most media handlers should override this class.
53  * <p>
54  * In theory, it could be used to handle miscellaneous media that
55  * we don't have entered in the media_type table, (like RTF documents,
56  * PS, PDF, etc..)
57  * <p>
58  * Of course it implements the MirMedia interface.
59  *
60  * @see mir.media.MirMedia
61  * @author mh <heckmann@hbe.ca>
62  * @version 24.09.2001
63  */
64
65 public class MediaHandlerGeneric implements MirMedia
66 {
67     protected static String imageHost = MirConfig.getProp("Producer.Image.Host");
68     protected static String imageRoot = MirConfig.getProp("Producer.ImageRoot");
69     protected static Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home")+
70                                                   "log/media.log");
71     public boolean set (byte[] uploadedData, Entity ent, Entity mediaTypeEnt )
72         throws MirMediaException {
73
74         String ext = mediaTypeEnt.getValue("name");
75         String mediaFname = ent.getId()+"."+ext;
76         String date = ent.getValue("date");
77         String datePath = StringUtil.webdbDate2path(date);
78         Integer size = new Integer(uploadedData.length);
79         try {
80             FileUtil.write(getStoragePath()+"/"+datePath+"/"+mediaFname,
81                             uploadedData);
82             //were done with the data, dereference.
83             uploadedData=null;
84             
85             ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
86             ent.setValueForProperty("size", size.toString());
87             ent.update();
88         } catch (Exception e) {
89             theLog.printError(e.toString()); 
90             throw new MirMediaException(e.toString());
91         }
92
93         return true;
94     }
95
96     public void produce (Entity ent, Entity mediaTypeEnt )
97       throws MirMediaException {
98       
99       //check first if the media file exist since produced
100       //location is also the storage location
101       String date = ent.getValue("date");
102       String datePath = StringUtil.webdbDate2path(date);
103       String relPath = datePath+ent.getId()+"."+mediaTypeEnt.getValue("name");
104       String fname = getStoragePath()+relPath;
105       if(! new File(fname).exists())
106         throw new MirMediaException("error in MirMedia.produce(): "+relPath+
107                                     "does not exist!");
108     }
109       
110
111     //a method that will probably never get used..
112     private byte[] getFile (String fileName)
113         throws MirMediaException {
114
115         long size = FileUtil.getSize(fileName);
116         if (size < 0) return null;
117
118         byte[] container = new byte[(int)size];
119             
120         try {
121             FileUtil.read(fileName, container);
122         } catch (Exception e) {
123             theLog.printError(e.toString()); 
124             throw new MirMediaException(e.toString());
125         }
126
127         return container;
128     }
129
130     public byte[] get (Entity ent, Entity mediaTypeEnt) {
131         return null;
132     }
133
134     public byte[] getIcon (Entity ent) {
135         return null;
136     }
137
138     public String getStoragePath()
139     {
140         return MirConfig.getProp("Producer.Media.Path");
141     }
142
143     public String getIconStoragePath()
144     {
145         return MirConfig.getProp("Producer.Image.IconPath");
146     }
147
148     public String getPublishHost()
149     {
150         return MirConfig.getProp("Producer.Media.Host");
151     }
152
153     public String getTinyIcon()
154     {
155         return MirConfig.getProp("Producer.Icon.TinyText");
156     }
157
158     public String getBigIcon()
159     {
160         return MirConfig.getProp("Producer.Icon.BigText");
161     }
162
163     public String getIconAlt()
164     {
165         return "Generic media"; 
166     }
167
168     public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
169     {
170       SimpleList theList = new SimpleList();
171       theList.add(ent);
172       return theList;
173     }
174
175     public boolean isVideo()
176     {
177       return false;
178     }
179
180     public boolean isAudio()
181     {
182       return false;
183     }
184
185     public boolean isImage()
186     {
187       return false;
188     }
189
190     public String getDescr( Entity mediaType)
191     {
192       return mediaType.getValue("mime_type");
193     }
194
195 }
196         
197         
198