2 * put your module comment here
6 package mircoders.media;
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
24 * Most media handlers should override this class.
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,
30 * Of course it implements the MirMedia interface.
32 * @see mir.media.MirMedia
33 * @author mh <heckmann@hbe.ca>
37 public class MediaHandlerGeneric implements MirMedia
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")+
43 public boolean set (byte[] uploadedData, Entity ent, Entity mediaTypeEnt )
44 throws MirMediaException {
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);
52 FileUtil.write(getStoragePath()+"/"+datePath+"/"+mediaFname,
54 //were done with the data, dereference.
57 ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
58 ent.setValueForProperty("size", size.toString());
60 } catch (Exception e) {
61 theLog.printError(e.toString());
62 throw new MirMediaException(e.toString());
68 public void produce (Entity ent, Entity mediaTypeEnt )
69 throws MirMediaException {
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+
83 //a method that will probably never get used..
84 private byte[] getFile (String fileName)
85 throws MirMediaException {
87 long size = FileUtil.getSize(fileName);
88 if (size < 0) return null;
90 byte[] container = new byte[(int)size];
93 FileUtil.read(fileName, container);
94 } catch (Exception e) {
95 theLog.printError(e.toString());
96 throw new MirMediaException(e.toString());
102 public byte[] get (Entity ent, Entity mediaTypeEnt) {
106 public byte[] getIcon (Entity ent) {
110 public String getStoragePath()
112 return MirConfig.getProp("Producer.Media.Path");
115 public String getIconStoragePath()
117 return MirConfig.getProp("Producer.Image.IconPath");
120 public String getPublishHost()
122 return MirConfig.getProp("Producer.Media.Host");
125 public String getTinyIcon()
127 return MirConfig.getProp("Producer.Icon.TinyText");
130 public String getBigIcon()
132 return MirConfig.getProp("Producer.Icon.BigText");
135 public String getIconAlt()
137 return "Generic media";
140 public String getURL(Entity ent, Entity mediaTypeEnt)
142 String stringSize = ent.getValue("size");
143 if (stringSize == null)
145 int size = Integer.parseInt(stringSize, 10)/1024;
146 String title = ent.getValue("title")+
147 " - "+mediaTypeEnt.getValue("name")+" "+
149 return StringUtil.createURLLinks(ent.getValue("publish_server")+"/"+
150 ent.getValue("publish_path"), title, imageRoot, getBigIcon());
153 public String getListView(Entity ent, Entity mediaTypeEnt)
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);
162 public boolean isVideo()
167 public boolean isAudio()
172 public boolean isImage()