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