fixing scaling images -> nullpointer, patch by yossarian
[mir.git] / source / mircoders / media / MediaHandlerImagesExtern.java
index a885d2f..61f6017 100755 (executable)
@@ -1,5 +1,5 @@
 /*\r
- * Copyright (C) 2001, 2002  The Mir-coders group\r
+ * Copyright (C) 2001, 2002 The Mir-coders group\r
  *\r
  * This file is part of Mir.\r
  *\r
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
  *\r
  * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with the com.oreilly.servlet library, any library\r
- * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
- * the above that use the same license as the above), and distribute linked\r
- * combinations including the two.  You must obey the GNU General Public\r
- * License in all respects for all of the code used other than the above\r
- * mentioned libraries.  If you modify this file, you may extend this exception\r
- * to your version of the file, but you are not obligated to do so.  If you do\r
- * not wish to do so, delete this exception statement from your version.\r
+ * the code of this program with  any library licensed under the Apache Software License,\r
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
+ * (or with modified versions of the above that use the same license as the above),\r
+ * and distribute linked combinations including the two.  You must obey the\r
+ * GNU General Public License in all respects for all of the code used other than\r
+ * the above mentioned libraries.  If you modify this file, you may extend this\r
+ * exception to your version of the file, but you are not obligated to do so.\r
+ * If you do not wish to do so, delete this exception statement from your version.\r
  */\r
-\r
 package mircoders.media;\r
 \r
 \r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.InputStream;\r
-\r
-import mir.config.MirPropertiesConfiguration;\r
 import mir.entity.Entity;\r
 import mir.log.LoggerWrapper;\r
 import mir.media.MediaExc;\r
 import mir.media.MediaFailure;\r
+import mir.media.image.ImageMagickImageProcessor;\r
+import mir.media.image.ImageProcessor;\r
 import mir.misc.StringUtil;\r
-import mircoders.storage.DatabaseUploadedMedia;\r
 \r
+import java.io.BufferedInputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
 \r
 /**\r
- * Image handler that stores images outside of the database. Will be replaced by the new\r
- *   media handling system.\r
+ * Image handler that stores images outside of the database.\r
+ * \r
  * @author Zapata\r
  * @version 1.0\r
  */\r
 \r
