bugfix: url was like http://server/dirfile.suf now http://server/dir/file.suf
[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  * Interfacedefinition für Datenbank-Adpatoren. Die Adaptoren legen
17  * jeweils das Verhalten und die Befehlsmächtigkeit der Datenbank
18  * fest.
19  *
20  * @author <mh>
21  * @version 24.09.2001
22  */
23
24 public class MediaHandlerGeneric implements MirMedia
25 {
26
27     private static String imageRoot = MirConfig.getProp("Producer.ImageRoot");
28     private Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home")+"log/media.log");
29     public boolean set (byte[] uploadedData, Entity ent, Entity mediaTypeEnt ) {
30         String ext = mediaTypeEnt.getValue("name");
31         String dir = MirConfig.getProp("Producer.Media.Path");
32         String mediaHost = MirConfig.getProp("Producer.Media.Host");
33         String mediaFname = ent.getId()+"."+ext;
34         String date = ent.getValue("date");
35         String datePath = StringUtil.webdbDate2path(date);
36         Integer size = new Integer(uploadedData.length);
37         //hack: make it a config option to use "dated" dirs
38         //we can't cause of stallman -mh
39         //if(FileUtil.write(dir+"/"+datePath+"/"+mediaFname, uploadedData)) {
40         if(FileUtil.write(dir+"/"+mediaFname, uploadedData)) {
41             try {
42                 ent.setValueForProperty("is_produced", "1");
43                 ent.setValueForProperty("icon_is_produced", "1");
44                 //hack: make it a config option to use "dated" dirs
45                 //we can't cause of stallman -mh
46                 //ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
47                 ent.setValueForProperty("publish_path", mediaFname);
48                 ent.setValueForProperty("publish_server", mediaHost);
49                 ent.setValueForProperty("size", size.toString());
50                 ent.update();
51             } catch (StorageObjectException e) {
52                 theLog.printError("StorageObjectException: "+e.toString()); 
53                 return false;
54             }
55         } else {
56             theLog.printError("could not write FILE!"); 
57             return false;
58         }
59
60         return true;
61     }
62
63     //a class that will probably never get used..
64     private byte[] getFile (String fileName) {
65         long size = FileUtil.getSize(fileName);
66         if (size < 0) return null;
67
68         byte[] container = new byte[(int)size];
69             
70         if(!FileUtil.read(fileName, container))
71             return null;
72
73         return container;
74     }
75
76     public byte[] get (Entity ent, Entity mediaTypeEnt) {
77         return null;
78     }
79
80     public byte[] getIcon (Entity ent) {
81         String name = "/path/to/some/generic/icon";
82         return getFile(name);
83     }
84
85     public String getStoragePath()
86     {
87         return MirConfig.getProp("Producer.Media.Path");
88     }
89
90     public String getIconStoragePath()
91     {
92         return MirConfig.getProp("Producer.Image.IconPath");
93     }
94
95     public String getPublishHost()
96     {
97         return MirConfig.getProp("Producer.Media.Host");
98     }
99
100     public String getTinyIcon()
101     {
102         return MirConfig.getProp("Producer.Icon.TinyText");
103     }
104
105     public String getBigIcon()
106     {
107         return MirConfig.getProp("Producer.Icon.BigText");
108     }
109
110     public String getIconAlt()
111     {
112         return "Generic media"; 
113     }
114
115     public String getURL(Entity ent, Entity mediaTypeEnt)
116     {
117       String title = ent.getValue("title")+
118         " - "+mediaTypeEnt.getValue("name")+" "+
119         ent.getValue("size")+" Bytes";
120       return StringUtil.createURLLinks(ent.getValue("publish_server")+"/"+
121         ent.getValue("publish_path"), title, imageRoot, getBigIcon());
122     }
123
124     public boolean isVideo()
125     {
126         return false;
127     }
128
129     public boolean isAudio()
130     {
131         return false;
132     }
133
134     public boolean isImage()
135     {
136         return false;
137     }
138
139 }
140         
141         
142