012c21e3bd9af546def17d03b30bcf5458472a25
[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 abstract class MediaHandlerImages implements MirMedia
36 {
37   static Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home")+
38                                                 "log/media.log");
39   static final String PNG = "PNG"; 
40   static final String JPEG = "JPEG"; 
41
42   abstract String getType();
43
44         public byte[] get(Entity ent, Entity mediaTypeEnt)
45     throws MirMediaException
46         {
47     byte[] image_data = null;
48
49     try {
50       image_data = ((EntityImages)ent).getImage();
51     } catch ( StorageObjectException e) {
52       theLog.printDebugInfo("MediaHandlerImages.get: "+e.toString()); 
53       throw new MirMediaException(e.toString());
54     }
55
56     return image_data;
57   }
58
59         public boolean set(byte[] uploadData, Entity ent, Entity mediaTypeEnt)
60     throws MirMediaException {
61
62     try {
63       ((EntityImages)ent).setImage(uploadData, getType());
64     } catch ( StorageObjectException e) {
65       theLog.printError("MediaHandlerImages.set: "+e.toString()); 
66       throw new MirMediaException(e.toString());
67     }
68     //deref. -mh
69     uploadData=null;
70
71     return true;
72         }
73
74   public void produce(Entity ent, Entity mediaTypeEnt) throws MirMediaException
75   {
76     String date = ent.getValue("date");
77     String datePath = StringUtil.webdbDate2path(date);
78     String ext = "."+mediaTypeEnt.getValue("name");
79     String filepath = datePath+ent.getId()+ext;
80     String iconFilePath = MirConfig.getProp("Producer.StorageRoot")
81                           +getIconStoragePath() + filepath;
82     String productionFilePath = getStoragePath() + "/" + filepath;
83
84
85     if (ent.getValue("icon_data")!= null &&
86         ent.getValue("image_data")!= null) {
87       // make icon
88       try {
89         FileUtil.write(iconFilePath,((EntityImages)ent).getIcon());
90         FileUtil.write(productionFilePath,((EntityImages)ent).getImage());
91         ent.setValueForProperty("icon_path",getIconStoragePath()+filepath);
92         ent.setValueForProperty("publish_path",filepath);
93         ent.update();
94       } catch ( Exception e) {
95         String msg = "MediaHandlerImages.produce - Error: " + e.toString();
96         theLog.printError(msg);
97         throw new MirMediaException(msg);
98       }
99     } else {
100       String msg="MediaHandlerImages.produce - missing image or icon OID for: "+
101                   ent.getId();
102       theLog.printError(msg);
103       throw new MirMediaException(msg);
104     }
105   }
106                         
107
108         public byte[] getIcon(Entity ent) throws MirMediaException
109         {
110     byte[] icon_data = null;
111
112     try {
113       icon_data = ((EntityImages)ent).getIcon();
114     } catch ( StorageObjectException e) {
115       theLog.printDebugInfo("MediaHandlerImages.getIcon: "+e.toString()); 
116       throw new MirMediaException(e.toString());
117     }
118
119     return icon_data;
120   }
121
122   public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
123   {
124     SimpleList theList = new SimpleList();
125     theList.add(ent);
126     return theList;
127   }
128
129   public String getStoragePath()
130   {
131     return MirConfig.getProp("Producer.Image.Path");
132   }
133
134   public String getIconStoragePath()
135   {
136     return MirConfig.getProp("Producer.Image.IconPath");
137   }
138
139   public String getPublishHost()
140   {
141     return MirConfig.getProp("Producer.Image.Host");
142   }
143
144   public String getTinyIcon ()
145   {
146     return MirConfig.getProp("Producer.Icon.TinyImage");
147   } 
148
149   public String getBigIcon ()
150   {
151     return MirConfig.getProp("Producer.Icon.BigImage");
152   } 
153
154   public String getIconAlt ()
155   {
156     return "Image";
157   } 
158
159   public boolean isVideo ()
160   {
161     return false;
162   } 
163
164   public boolean isAudio ()
165   {
166     return false;
167   } 
168
169   public boolean isImage ()
170   {
171     return true;
172   } 
173
174   public String getDescr(Entity mediaType)
175   {
176     return "";
177   }
178
179 }