f4f9bc773c2880d5b8042f7d94e54e55714d424b
[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 freemarker.template.SimpleList;
39
40 import mir.config.MirPropertiesConfiguration;
41 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
42 import mir.entity.Entity;
43 import mir.media.MirMedia;
44 import mir.media.MirMediaException;
45 import mir.misc.FileUtil;
46 import mir.misc.StringUtil;
47 import mir.storage.StorageObjectFailure;
48 import mir.log.LoggerWrapper;
49
50 import mircoders.entity.EntityImages;
51
52 /**
53  * This class handles saving, fetching creating representations
54  * for all images. The image content is stored in the DB. The content is
55  * written out to a file at the ProducerImages level.
56  * Remember that Handlers for specific image types, Gif, Jpeg, etc..
57  * should override it.
58  * It implements the MirMedia interface.
59  * <p>
60  * slowly starting to look better, a next step would be to have the
61  * representation stuff (WebdbImage) happen here.
62  * -mh 01.03.2002
63  *
64  * @see mir.media.MirMedia
65  * @author mh
66  * @version $Id: MediaHandlerImages.java,v 1.17 2003/02/23 05:00:14 zapata Exp $
67  */
68
69
70 public abstract class MediaHandlerImages implements MirMedia
71 {
72   protected static MirPropertiesConfiguration configuration;
73   protected static final String PNG = "PNG";
74   protected static final String JPEG = "JPEG";
75
76   protected LoggerWrapper logger;
77
78   static {
79     try {
80       configuration = MirPropertiesConfiguration.instance();
81     }
82     catch (PropertiesConfigExc e) {
83       throw new RuntimeException("Can't get configuration: " + e.getMessage());
84     }
85   }
86
87   abstract String getType();
88
89   public MediaHandlerImages() {
90     logger = new LoggerWrapper("Media.Images");
91   }
92
93   public InputStream getMedia(Entity ent, Entity mediaTypeEnt)
94     throws MirMediaException
95         {
96     InputStream in;
97     try {
98       in = ((EntityImages)ent).getImage();
99     }
100     catch ( StorageObjectFailure e) {
101       logger.error("MediaHandlerImages.getImage: "+e.toString());
102       throw new MirMediaException(e.toString());
103     }
104
105     return in;
106   }
107
108   public void set(InputStream in, Entity ent, Entity mediaTypeEnt)
109     throws MirMediaException {
110
111     try {
112       ((EntityImages)ent).setImage(in, getType());
113     }
114     catch ( StorageObjectFailure e) {
115       logger.error("MediaHandlerImages.set: "+e.getMessage());
116       e.printStackTrace(logger.asPrintWriter(logger.DEBUG_MESSAGE));
117       throw new MirMediaException(e.getMessage());
118     }
119   }
120
121   public void produce(Entity ent, Entity mediaTypeEnt) throws MirMediaException
122   {
123     String date = ent.getValue("date");
124     String datePath = StringUtil.webdbDate2path(date);
125     String ext = "."+mediaTypeEnt.getValue("name");
126     String filepath = datePath+ent.getId()+ext;
127     String iconFilePath = configuration.getString("Producer.StorageRoot")
128                           +getIconStoragePath() + filepath;
129     String productionFilePath = getStoragePath() + File.separator + filepath;
130
131
132     if (ent.getValue("icon_data")!= null &&
133         ent.getValue("image_data")!= null) {
134       // make icon
135       try {
136         InputStream in = ((EntityImages)ent).getIcon();
137         FileUtil.write(iconFilePath, in);
138         in = ((EntityImages)ent).getImage();
139         FileUtil.write(productionFilePath, in);
140         ent.setValueForProperty("icon_path",getIconStoragePath()+filepath);
141         ent.setValueForProperty("publish_path",filepath);
142         ent.update();
143       }
144       catch (Throwable e) {
145         logger.error("MediaHandlerImages.produce: " + e.toString());
146         throw new MirMediaException("MediaHandlerImages.produce: " + e.toString());
147       }
148     }
149     else {
150       logger.error("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
151       throw new MirMediaException("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
152     }
153   }
154
155
156   public InputStream getIcon(Entity ent) throws MirMediaException
157   {
158     InputStream in;
159     try {
160       in = ((EntityImages)ent).getIcon();
161     }
162     catch (Throwable e) {
163       logger.error("MediaHandlerImages.getIcon: " + e.toString());
164       throw new MirMediaException(e.toString());
165     }
166
167     return in;
168   }
169
170   public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
171   {
172     SimpleList theList = new SimpleList();
173     theList.add(ent);
174     return theList;
175   }
176
177   public String getStoragePath()
178   {
179     return configuration.getString("Producer.Image.Path");
180   }
181
182   public String getIconStoragePath()
183   {
184     return configuration.getString("Producer.Image.IconPath");
185   }
186
187   public String getPublishHost()
188   {
189     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
190   }
191
192   public String getTinyIconName()
193   {
194     return configuration.getString("Producer.Icon.TinyImage");
195   }
196
197   public String getBigIconName()
198   {
199     return configuration.getString("Producer.Icon.BigImage");
200   }
201
202   public String getIconAltName()
203   {
204     return "Image";
205   }
206
207   public boolean isVideo()
208   {
209     return false;
210   }
211
212   public boolean isAudio()
213   {
214     return false;
215   }
216
217   public boolean isImage ()
218   {
219     return true;
220   }
221
222   public String getDescr(Entity mediaType)
223   {
224     return "image/jpeg";
225   }
226
227 }