neue forms
[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     protected String imageHost = MirConfig.getProp("Producer.Image.Host");
39     protected String imageRoot = MirConfig.getProp("Producer.ImageRoot");
40     protected Logfile theLog = Logfile.getInstance(this.getClass().getName());
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         if(FileUtil.write(dir+"/"+datePath+"/"+mediaFname, uploadedData)) {
50         //if(FileUtil.write(dir+"/"+mediaFname, uploadedData)) {
51             //were done with the data, dereference.
52             uploadedData=null;
53             
54             try {
55                 ent.setValueForProperty("is_produced", "1");
56                 ent.setValueForProperty("icon_is_produced", "1");
57                 ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
58                 //ent.setValueForProperty("publish_path", mediaFname);
59                 ent.setValueForProperty("publish_server", mediaHost);
60                 ent.setValueForProperty("size", size.toString());
61                 ent.update();
62             } catch (StorageObjectException e) {
63                 theLog.printError("StorageObjectException: "+e.toString()); 
64                 return false;
65             }
66         } else {
67             theLog.printError("could not write FILE!"); 
68             return false;
69         }
70
71         return true;
72     }
73
74     //a class that will probably never get used..
75     private byte[] getFile (String fileName) {
76         long size = FileUtil.getSize(fileName);
77         if (size < 0) return null;
78
79         byte[] container = new byte[(int)size];
80             
81         if(!FileUtil.read(fileName, container))
82             return null;
83
84         return container;
85     }
86
87     public byte[] get (Entity ent, Entity mediaTypeEnt) {
88         return null;
89     }
90
91     public byte[] getIcon (Entity ent) {
92         String name = "/path/to/some/generic/icon";
93         return getFile(name);
94     }
95
96     public String getStoragePath()
97     {
98         return MirConfig.getProp("Producer.Media.Path");
99     }
100
101     public String getIconStoragePath()
102     {
103         return MirConfig.getProp("Producer.Image.IconPath");
104     }
105
106     public String getPublishHost()
107     {
108         return MirConfig.getProp("Producer.Media.Host");
109     }
110
111     public String getTinyIcon()
112     {
113         return MirConfig.getProp("Producer.Icon.TinyText");
114     }
115
116     public String getBigIcon()
117     {
118         return MirConfig.getProp("Producer.Icon.BigText");
119     }
120
121     public String getIconAlt()
122     {
123         return "Generic media"; 
124     }
125
126     public String getURL(Entity ent, Entity mediaTypeEnt)
127     {
128       String stringSize = ent.getValue("size");
129       if (stringSize == null)
130         return null;
131       int size = Integer.parseInt(stringSize, 10)/1024;
132       String title = ent.getValue("title")+
133         " - "+mediaTypeEnt.getValue("name")+" "+
134         size+" KB";
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