various fixes/cleanup: old producers are now completely gone, old logfile class too
[mir.git] / source / mircoders / media / MediaHandlerImagesExtern.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mircoders.media;\r
33 \r
34 \r
35 import java.io.File;\r
36 import java.io.FileInputStream;\r
37 import java.io.InputStream;\r
38 \r
39 import mir.log.LoggerWrapper;\r
40 import mir.config.MirPropertiesConfiguration;\r
41 import mir.entity.Entity;\r
42 import mir.media.MirMediaException;\r
43 import mir.misc.StringUtil;\r
44 \r
45 import mircoders.storage.DatabaseUploadedMedia;\r
46 \r
47 \r
48 /**\r
49  * Image handler that stores images outside of the database. Will be replaced by the new\r
50  *   media handling system.\r
51  * @author Zapata\r
52  * @version 1.0\r
53  */\r
54 \r
55 public class MediaHandlerImagesExtern extends MediaHandlerGeneric\r
56 {\r
57   public MediaHandlerImagesExtern() {\r
58     logger = new LoggerWrapper("Media.Images.Extern");\r
59   }\r
60 \r
61   public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MirMediaException\r
62   {\r
63     try {\r
64       String date = anImageEntity.getValue("date");\r
65       String datePath = StringUtil.webdbDate2path(date);\r
66       String ext = "." + mediaTypeEnt.getValue("name");\r
67       String filePath = datePath + anImageEntity.getId() + ext;\r
68       String iconFilePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") + getIconStoragePath() + filePath;\r
69       String imageFilePath = getStoragePath() + File.separator + filePath;\r
70       int maxIconSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxIconSize");\r
71 \r
72       File imageFile = new File(imageFilePath);\r
73       File iconFile = new File(iconFilePath);\r
74 \r
75       if (!imageFile.exists()) {\r
76         throw new MirMediaException("error in MediaHandlerImagesExtern.produce(): " + filePath + " does not exist!");\r
77       }\r
78       else {\r
79         ImageProcessor processor = new ImageProcessor(imageFile, "JPEG");\r
80 \r
81         processor.descaleImage(maxIconSize, 0.2F);\r
82         File dir = new File(iconFile.getParent());\r
83           if (dir!=null && !dir.exists()){\r
84             dir.mkdirs();\r
85         }\r
86         processor.writeScaledData(iconFile);\r
87 \r
88         logger.info(processor.getWidth()+"x"+processor.getHeight());\r
89 \r
90         anImageEntity.setValueForProperty("img_height", new Integer(processor.getHeight()).toString());\r
91         anImageEntity.setValueForProperty("img_width", new Integer(processor.getWidth()).toString());\r
92 \r
93         anImageEntity.setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString());\r
94         anImageEntity.setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString());\r
95 \r
96         anImageEntity.setValueForProperty("icon_path", getIconStoragePath()+filePath);\r
97         anImageEntity.setValueForProperty("publish_path", filePath);\r
98 \r
99         anImageEntity.update();\r
100       }\r
101     }\r
102     catch(Throwable t) {\r
103       logger.error("MediaHandlerImagesExtern.produce: " + t.getMessage());\r
104       t.printStackTrace(logger.asPrintWriter(logger.DEBUG_MESSAGE));\r
105       throw new MirMediaException(t.getMessage());\r
106     }\r
107   }\r
108 \r
109 \r
110   public InputStream getIcon(Entity anImageEntity) throws MirMediaException\r
111   {\r
112     try {\r
113       Entity mediaType = DatabaseUploadedMedia.getInstance().getMediaType(\r
114           anImageEntity);\r
115 \r
116       String date = anImageEntity.getValue("date");\r
117       String datePath = StringUtil.webdbDate2path(date);\r
118       String ext = "." + mediaType.getValue("name");\r
119       String filePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") +\r
120           getIconStoragePath() + datePath + anImageEntity.getId() + ext;\r
121 \r
122       return new FileInputStream(new File(filePath));\r
123     }\r
124     catch (Throwable t) {\r
125       throw new MirMediaException(t.getMessage());\r
126     }\r
127   }\r
128 \r
129   public String getStoragePath()\r
130   {\r
131     return configuration.getString("Producer.Image.Path");\r
132   }\r
133 \r
134   public String getIconStoragePath()\r
135   {\r
136     return configuration.getString("Producer.Image.IconPath");\r
137   }\r
138 \r
139   public String getPublishHost()\r
140   {\r
141     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));\r
142   }\r
143 \r
144   public String getTinyIconName()\r
145   {\r
146     return configuration.getString("Producer.Icon.TinyImage");\r
147   }\r
148 \r
149   public String getBigIconName()\r
150   {\r
151     return configuration.getString("Producer.Icon.BigImage");\r
152   }\r
153 \r
154   public String getIconAltName()\r
155   {\r
156     return "Image";\r
157   }\r
158 \r
159   public boolean isVideo()\r
160   {\r
161     return false;\r
162   }\r
163 \r
164   public boolean isAudio()\r
165   {\r
166     return false;\r
167   }\r
168 \r
169   public boolean isImage ()\r
170   {\r
171     return true;\r
172   }\r
173 \r
174   public String getDescr(Entity mediaType)\r
175   {\r
176      return "image/jpeg";\r
177   }\r
178 }\r