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