Changes which allow images to be resized by Mir and saved in the filesystem if an...
[mir.git] / source / mircoders / media / MediaHandlerImagesExtern.java
index 74da670..b498aa4 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002  The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\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
- */\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.media.MirMediaException;\r
-import mir.misc.StringUtil;\r
-import mircoders.storage.DatabaseUploadedMedia;\r
-\r
-\r
-/**\r
- * Image handler that stores images outside of the database. Will be replaced by the new\r
- *   media handling system.\r
- * @author Zapata\r
- * @version 1.0\r
- */\r
-\r
-public class MediaHandlerImagesExtern extends MediaHandlerGeneric\r
-{\r
-  public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MirMediaException\r
-  {\r
-    try {\r
-      String date = anImageEntity.getValue("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
-\r
-      File imageFile = new File(imageFilePath);\r
-      File iconFile = new File(iconFilePath);\r
-\r
-      if (!imageFile.exists()) {\r
-        throw new MirMediaException("error in MediaHandlerImagesExtern.produce(): " + filePath + " does not exist!");\r
-      }\r
-      else {\r
-        ImageProcessor processor = new ImageProcessor(imageFile, "JPEG");\r
-\r
-        processor.descaleImage(150, 0.8F);\r
-        File dir = new File(iconFile.getParent());\r
-          if (dir!=null && !dir.exists()){\r
-            dir.mkdirs();\r
-        }\r
-        processor.writeScaledData(iconFile);\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
-\r
-        anImageEntity.update();\r
-      }\r
-    }\r
-    catch(Throwable t) {\r
-      t.printStackTrace(System.out);\r
-      throw new MirMediaException(t.getMessage());\r
-    }\r
-  }\r
-\r
-\r
-  public InputStream getIcon(Entity anImageEntity) throws MirMediaException\r
-  {\r
-    try {\r
-      Entity mediaType = DatabaseUploadedMedia.getInstance().getMediaType(\r
-          anImageEntity);\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
-\r
-      return new FileInputStream(new File(filePath));\r
-    }\r
-    catch (Throwable t) {\r
-      throw new MirMediaException(t.getMessage());\r
-    }\r
-  }\r
-\r
-  public String getStoragePath()\r
-  {\r
-    return configuration.getString("Producer.Image.Path");\r
-  }\r
-\r
-  public String getIconStoragePath()\r
-  {\r
-    return configuration.getString("Producer.Image.IconPath");\r
-  }\r
-\r
-  public String getPublishHost()\r
-  {\r
-    return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));\r
-  }\r
-\r
-  public String getTinyIconName()\r
-  {\r
-    return configuration.getString("Producer.Icon.TinyImage");\r
-  }\r
-\r
-  public String getBigIconName()\r
-  {\r
-    return configuration.getString("Producer.Icon.BigImage");\r
-  }\r
-\r
-  public String getIconAltName()\r
-  {\r
-    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
-  }\r
-}\r
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+package mircoders.media;
+
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import mir.util.StreamCopier;
+import mir.entity.Entity;
+import mir.log.LoggerWrapper;
+import mir.media.MediaExc;
+import mir.media.MediaFailure;
+import mir.media.image.ImageProcessor;
+import mir.media.image.ImageMagickImageProcessor;
+import mir.misc.StringUtil;
+
+/**
+ * Image handler that stores images outside of the database.
+ * 
+ * @author Zapata
+ * @version 1.0
+ */
+
+public class MediaHandlerImagesExtern extends MediaHandlerGeneric
+{
+  private int maxSize;
+  private int maxIconSize;
+  private float minDescaleRatio;
+  private int minDescaleReduction;
+  private boolean scaleImages;
+  private String imagesOriginalDir;
+  private String imagesOriginalDirRelPath;
+  private String imageOriginalFilePath;
+  private String imageOriginalRelPath;
+  
+  public MediaHandlerImagesExtern() {
+    logger = new LoggerWrapper("Media.Images.Extern");
+    maxSize = configuration.getInt("Producer.Image.MaxSize");
+    maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
+    minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
+    minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
+    scaleImages = configuration.getBoolean("Producer.Image.ScaleImages");
+    imagesOriginalDir = configuration.getString("Producer.ImagesOriginalDir.Path"); 
+    imagesOriginalDirRelPath = configuration.getString("Producer.ImagesOriginalDir.RelPath");
+  }
+
+  public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
+    try {
+      String date = anImageEntity.getFieldValue("date");
+      String datePath = StringUtil.webdbDate2path(date);
+      String ext = "." + mediaTypeEnt.getFieldValue("name");
+      String fileBasePath = datePath + anImageEntity.getId();
+      String filePath = fileBasePath + ext;
+      String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg";
+      String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath;
+      String imageFilePath = getBaseStoragePath() + File.separator + filePath;
+      
+      // yoss: get a file path where the the original image should be saved if image resizing is turned on
+      imageOriginalFilePath = imagesOriginalDir + filePath;
+      imageOriginalRelPath = imagesOriginalDirRelPath +  filePath;
+      
+      // yoss:make a new File object for the originalFile
+      File originalFile = new File(imageOriginalFilePath);   
+      logger.info("imageOriginalFilePath:" + imageOriginalFilePath);
+      File imageFile = new File(imageFilePath);
+      logger.info("******************************************");
+      logger.info("imageFile exists: " + imageFile.exists());
+      logger.info("imageFile: " + imageFile.getAbsolutePath());
+      File iconFile = new File(iconStoragePath);
+      logger.info("iconStoragePath:"+ iconStoragePath);
+      
+      
+      if (!imageFile.exists()) {
+        throw new MediaExc("error in MediaHandlerImagesExtern.execute(): " + filePath + " does not exist!");
+      }
+      else {
+        ImageProcessor processor = new ImageMagickImageProcessor(imageFile);
+
+        processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
+        File dir = new File(iconFile.getParent());
+          if (dir!=null && !dir.exists()){
+            dir.mkdirs();
+        }
+        processor.writeScaledData(iconFile, "JPEG");
+        
+        // yoss: if the config is set so that images should be scaled, make the resized file.
+        if (scaleImages){ 
+               logger.info("entered scaleImages");
+            ImageProcessor originalProcessor = new ImageMagickImageProcessor(imageFile);
+            originalProcessor.descaleImage(maxSize, minDescaleRatio, minDescaleReduction);
+               File originalDir = new File(originalFile.getParent());
+               if(originalDir!=null && !originalDir.exists()) {
+                       originalDir.mkdirs();
+               }
+               if(!originalFile.exists()) {
+                       logger.debug("the original image file doesn't exist, copying to originalFile");
+                       StreamCopier.copyFile(imageFile, originalFile);
+               }
+               // yoss: don't write the scaled data again if it's the same size as 
+               // the file that's there right now.  Image producer runs are 4 times faster this way.
+               logger.info("about to write scaled data, byte length: " + originalProcessor.getScaledFileSize());
+               if (originalProcessor.getScaledFileSize() != imageFile.length()) {
+                       originalProcessor.writeScaledData(imageFile, "JPEG");
+               }
+               anImageEntity.setFieldValue("original_file_path", imageOriginalRelPath);
+            anImageEntity.setFieldValue("img_height", new Integer(originalProcessor.getScaledHeight()).toString());
+            anImageEntity.setFieldValue("img_width", new Integer(originalProcessor.getScaledWidth()).toString());
+               
+               originalProcessor.cleanup();
+               
+        } else {
+               anImageEntity.setFieldValue("img_height", new Integer(processor.getHeight()).toString());
+               anImageEntity.setFieldValue("img_width", new Integer(processor.getWidth()).toString());
+        }
+
+        anImageEntity.setFieldValue("icon_height", new Integer(processor.getScaledHeight()).toString());
+        anImageEntity.setFieldValue("icon_width", new Integer(processor.getScaledWidth()).toString());
+
+        processor.cleanup();
+
+        anImageEntity.setFieldValue("icon_path", iconPath);
+        anImageEntity.setFieldValue("publish_path", filePath);
+
+        anImageEntity.update();
+
+      }
+    }
+    catch(Throwable t) {
+      logger.error("MediaHandlerImagesExtern.execute: " + t.getMessage(), t);
+      throw new MediaFailure(t.getMessage(), t);
+    }
+  }
+
+
+  /** {@inheritDoc} */
+  public InputStream getThumbnail(Entity anImageEntity) throws MediaExc, MediaFailure {
+    try {
+      File file = new File(configuration.getString("Producer.StorageRoot") + anImageEntity.getFieldValue("icon_path"));
+
+      if (!file.exists()) {
+        // hackish
+        file = new File(configuration.getHome(), "../img/photo_big.gif");
+      }
+
+      return new BufferedInputStream(
+        new FileInputStream(file),8192);
+    }
+    catch (Throwable t) {
+      return null;
+    }
+  }
+
+  public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {
+    return "image/jpeg";
+  }
+
+  public String getBaseStoragePath()
+  {
+    return configuration.getString("Producer.Image.Path");
+  }
+
+  public String getBaseIconStoragePath()
+  {
+    return configuration.getString("Producer.Image.IconPath");
+  }
+
+  public String getPublishHost()
+  {
+    return StringUtil.removeSlash(configuration.getString("Producer.Image.Host"));
+  }
+
+  public String getTinyIconName()
+  {
+    return configuration.getString("Producer.Icon.TinyImage");
+  }
+
+  public String getBigIconName()
+  {
+    return configuration.getString("Producer.Icon.BigImage");
+  }
+
+  public String getIconAltName()
+  {
+    return "Image";
+  }
+
+  public String getDescr(Entity mediaType)
+  {
+     return "image/jpeg";
+  }
+}