2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with any library licensed under the Apache Software License,
\r
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
\r
23 * (or with modified versions of the above that use the same license as the above),
\r
24 * and distribute linked combinations including the two. You must obey the
\r
25 * GNU General Public License in all respects for all of the code used other than
\r
26 * the above mentioned libraries. If you modify this file, you may extend this
\r
27 * exception to your version of the file, but you are not obligated to do so.
\r
28 * If you do not wish to do so, delete this exception statement from your version.
\r
30 package mircoders.media;
\r
33 import mir.entity.Entity;
\r
34 import mir.log.LoggerWrapper;
\r
35 import mir.media.MediaExc;
\r
36 import mir.media.MediaFailure;
\r
37 import mir.media.image.ImageMagickImageProcessor;
\r
38 import mir.media.image.ImageProcessor;
\r
39 import mir.misc.StringUtil;
\r
41 import java.io.BufferedInputStream;
\r
42 import java.io.File;
\r
43 import java.io.FileInputStream;
\r
44 import java.io.FileNotFoundException;
\r
45 import java.io.IOException;
\r
46 import java.io.InputStream;
\r
49 * Image handler that stores images outside of the database.
\r
55 public class MediaHandlerImagesExtern extends MediaHandlerGeneric {
\r
56 private int maxIconSize;
\r
57 private float minDescaleRatio;
\r
58 private int minDescaleReduction;
\r
60 public MediaHandlerImagesExtern() {
\r
62 logger = new LoggerWrapper("Media.Images.Extern");
\r
64 maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
\r
65 minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
\r
66 minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
\r
69 public void produce(Entity anImageEntity, Entity aMediaTypeEntity) throws MediaExc, MediaFailure {
\r
70 String date = anImageEntity.getFieldValue("date");
\r
71 String datePath = StringUtil.webdbDate2path(date);
\r
72 String ext = "." + aMediaTypeEntity.getFieldValue("name");
\r
73 String fileBasePath = datePath + anImageEntity.getId();
\r
74 String filePath = fileBasePath + ext;
\r
75 String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg";
\r
76 String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath;
\r
77 String imageFilePath = getBaseStoragePath() + File.separator + filePath;
\r
79 File imageFile = new File(imageFilePath);
\r
80 File iconFile = new File(iconStoragePath);
\r
82 if (!imageFile.exists()) {
\r
83 throw new MediaExc("error in MediaHandlerImagesExtern.execute(): " + filePath + " does not exist!");
\r
86 ImageProcessor processor;
\r
88 processor = new ImageMagickImageProcessor(imageFile);
\r
90 catch (IOException e) {
\r
91 throw new MediaFailure(e);
\r
94 processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
\r
95 File dir = new File(iconFile.getParent());
\r
96 if (dir!=null && !dir.exists()){
\r
100 processor.writeScaledData(iconFile, "JPEG");
\r
101 } catch (FileNotFoundException e) {
\r
102 // TODO Auto-generated catch block
\r
103 e.printStackTrace();
\r
104 } catch (IOException e) {
\r
105 // TODO Auto-generated catch block
\r
106 e.printStackTrace();
\r
110 anImageEntity.setFieldValue("img_height",
\r
111 Integer.toString(processor.getHeight()));
\r
112 anImageEntity.setFieldValue("img_width",
\r
113 Integer.toString(processor.getWidth()));
\r
115 anImageEntity.setFieldValue("icon_height",
\r
116 Integer.toString(processor.getScaledHeight()));
\r
117 anImageEntity.setFieldValue("icon_width",
\r
118 Integer.toString(processor.getScaledWidth()));
\r
120 processor.cleanup();
\r
121 anImageEntity.setFieldValue("icon_path", iconPath);
\r
122 anImageEntity.setFieldValue("publish_path", filePath);
\r
124 anImageEntity.update();
\r
125 reportChange(iconStoragePath);
\r
126 reportChange(imageFilePath);
\r
131 /** {@inheritDoc} */
\r
132 public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure {
\r
134 File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path"));
\r
136 if (!file.exists()) {
\r
138 file = new File(configuration.getHome(), "../img/photo_big.gif");
\r
141 return new BufferedInputStream(
\r
142 new FileInputStream(file),8192);
\r
144 catch (Throwable t) {
\r
149 public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {
\r
150 return "image/jpeg";
\r
153 public String getBaseStoragePath()
\r
155 return configuration.getString("Producer.Image.Path");
\r
158 public String getBaseIconStoragePath()
\r
160 return configuration.getString("Producer.Image.IconPath");
\r
163 public String getPublishHost()
\r
165 return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
\r
168 public String getTinyIconName()
\r
170 return configuration.getString("Producer.Icon.TinyImage");
\r
173 public String getBigIconName()
\r
175 return configuration.getString("Producer.Icon.BigImage");
\r
178 public String getIconAltName()
\r
183 public String getDescr(Entity mediaType)
\r
185 return "image/jpeg";
\r