fixing scaling images -> nullpointer, patch by yossarian
[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  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
29  */\r
30 package mircoders.media;\r
31 \r
32 \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
40 \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
47 \r
48 /**\r
49  * Image handler that stores images outside of the database.\r
50  * \r
51  * @author Zapata\r
52  * @version 1.0\r
53  */\r
54 \r
55 public class MediaHandlerImagesExtern extends MediaHandlerGeneric {\r
56   private int maxIconSize;\r
57   private float minDescaleRatio;\r
58   private int minDescaleReduction;\r
59 \r
60   public MediaHandlerImagesExtern() {\r
61 \r
62     logger = new LoggerWrapper("Media.Images.Extern");\r
63 \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
67   }\r
68 \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
78 \r
79       File imageFile = new File(imageFilePath);\r
80       File iconFile = new File(iconStoragePath);\r
81 \r
82       if (!imageFile.exists()) {\r
83         throw new MediaExc("error in MediaHandlerImagesExtern.execute(): " + filePath + " does not exist!");\r
84       }\r
85       else {\r
86         ImageProcessor processor;\r
87         try {\r
88           processor = new ImageMagickImageProcessor(imageFile);\r
89         }\r
90         catch (IOException e) {\r
91           throw new MediaFailure(e);\r
92         }\r
93 \r
94         processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);\r
95         File dir = new File(iconFile.getParent());\r
96         if (dir!=null && !dir.exists()){\r
97           dir.mkdirs();\r
98         }\r
99                 try {\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
107                 }\r
108 \r
109 \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
114 \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
119 \r
120         processor.cleanup();\r
121         anImageEntity.setFieldValue("icon_path", iconPath);\r
122         anImageEntity.setFieldValue("publish_path", filePath);\r
123 \r
124         anImageEntity.update();\r
125         reportChange(iconStoragePath);\r
126         reportChange(imageFilePath);\r
127       }\r
128   }\r
129 \r
130 \r
131   /** {@inheritDoc} */\r
132   public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure {\r
133     try {\r
134       File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path"));\r
135 \r
136       if (!file.exists()) {\r
137         // hackish\r
138         file = new File(configuration.getHome(), "../img/photo_big.gif");\r
139       }\r
140 \r
141       return new BufferedInputStream(\r
142         new FileInputStream(file),8192);\r
143     }\r
144     catch (Throwable t) {\r
145       return null;\r
146     }\r
147   }\r
148 \r
149   public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {\r
150     return "image/jpeg";\r
151   }\r
152 \r
153   public String getBaseStoragePath()\r
154   {\r
155     return configuration.getString("Producer.Image.Path");\r
156   }\r
157 \r
158   public String getBaseIconStoragePath()\r
159   {\r
160     return configuration.getString("Producer.Image.IconPath");\r
161   }\r
162 \r
163   public String getPublishHost()\r
164   {\r
165     return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));\r
166   }\r
167 \r
168   public String getTinyIconName()\r
169   {\r
170     return configuration.getString("Producer.Icon.TinyImage");\r
171   }\r
172 \r
173   public String getBigIconName()\r
174   {\r
175     return configuration.getString("Producer.Icon.BigImage");\r
176   }\r
177 \r
178   public String getIconAltName()\r
179   {\r
180     return "Image";\r
181   }\r
182 \r
183   public String getDescr(Entity mediaType)\r
184   {\r
185      return "image/jpeg";\r
186   }\r
187 }\r