1f01ee57e7c83b39b0db7872f02c197e81ffa9eb
[mir.git] / source / mircoders / media / MediaHandlerImagesExtern.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 package mircoders.media;
33
34
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.InputStream;
38
39 import mir.config.MirPropertiesConfiguration;
40 import mir.entity.Entity;
41 import mir.log.LoggerWrapper;
42 import mir.media.MediaExc;
43 import mir.media.MediaFailure;
44 import mir.misc.StringUtil;
45 import mircoders.storage.DatabaseUploadedMedia;
46
47
48 /**
49  * Image handler that stores images outside of the database. Will be replaced by the new
50  *   media handling system.
51  * @author Zapata
52  * @version 1.0
53  */
54
55 public class MediaHandlerImagesExtern extends MediaHandlerGeneric
56 {
57   public MediaHandlerImagesExtern() {
58     logger = new LoggerWrapper("Media.Images.Extern");
59   }
60
61   public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
62     try {
63       String date = anImageEntity.getValue("date");
64       String datePath = StringUtil.webdbDate2path(date);
65       String ext = "." + mediaTypeEnt.getValue("name");
66       String filePath = datePath + anImageEntity.getId() + ext;
67       String iconFilePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") + getIconStoragePath() + filePath;
68       String imageFilePath = getStoragePath() + File.separator + filePath;
69       int maxIconSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxIconSize");
70
71       File imageFile = new File(imageFilePath);
72       File iconFile = new File(iconFilePath);
73
74       if (!imageFile.exists()) {
75         throw new MediaExc("error in MediaHandlerImagesExtern.produce(): " + filePath + " does not exist!");
76       }
77       else {
78         ImageProcessor processor = new ImageProcessor(imageFile, "JPEG");
79
80         processor.descaleImage(maxIconSize, 0.2F);
81         File dir = new File(iconFile.getParent());
82           if (dir!=null && !dir.exists()){
83             dir.mkdirs();
84         }
85         processor.writeScaledData(iconFile);
86
87         logger.info(processor.getWidth()+"x"+processor.getHeight());
88
89         anImageEntity.setValueForProperty("img_height", new Integer(processor.getHeight()).toString());
90         anImageEntity.setValueForProperty("img_width", new Integer(processor.getWidth()).toString());
91
92         anImageEntity.setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString());
93         anImageEntity.setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString());
94
95         anImageEntity.setValueForProperty("icon_path", getIconStoragePath()+filePath);
96         anImageEntity.setValueForProperty("publish_path", filePath);
97
98         anImageEntity.update();
99       }
100     }
101     catch(Throwable t) {
102       logger.error("MediaHandlerImagesExtern.produce: " + t.getMessage());
103       t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
104       throw new MediaFailure(t.getMessage(), t);
105     }
106   }
107
108
109   public InputStream getIcon(Entity anImageEntity) throws MediaExc, MediaFailure {
110     try {
111       Entity mediaType = DatabaseUploadedMedia.getInstance().getMediaType(
112           anImageEntity);
113
114       String date = anImageEntity.getValue("date");
115       String datePath = StringUtil.webdbDate2path(date);
116       String ext = "." + mediaType.getValue("name");
117       String filePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") +
118           getIconStoragePath() + datePath + anImageEntity.getId() + ext;
119
120       return new FileInputStream(new File(filePath));
121     }
122     catch (Throwable t) {
123       throw new MediaFailure(t);
124     }
125   }
126
127   public String getStoragePath()
128   {
129     return configuration.getString("Producer.Image.Path");
130   }
131
132   public String getIconStoragePath()
133   {
134     return configuration.getString("Producer.Image.IconPath");
135   }
136
137   public String getPublishHost()
138   {
139     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
140   }
141
142   public String getTinyIconName()
143   {
144     return configuration.getString("Producer.Icon.TinyImage");
145   }
146
147   public String getBigIconName()
148   {
149     return configuration.getString("Producer.Icon.BigImage");
150   }
151
152   public String getIconAltName()
153   {
154     return "Image";
155   }
156
157   public boolean isVideo()
158   {
159     return false;
160   }
161
162   public boolean isAudio()
163   {
164     return false;
165   }
166
167   public boolean isImage ()
168   {
169     return true;
170   }
171
172   public String getDescr(Entity mediaType)
173   {
174      return "image/jpeg";
175   }
176 }