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