1710c955f6dfee356a238e5f57ca590ee71bda29
[mir.git] / source / mir / media / MediaHandlerGeneric.java
1 /*
2  * put your module comment here
3  */
4
5
6 package  mir.media;
7
8 import java.util.*;
9
10 import mir.entity.*;
11 import mir.misc.*;
12 import mir.storage.*;
13
14
15 /**
16  * This is the Generic MediaHandler. It stores the media data on
17  * the filesystem and keeps basic metadata  (size, type...) in the
18  * DB. Usually only representation needs to be overridden.
19  * See the MediaHandlerAudio class to see an example of how one
20  * could override it.
21  * <p>
22  * Most media handlers should override this class.
23  * <p>
24  * In theory, it could be used to handle miscellaneous media that
25  * we don't have entered in the media_type table, (like RTF documents,
26  * PS, PDF, etc..)
27  * <p>
28  * Of course it implements the MirMedia interface.
29  *
30  * @see mir.media.MirMedia
31  * @author mh <heckmann@hbe.ca>
32  * @version 24.09.2001
33  */
34
35 public class MediaHandlerGeneric implements MirMedia
36 {
37
38     private String imageHost = MirConfig.getProp("Producer.Image.Host");
39     private static String imageRoot = MirConfig.getProp("Producer.ImageRoot");
40     private Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home")+"log/media.log");
41     public boolean set (byte[] uploadedData, Entity ent, Entity mediaTypeEnt ) {
42         String ext = mediaTypeEnt.getValue("name");
43         String dir = MirConfig.getProp("Producer.Media.Path");
44         String mediaHost = MirConfig.getProp("Producer.Media.Host");
45         String mediaFname = ent.getId()+"."+ext;
46         String date = ent.getValue("date");
47         String datePath = StringUtil.webdbDate2path(date);
48         Integer size = new Integer(uploadedData.length);
49         //hack: make it a config option to use "dated" dirs
50         //we can't cause of stallman -mh
51         //if(FileUtil.write(dir+"/"+datePath+"/"+mediaFname, uploadedData)) {
52         if(FileUtil.write(dir+"/"+mediaFname, uploadedData)) {
53             //were done with the data, dereference.
54             uploadedData=null;
55             
56             try {
57                 ent.setValueForProperty("is_produced", "1");
58                 ent.setValueForProperty("icon_is_produced", "1");
59                 //hack: make it a config option to use "dated" dirs
60                 //we can't cause of stallman -mh
61                 //ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
62                 ent.setValueForProperty("publish_path", mediaFname);
63                 ent.setValueForProperty("publish_server", mediaHost);
64                 ent.setValueForProperty("size", size.toString());
65                 ent.update();
66             } catch (StorageObjectException e) {
67                 theLog.printError("StorageObjectException: "+e.toString()); 
68                 return false;
69             }
70         } else {
71             theLog.printError("could not write FILE!"); 
72             return false;
73         }
74
75         return true;
76     }
77
78     //a class that will probably never get used..
79     private byte[] getFile (String fileName) {
80         long size = FileUtil.getSize(fileName);
81         if (size < 0) return null;
82
83         byte[] container = new byte[(int)size];
84             
85         if(!FileUtil.read(fileName, container))
86             return null;
87
88         return container;
89     }
90
91     public byte[] get (Entity ent, Entity mediaTypeEnt) {
92         return null;
93     }
94
95     public byte[] getIcon (Entity ent) {
96         String name = "/path/to/some/generic/icon";
97         return getFile(name);
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 title = ent.getValue("title")+
133         " - "+mediaTypeEnt.getValue("name")+" "+
134         ent.getValue("size")+" Bytes";
135       return StringUtil.createURLLinks(ent.getValue("publish_server")+"/"+
136         ent.getValue("publish_path"), title, imageRoot, getBigIcon());
137     }
138
139     public String getListView(Entity ent, Entity mediaTypeEnt)
140     {
141       //String title = ent.getValue("title")+
142       //  " - "+mediaTypeEnt.getValue("name")+" "+
143       //  ent.getValue("size")+" Bytes";
144       return StringUtil.createIMGLinks(imageHost+
145         getBigIcon(), null, null, null);
146     }
147
148     public boolean isVideo()
149     {
150         return false;
151     }
152
153     public boolean isAudio()
154     {
155         return false;
156     }
157
158     public boolean isImage()
159     {
160         return false;
161     }
162
163 }
164         
165         
166