2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
30 package mircoders.media;
32 import java.io.ByteArrayInputStream;
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;
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;
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..
59 * It implements the MirMediaHandler interface.
61 * slowly starting to look better, a next step would be to have the
62 * representation stuff (WebdbImage) happen here.
65 * @see mir.media.MediaHandler
67 * @version $Id: MediaHandlerImages.java,v 1.23.2.11 2006/11/12 18:44:46 yossarian Exp $
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";
76 protected LoggerWrapper logger;
78 abstract String getType();
80 public MediaHandlerImages() {
81 logger = new LoggerWrapper("Media.Images");
84 public InputStream getMedia(Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
86 return new ByteArrayInputStream(((EntityImages) ent).getImage());
89 logger.error("MediaHandlerImages.getImage: " + e.toString());
91 throw new MediaFailure(e);
95 public void store(UploadedFile anUploadedFile, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
97 store(anUploadedFile.getInputStream(), aMedia, aMediaType);
99 catch (SessionExc e) {
100 throw new MediaFailure(e);
104 public void store(InputStream in, Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
106 ((EntityImages) ent).setImage(in, getType());
108 catch (IOException e) {
109 logger.error("MediaHandlerImages.store", e);
111 throw new MediaFailure("A problem has occurred processing the media file", e);
113 catch (SQLException e) {
114 logger.error("MediaHandlerImages.store", e);
116 throw new MediaFailure("A problem has occurred processing the media file", e);
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;
129 if (ent.getFieldValue("icon_data")!= null &&
130 ent.getFieldValue("image_data")!= null) {
134 InputStream in = new ByteArrayInputStream(((EntityImages) ent).getIcon());
136 File iconFile = new File(iconFilePath);
138 FileRoutines.createParentDirectories(iconFile);
140 out = new FileOutputStream(iconFile);
142 IORoutines.copyStream(in, out);
143 reportChange(iconFile.getAbsolutePath());
153 in = new ByteArrayInputStream(((EntityImages) ent).getImage());
155 File imageFile = new File(productionFilePath);
157 FileRoutines.createParentDirectories(imageFile);
159 out = new FileOutputStream(imageFile);
161 IORoutines.copyStream(in, out);
162 reportChange(imageFile.getAbsolutePath());
172 ent.setFieldValue("icon_path", getBaseIconStoragePath() + filepath);
173 ent.setFieldValue("publish_path", filepath);
176 catch (Throwable e) {
177 logger.error("Error producing image", e);
179 throw new MediaFailure("Error producing image", e);
185 logger.error("Can't produce image " + ent.getId() + ": missing image or icon OID");
187 throw new MediaExc("Can't produce image " + ent.getId() + ": missing image or icon OID");
191 public InputStream getThumbnail(Entity ent) throws MediaExc, MediaFailure {
193 return new ByteArrayInputStream(((EntityImages) ent).getIcon());
195 catch (Throwable e) {
196 logger.error("MediaHandlerImages.getIcon: " + e.toString());
197 throw new MediaFailure(e);
201 public String getBaseStoragePath() {
202 return configuration.getString("Producer.Image.Path");
205 public String getBaseIconStoragePath() {
206 return configuration.getString("Producer.Image.IconPath");
209 public String getPublishHost() {
210 return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
213 public String getTinyIconName() {
214 return configuration.getString("Producer.Icon.TinyImage");
217 public String getBigIconName() {
218 return configuration.getString("Producer.Icon.BigImage");
221 public String getIconAltName() {
225 public String getDescr(Entity mediaType) {