Changes which allow images to be resized by Mir and saved in the filesystem if an...
[mir.git] / source / mircoders / media / MediaHandlerImagesExtern.java
index 6b50ef5..b498aa4 100755 (executable)
 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.ImageMagickImageProcessor;
 import mir.media.image.ImageProcessor;
+import mir.media.image.ImageMagickImageProcessor;
 import mir.misc.StringUtil;
 
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-
 /**
  * Image handler that stores images outside of the database.
  * 
@@ -51,73 +53,114 @@ import java.io.InputStream;
  * @version 1.0
  */
 
-public class MediaHandlerImagesExtern extends MediaHandlerGeneric {
+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 aMediaTypeEntity) throws MediaExc, MediaFailure {
+  public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
+    try {
       String date = anImageEntity.getFieldValue("date");
       String datePath = StringUtil.webdbDate2path(date);
-      String ext = "." + aMediaTypeEntity.getFieldValue("name");
+      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;
-        try {
-          processor = new ImageMagickImageProcessor(imageFile);
-        }
-        catch (IOException e) {
-          throw new MediaFailure(e);
-        }
+        ImageProcessor processor = new ImageMagickImageProcessor(imageFile);
 
         processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
         File dir = new File(iconFile.getParent());
-        if (dir!=null && !dir.exists()){
-          dir.mkdirs();
+          if (dir!=null && !dir.exists()){
+            dir.mkdirs();
         }
-        try {
-          processor.writeScaledData(iconFile, "JPEG");
+        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());
         }
-        catch (IOException e) {
-          throw new MediaFailure(e);
-        }
-
-        anImageEntity.setFieldValue("img_height",
-            Integer.toString(processor.getHeight()));
-        anImageEntity.setFieldValue("img_width",
-            Integer.toString(processor.getWidth()));
 
-        anImageEntity.setFieldValue("icon_height",
-            Integer.toString(processor.getScaledHeight()));
-        anImageEntity.setFieldValue("icon_width",
-            Integer.toString(processor.getScaledWidth()));
+        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);
+    }
   }