rework of media handling vis a vis storage and producing. making it more
[mir.git] / source / mircoders / media / MediaHandlerImages.java
1
2 package mircoders.media;
3
4 import java.lang.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 import mir.media.*;
10 import mir.misc.*;
11 import mir.entity.*;
12 import mir.storage.StorageObjectException;
13 import mircoders.entity.EntityImages;
14
15 /**
16  * This class handles saving, fetching creating representations
17  * for all images. The image content is stored in the DB. The content is
18  * written out to a file at the ProducerImages level.
19  * Remember that Handlers for specific image types, Gif, Jpeg, etc..
20  * should override it.
21  * It implements the MirMedia interface.
22  * <p>
23  * slowly starting to look better, a next step would be to have the
24  * representation stuff (WebdbImage) happen here.
25  * -mh 01.03.2002
26  *
27  * @see mir.media.MirMedia
28  * @author mh
29  * @version 24.09.2001
30  */
31
32
33 public class MediaHandlerImages implements MirMedia
34 {
35   private Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home")+
36                                                 "log/media.log");
37
38         public byte[] get(Entity ent, Entity mediaTypeEnt)
39     throws MirMediaException
40         {
41     byte[] image_data = null;
42
43     try {
44       image_data = ((EntityImages)ent).getImage();
45     } catch ( StorageObjectException e) {
46       theLog.printDebugInfo("MediaHandlerImages.get: "+e.toString()); 
47       throw new MirMediaException(e.toString());
48     }
49
50     return image_data;
51   }
52
53         public boolean set(byte[] uploadData, Entity ent, Entity mediaTypeEnt)
54     throws MirMediaException {
55
56     try {
57       ((EntityImages)ent).setImage(uploadData);
58     } catch ( StorageObjectException e) {
59       theLog.printError("MediaHandlerImages.set: "+e.toString()); 
60       throw new MirMediaException(e.toString());
61     }
62     //deref. -mh
63     uploadData=null;
64
65     return true;
66         }
67
68   public void produce(Entity ent, Entity mediaTypeEnt) throws MirMediaException
69   {
70     String date = ent.getValue("date");
71     String datePath = StringUtil.webdbDate2path(date);
72     String ext = "."+mediaTypeEnt.getValue("name");
73     String filepath = datePath+ent.getId()+ext;
74     String iconFilePath = MirConfig.getProp("Producer.StorageRoot")
75                           +getIconStoragePath() + filepath;
76     String productionFilePath = getStoragePath() + "/" + filepath;
77
78
79     if (ent.getValue("icon_data")!= null &&
80         ent.getValue("image_data")!= null) {
81       // make icon
82       try {
83         FileUtil.write(iconFilePath,((EntityImages)ent).getIcon());
84         FileUtil.write(productionFilePath,((EntityImages)ent).getImage());
85         ent.setValueForProperty("icon_path",getIconStoragePath()+filepath);
86         ent.setValueForProperty("publish_path",filepath);
87         //ent.setValueForProperty("publish_server", getPublishHost());
88         //ent.setValueForProperty("icon_is_produced","1");
89         //ent.setValueForProperty("is_produced","1");
90         ent.update();
91       } catch ( Exception e) {
92         String msg = "MediaHandlerImages.produce - Error: " + e.toString();
93         theLog.printError(msg);
94         throw new MirMediaException(msg);
95       }
96     } else {
97       String msg="MediaHandlerImages.produce - missing image or icon OID for: "+
98                   ent.getId();
99       theLog.printError(msg);
100       throw new MirMediaException(msg);
101     }
102   }
103                         
104
105         public byte[] getIcon(Entity ent) throws MirMediaException
106         {
107     byte[] icon_data = null;
108
109     try {
110       icon_data = ((EntityImages)ent).getIcon();
111     } catch ( StorageObjectException e) {
112       theLog.printDebugInfo("MediaHandlerImages.getIcon: "+e.toString()); 
113       throw new MirMediaException(e.toString());
114     }
115
116     return icon_data;
117   }
118
119   public String getURL(Entity ent, Entity mediaTypeEnt)
120   {
121     String title = ent.getValue("title");
122     return StringUtil.createIMGLinks(ent.getValue("publish_server")+
123     ent.getValue("publish_path"), title, ent.getValue("img_height"),
124     ent.getValue("img_width"));
125   }
126
127   public String getListView(Entity ent, Entity mediaTypeEnt)
128   {
129     //String title = ent.getValue("title");
130     return StringUtil.createIMGLinks(
131       MirConfig.getProp("Producer.ProductionHost")+ent.getValue("icon_path"),
132       null, ent.getValue("icon_height"), ent.getValue("icon_width"));
133   }
134
135   public String getStoragePath()
136   {
137     return MirConfig.getProp("Producer.Image.Path");
138   }
139
140   public String getIconStoragePath()
141   {
142     return MirConfig.getProp("Producer.Image.IconPath");
143   }
144
145   public String getPublishHost()
146   {
147     return MirConfig.getProp("Producer.Image.Host");
148   }
149
150   public String getTinyIcon ()
151   {
152     return MirConfig.getProp("Producer.Icon.TinyImage");
153   } 
154
155   public String getBigIcon ()
156   {
157     return MirConfig.getProp("Producer.Icon.BigImage");
158   } 
159
160   public String getIconAlt ()
161   {
162     return "Image";
163   } 
164
165   public boolean isVideo ()
166   {
167     return false;
168   } 
169
170   public boolean isAudio ()
171   {
172     return false;
173   } 
174
175   public boolean isImage ()
176   {
177     return true;
178   } 
179
180 }