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