-public class MediaHandlerImagesExtern extends MediaHandlerGeneric\r
-{\r
+public class MediaHandlerImagesExtern extends MediaHandlerGeneric {\r
+  private int maxIconSize;\r
+  private float minDescaleRatio;\r
+  private int minDescaleReduction;\r
+\r
   public MediaHandlerImagesExtern() {\r
+\r
     logger = new LoggerWrapper("Media.Images.Extern");\r
+\r
+    maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");\r
+    minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;\r
+    minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");\r
   }\r
 \r
-  public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {\r
-    try {\r
-      String date = anImageEntity.getValue("date");\r
+  public void produce(Entity anImageEntity, Entity aMediaTypeEntity) throws MediaExc, MediaFailure {\r
+      String date = anImageEntity.getFieldValue("date");\r
       String datePath = StringUtil.webdbDate2path(date);\r
-      String ext = "." + mediaTypeEnt.getValue("name");\r
-      String filePath = datePath + anImageEntity.getId() + ext;\r
-      String iconFilePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") + getIconStoragePath() + filePath;\r
-      String imageFilePath = getStoragePath() + File.separator + filePath;\r
-      int maxIconSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxIconSize");\r
+      String ext = "." + aMediaTypeEntity.getFieldValue("name");\r
+      String fileBasePath = datePath + anImageEntity.getId();\r
+      String filePath = fileBasePath + ext;\r
+      String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg";\r
+      String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath;\r
+      String imageFilePath = getBaseStoragePath() + File.separator + filePath;\r
 \r
       File imageFile = new File(imageFilePath);\r
-      File iconFile = new File(iconFilePath);\r
+      File iconFile = new File(iconStoragePath);\r
 \r
       if (!imageFile.exists()) {\r
-        throw new MediaExc("error in MediaHandlerImagesExtern.produce(): " + filePath + " does not exist!");\r
+        throw new MediaExc("error in MediaHandlerImagesExtern.execute(): " + filePath + " does not exist!");\r
       }\r
       else {\r
-        ImageProcessor processor = new ImageProcessor(imageFile, "JPEG");\r
+        ImageProcessor processor;\r
+        try {\r
+          processor = new ImageMagickImageProcessor(imageFile);\r
+        }\r
+        catch (IOException e) {\r
+          throw new MediaFailure(e);\r
+        }\r
 \r
-        processor.descaleImage(maxIconSize, 0.2F);\r
+        processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);\r
         File dir = new File(iconFile.getParent());\r
-          if (dir!=null && !dir.exists()){\r
-            dir.mkdirs();\r
+        if (dir!=null && !dir.exists()){\r
+          dir.mkdirs();\r
         }\r
-        processor.writeScaledData(iconFile);\r
-\r
-        logger.info(processor.getWidth()+"x"+processor.getHeight());\r
-\r
-        anImageEntity.setValueForProperty("img_height", new Integer(processor.getHeight()).toString());\r
-        anImageEntity.setValueForProperty("img_width", new Integer(processor.getWidth()).toString());\r
-\r
-        anImageEntity.setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString());\r
-        anImageEntity.setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString());\r
-\r
-        anImageEntity.setValueForProperty("icon_path", getIconStoragePath()+filePath);\r
-        anImageEntity.setValueForProperty("publish_path", filePath);\r
+               try {\r
+                       processor.writeScaledData(iconFile, "JPEG");\r
+               } catch (FileNotFoundException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               } catch (IOException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+               }\r
+\r
+\r
+        anImageEntity.setFieldValue("img_height",\r
+            Integer.toString(processor.getHeight()));\r
+        anImageEntity.setFieldValue("img_width",\r
+            Integer.toString(processor.getWidth()));\r
+\r
+        anImageEntity.setFieldValue("icon_height",\r
+            Integer.toString(processor.getScaledHeight()));\r
+        anImageEntity.setFieldValue("icon_width",\r
+            Integer.toString(processor.getScaledWidth()));\r
+\r
+        processor.cleanup();\r
+        anImageEntity.setFieldValue("icon_path", iconPath);\r
+        anImageEntity.setFieldValue("publish_path", filePath);\r
 \r
         anImageEntity.update();\r
+        reportChange(iconStoragePath);\r
+        reportChange(imageFilePath);\r
       }\r
-    }\r
-    catch(Throwable t) {\r
-      logger.error("MediaHandlerImagesExtern.produce: " + t.getMessage());\r
-      t.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));\r
-      throw new MediaFailure(t.getMessage(), t);\r
-    }\r
   }\r
 \r
 \r
-  public InputStream getIcon(Entity anImageEntity) throws MediaExc, MediaFailure {\r
+  /** {@inheritDoc} */\r
+  public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure {\r
     try {\r
-      Entity mediaType = DatabaseUploadedMedia.getInstance().getMediaType(\r
-          anImageEntity);\r
+      File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path"));\r
 \r
-      String date = anImageEntity.getValue("date");\r
-      String datePath = StringUtil.webdbDate2path(date);\r
-      String ext = "." + mediaType.getValue("name");\r
-      String filePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") +\r
-          getIconStoragePath() + datePath + anImageEntity.getId() + ext;\r
+      if (!file.exists()) {\r
+        // hackish\r
+        file = new File(configuration.getHome(), "../img/photo_big.gif");\r
+      }\r
 \r
-      return new FileInputStream(new File(filePath));\r
+      return new BufferedInputStream(\r
+        new FileInputStream(file),8192);\r
     }\r
     catch (Throwable t) {\r
-      throw new MediaFailure(t);\r
+      return null;\r
     }\r
   }\r
 \r
-  public String getStoragePath()\r
+  public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {\r
+    return "image/jpeg";\r
+  }\r
+\r
+  public String getBaseStoragePath()\r
   {\r
     return configuration.getString("Producer.Image.Path");\r
   }\r
 \r
-  public String getIconStoragePath()\r
+  public String getBaseIconStoragePath()\r
   {\r
     return configuration.getString("Producer.Image.IconPath");\r
   }\r
@@ -154,21 +180,6 @@ public class MediaHandlerImagesExtern extends MediaHandlerGeneric
     return "Image";\r
   }\r
 \r
-  public boolean isVideo()\r
-  {\r
-    return false;\r
-  }\r
-\r
-  public boolean isAudio()\r
-  {\r
-    return false;\r
-  }\r
-\r
-  public boolean isImage ()\r
-  {\r
-    return true;\r
-  }\r
-\r
   public String getDescr(Entity mediaType)\r
   {\r
      return "image/jpeg";\r