code cleaning, new config
[mir.git] / source / mircoders / media / MediaHandlerImages.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32
33 package mircoders.media;
34
35 import java.io.File;
36 import java.io.InputStream;
37
38 import mir.config.MirPropertiesConfiguration;
39 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
40 import mir.entity.Entity;
41 import mir.media.MirMedia;
42 import mir.media.MirMediaException;
43 import mir.misc.FileUtil;
44 import mir.misc.Logfile;
45 import mir.misc.StringUtil;
46 import mir.storage.StorageObjectFailure;
47 import mircoders.entity.EntityImages;
48 import freemarker.template.SimpleList;
49
50 /**
51  * This class handles saving, fetching creating representations
52  * for all images. The image content is stored in the DB. The content is
53  * written out to a file at the ProducerImages level.
54  * Remember that Handlers for specific image types, Gif, Jpeg, etc..
55  * should override it.
56  * It implements the MirMedia interface.
57  * <p>
58  * slowly starting to look better, a next step would be to have the
59  * representation stuff (WebdbImage) happen here.
60  * -mh 01.03.2002
61  *
62  * @see mir.media.MirMedia
63  * @author mh
64  * @version $Id: MediaHandlerImages.java,v 1.16 2003/01/25 17:50:35 idfx Exp $
65  */
66
67
68 public abstract class MediaHandlerImages implements MirMedia
69 {
70   static MirPropertiesConfiguration configuration;
71   static Logfile theLog;
72   static final String PNG = "PNG";
73   static final String JPEG = "JPEG";
74
75   static {
76     try {
77       configuration = MirPropertiesConfiguration.instance();
78     } catch (PropertiesConfigExc e) {
79       e.printStackTrace();
80     }
81     theLog = Logfile.getInstance(configuration.getString("Home")+"log/media.log");
82   }
83   
84   abstract String getType();
85
86         public InputStream getMedia(Entity ent, Entity mediaTypeEnt)
87     throws MirMediaException
88         {
89     InputStream in;
90     try {
91       in = ((EntityImages)ent).getImage();
92     } catch ( StorageObjectFailure e) {
93       theLog.printDebugInfo("MediaHandlerImages.getImage: "+e.toString());
94       throw new MirMediaException(e.toString());
95     }
96
97     return in;
98   }
99
100   public void set(InputStream in, Entity ent, Entity mediaTypeEnt)
101     throws MirMediaException {
102
103     try {
104       ((EntityImages)ent).setImage(in, getType());
105     }
106     catch ( StorageObjectFailure e) {
107       e.printStackTrace(System.out);
108       theLog.printError("MediaHandlerImages.set: "+e.getMessage());
109       throw new MirMediaException(e.getMessage());
110     }
111   }
112
113   public void produce(Entity ent, Entity mediaTypeEnt) throws MirMediaException
114   {
115     String date = ent.getValue("date");
116     String datePath = StringUtil.webdbDate2path(date);
117     String ext = "."+mediaTypeEnt.getValue("name");
118     String filepath = datePath+ent.getId()+ext;
119     String iconFilePath = configuration.getString("Producer.StorageRoot")
120                           +getIconStoragePath() + filepath;
121     String productionFilePath = getStoragePath() + File.separator + filepath;
122
123
124     if (ent.getValue("icon_data")!= null &&
125         ent.getValue("image_data")!= null) {
126       // make icon
127       try {
128         InputStream in = ((EntityImages)ent).getIcon();
129         FileUtil.write(iconFilePath, in);
130         in = ((EntityImages)ent).getImage();
131         FileUtil.write(productionFilePath, in);
132         ent.setValueForProperty("icon_path",getIconStoragePath()+filepath);
133         ent.setValueForProperty("publish_path",filepath);
134         ent.update();
135       } catch ( Exception e) {
136         String msg = "MediaHandlerImages.produce - Error: " + e.toString();
137         theLog.printError(msg);
138         throw new MirMediaException(msg);
139       }
140     } else {
141       String msg="MediaHandlerImages.produce - missing image or icon OID for: "+
142                   ent.getId();
143       theLog.printError(msg);
144       throw new MirMediaException(msg);
145     }
146   }
147
148
149   public InputStream getIcon(Entity ent) throws MirMediaException
150   {
151     InputStream in;
152     try {
153       in = ((EntityImages)ent).getIcon();
154     } catch ( StorageObjectFailure e) {
155       theLog.printDebugInfo("MediaHandlerImages.getIcon: "+e.toString());
156       throw new MirMediaException(e.toString());
157     }
158
159     return in;
160   }
161
162   public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
163   {
164     SimpleList theList = new SimpleList();
165     theList.add(ent);
166     return theList;
167   }
168
169   public String getStoragePath()
170   {
171     return configuration.getString("Producer.Image.Path");
172   }
173
174   public String getIconStoragePath()
175   {
176     return configuration.getString("Producer.Image.IconPath");
177   }
178
179   public String getPublishHost()
180   {
181     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
182   }
183
184   public String getTinyIconName()
185   {
186     return configuration.getString("Producer.Icon.TinyImage");
187   }
188
189   public String getBigIconName()
190   {
191     return configuration.getString("Producer.Icon.BigImage");
192   }
193
194   public String getIconAltName()
195   {
196     return "Image";
197   }
198
199   public boolean isVideo()
200   {
201     return false;
202   }
203
204   public boolean isAudio()
205   {
206     return false;
207   }
208
209   public boolean isImage ()
210   {
211     return true;
212   }
213
214   public String getDescr(Entity mediaType)
215   {
216     return "image/jpeg";
217   }
218
219 }