organizing imports
[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.log.LoggerWrapper;
42 import mir.media.MirMedia;
43 import mir.media.MirMediaException;
44 import mir.misc.FileUtil;
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.19 2003/03/05 19:23:16 idfx Exp $
65  */
66
67
68 public abstract class MediaHandlerImages implements MirMedia
69 {
70   protected static MirPropertiesConfiguration configuration;
71   protected static final String PNG = "PNG";
72   protected static final String JPEG = "JPEG";
73
74   protected LoggerWrapper logger;
75
76   static {
77     try {
78       configuration = MirPropertiesConfiguration.instance();
79     }
80     catch (PropertiesConfigExc e) {
81       throw new RuntimeException("Can't get configuration: " + e.getMessage());
82     }
83   }
84
85   abstract String getType();
86
87   public MediaHandlerImages() {
88     logger = new LoggerWrapper("Media.Images");
89   }
90
91   public InputStream getMedia(Entity ent, Entity mediaTypeEnt)
92     throws MirMediaException
93         {
94     InputStream in;
95     try {
96       in = ((EntityImages)ent).getImage();
97     }
98     catch ( StorageObjectFailure e) {
99       logger.error("MediaHandlerImages.getImage: "+e.toString());
100       throw new MirMediaException(e.toString());
101     }
102
103     return in;
104   }
105
106   public void set(InputStream in, Entity ent, Entity mediaTypeEnt)
107     throws MirMediaException {
108
109     try {
110       ((EntityImages)ent).setImage(in, getType());
111     }
112     catch ( StorageObjectFailure e) {
113       logger.error("MediaHandlerImages.set: "+e.getMessage());
114       e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
115       throw new MirMediaException(e.getMessage());
116     }
117   }
118
119   public void produce(Entity ent, Entity mediaTypeEnt) throws MirMediaException
120   {
121     String date = ent.getValue("date");
122     String datePath = StringUtil.webdbDate2path(date);
123     String ext = "."+mediaTypeEnt.getValue("name");
124     String filepath = datePath+ent.getId()+ext;
125     String iconFilePath = configuration.getString("Producer.StorageRoot")
126                           +getIconStoragePath() + filepath;
127     String productionFilePath = getStoragePath() + File.separator + filepath;
128
129
130     if (ent.getValue("icon_data")!= null &&
131         ent.getValue("image_data")!= null) {
132       // make icon
133       try {
134         InputStream in = ((EntityImages)ent).getIcon();
135         FileUtil.write(iconFilePath, in);
136         in = ((EntityImages)ent).getImage();
137         FileUtil.write(productionFilePath, in);
138         ent.setValueForProperty("icon_path",getIconStoragePath()+filepath);
139         ent.setValueForProperty("publish_path",filepath);
140         ent.update();
141       }
142       catch (Throwable e) {
143         logger.error("MediaHandlerImages.produce: " + e.toString());
144         throw new MirMediaException("MediaHandlerImages.produce: " + e.toString());
145       }
146     }
147     else {
148       logger.error("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
149       throw new MirMediaException("MediaHandlerImages.produce: missing image or icon OID for: " + ent.getId());
150     }
151   }
152
153
154   public InputStream getIcon(Entity ent) throws MirMediaException
155   {
156     InputStream in;
157     try {
158       in = ((EntityImages)ent).getIcon();
159     }
160     catch (Throwable e) {
161       logger.error("MediaHandlerImages.getIcon: " + e.toString());
162       throw new MirMediaException(e.toString());
163     }
164
165     return in;
166   }
167
168   public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
169   {
170     SimpleList theList = new SimpleList();
171     theList.add(ent);
172     return theList;
173   }
174
175   public String getStoragePath()
176   {
177     return configuration.getString("Producer.Image.Path");
178   }
179
180   public String getIconStoragePath()
181   {
182     return configuration.getString("Producer.Image.IconPath");
183   }
184
185   public String getPublishHost()
186   {
187     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
188   }
189
190   public String getTinyIconName()
191   {
192     return configuration.getString("Producer.Icon.TinyImage");
193   }
194
195   public String getBigIconName()
196   {
197     return configuration.getString("Producer.Icon.BigImage");
198   }
199
200   public String getIconAltName()
201   {
202     return "Image";
203   }
204
205   public boolean isVideo()
206   {
207     return false;
208   }
209
210   public boolean isAudio()
211   {
212     return false;
213   }
214
215   public boolean isImage ()
216   {
217     return true;
218   }
219
220   public String getDescr(Entity mediaType)
221   {
222     return "image/jpeg";
223   }
224
225 }