Took the ChangeTracker reporting out of GeneratingProducerNode and put it into the...
[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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.media;
31
32 import java.io.ByteArrayInputStream;
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38 import java.sql.SQLException;
39
40 import mir.config.MirPropertiesConfiguration;
41 import mir.entity.Entity;
42 import mir.log.LoggerWrapper;
43 import mir.media.MediaExc;
44 import mir.media.MediaFailure;
45 import mir.media.MediaHandler;
46 import mir.misc.StringUtil;
47 import mir.session.SessionExc;
48 import mir.session.UploadedFile;
49 import mir.util.FileRoutines;
50 import mir.util.IORoutines;
51 import mircoders.entity.EntityImages;
52
53 /**
54  * This class handles saving, fetching creating representations
55  * for all images. The image content is stored in the DB. The content is
56  * written out to a file at the ProducerImages level.
57  * Remember that Handlers for specific image types, Gif, Jpeg, etc..
58  * should override it.
59  * It implements the MirMediaHandler interface.
60  * <p>
61  * slowly starting to look better, a next step would be to have the
62  * representation stuff (WebdbImage) happen here.
63  * -mh 01.03.2002
64  *
65  * @see mir.media.MediaHandler
66  * @author mh
67  * @version $Id: MediaHandlerImages.java,v 1.23.2.11 2006/11/12 18:44:46 yossarian Exp $
68  */
69
70
71 public abstract class MediaHandlerImages extends AbstractMediaHandler implements MediaHandler {
72   protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
73   protected static final String PNG = "PNG";
74   protected static final String JPEG = "JPEG";
75
76   protected LoggerWrapper logger;
77
78   abstract String getType();
79
80   public MediaHandlerImages() {
81     logger = new LoggerWrapper("Media.Images");
82   }
83
84   public InputStream getMedia(Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
85     try {
86       return new ByteArrayInputStream(((EntityImages) ent).getImage());
87     }
88     catch (Throwable e) {
89       logger.error("MediaHandlerImages.getImage: " + e.toString());
90
91       throw new MediaFailure(e);
92     }
93   }
94
95   public void store(UploadedFile anUploadedFile, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
96     try {
97       store(anUploadedFile.getInputStream(), aMedia, aMediaType);
98     }
99     catch (SessionExc e) {
100       throw new MediaFailure(e);
101     }
102   }
103
104   public void store(InputStream in, Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
105     try {
106       ((EntityImages) ent).setImage(in, getType());
107     }
108     catch (IOException e) {
109       logger.error("MediaHandlerImages.store", e);
110
111       throw new MediaFailure("A problem has occurred processing the media file", e);
112     }
113     catch (SQLException e) {
114       logger.error("MediaHandlerImages.store", e);
115
116       throw new MediaFailure("A problem has occurred processing the media file", e);
117     }
118   }
119
120   public void produce(Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
121     String date = ent.getFieldValue("date");
122     String datePath = StringUtil.webdbDate2path(date);
123     String ext = "."+mediaTypeEnt.getFieldValue("name");
124     String filepath = datePath+ent.getId()+ext;
125     String iconFilePath = configuration.getString("Producer.StorageRoot") + getBaseIconStoragePath() + filepath;
126     String productionFilePath = getBaseStoragePath() + File.separator + filepath;
127
128
129     if (ent.getFieldValue("icon_data")!= null &&
130         ent.getFieldValue("image_data")!= null) {
131       // make icon
132       try {
133         OutputStream out;
134         InputStream in = new ByteArrayInputStream(((EntityImages) ent).getIcon());
135         try {
136           File iconFile = new File(iconFilePath);
137
138           FileRoutines.createParentDirectories(iconFile);
139
140           out = new FileOutputStream(iconFile);
141           try {
142             IORoutines.copyStream(in, out);
143             reportChange(iconFile.getAbsolutePath());
144           }
145           finally {
146             out.close();
147           }
148         }
149         finally {
150           in.close();
151         }
152
153         in = new ByteArrayInputStream(((EntityImages) ent).getImage());
154         try {
155           File imageFile = new File(productionFilePath);
156
157           FileRoutines.createParentDirectories(imageFile);
158
159           out = new FileOutputStream(imageFile);
160           try {
161             IORoutines.copyStream(in, out);
162             reportChange(imageFile.getAbsolutePath());
163           }
164           finally {
165             out.close();
166           }
167         }
168         finally {
169           in.close();
170         }
171
172         ent.setFieldValue("icon_path", getBaseIconStoragePath() + filepath);
173         ent.setFieldValue("publish_path", filepath);
174         ent.update();
175       }
176       catch (Throwable e) {
177         logger.error("Error producing image", e);
178
179         throw new MediaFailure("Error producing image", e);
180       }
181       finally {
182       }
183     }
184     else {
185       logger.error("Can't produce image " + ent.getId() + ": missing image or icon OID");
186
187       throw new MediaExc("Can't produce image " + ent.getId() + ": missing image or icon OID");
188     }
189   }
190  
191   public InputStream getThumbnail(Entity ent) throws MediaExc, MediaFailure {
192     try {
193       return new ByteArrayInputStream(((EntityImages) ent).getIcon());
194     }
195     catch (Throwable e) {
196       logger.error("MediaHandlerImages.getIcon: " + e.toString());
197       throw new MediaFailure(e);
198     }
199   }
200
201   public String getBaseStoragePath() {
202     return configuration.getString("Producer.Image.Path");
203   }
204
205   public String getBaseIconStoragePath() {
206     return configuration.getString("Producer.Image.IconPath");
207   }
208
209   public String getPublishHost() {
210     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
211   }
212
213   public String getTinyIconName() {
214     return configuration.getString("Producer.Icon.TinyImage");
215   }
216
217   public String getBigIconName() {
218     return configuration.getString("Producer.Icon.BigImage");
219   }
220
221   public String getIconAltName() {
222     return "Image";
223   }
224
225   public String getDescr(Entity mediaType) {
226     return "image/jpeg";
227   }
228
229 }