Refactored the class with a whole lot of extract method to make it easier to understand.
authoryossarian <yossarian>
Mon, 13 Nov 2006 22:44:35 +0000 (22:44 +0000)
committeryossarian <yossarian>
Mon, 13 Nov 2006 22:44:35 +0000 (22:44 +0000)
source/mircoders/media/MediaHandlerImagesExtern.java

index 788d4fa..4b52f1f 100755 (executable)
@@ -33,6 +33,7 @@ package mircoders.media;
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 
 import mir.entity.Entity;
@@ -69,110 +70,101 @@ public class MediaHandlerImagesExtern extends MediaHandlerGeneric
   }
 
   public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
-       String imagesOriginalDir = configuration.getString("Producer.ImagesOriginalDir.Path"); ;
-       String imagesOriginalDirRelPath = configuration.getString("Producer.ImagesOriginalDir.RelPath");;
-       String imageOriginalFilePath;
-       String imageOriginalRelPath;
     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);
+        String iconPath = getBaseIconStoragePath() + fileBasePath + ".jpg";
+        String iconStoragePath = doIconScaling(processor, iconPath);
+        anImageEntity.setFieldValue("icon_height", new Integer(processor.getScaledHeight()).toString());
+        anImageEntity.setFieldValue("icon_width", new Integer(processor.getScaledWidth()).toString());
+        anImageEntity.setFieldValue("icon_path", iconPath);
+        
+        if (scaleImages) {
+          String imageOriginalRelPath = doImageScaling(filePath, imageFile, processor);
+          anImageEntity.setFieldValue("original_file_path", imageOriginalRelPath);
+          anImageEntity.setFieldValue("img_height", Integer.toString(processor.getScaledHeight()));
+          anImageEntity.setFieldValue("img_width", Integer.toString(processor.getScaledWidth()));
+        } else {
+          anImageEntity.setFieldValue("img_height", new Integer(processor.getHeight()).toString());
+          anImageEntity.setFieldValue("img_width", new Integer(processor.getWidth()).toString());
+        }
+        processor.cleanup();
+        anImageEntity.setFieldValue("publish_path", filePath);
+        anImageEntity.update();
+        reportChange(iconStoragePath);
+        reportChange(imageFilePath);
+      }
+    }
+    catch(Throwable t) {
+      logger.error("MediaHandlerImagesExtern.execute: " + t.getMessage(), t);
+      throw new MediaFailure(t.getMessage(), t);
+    }
+  }
+
+    /**
+     * Scale an icon image and write it to the file system.
+     * @param processor
+     * @param iconPath
+     * @return
+     * @throws MediaExc
+     */
+    private String doIconScaling(ImageProcessor processor, String iconPath) throws MediaExc {
+        String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath;
+        File iconFile = new File(iconStoragePath);
+        processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
         File dir = new File(iconFile.getParent());
         if (dir != null && !dir.exists()) {
           dir.mkdirs();
         }
         processor.writeScaledData(iconFile, "JPEG");
-        reportChange(iconStoragePath);
-        reportChange(imageFilePath);
+        return iconStoragePath;
+    }
 
-        // 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);
+    /**
+     * Make the resized file.
+     * @param filePath
+     * @param imageFile
+     * @param processor
+     * @return
+     * @throws MediaExc
+     * @throws IOException
+     */
+    private String doImageScaling(String filePath, File imageFile, ImageProcessor processor) throws MediaExc, IOException {
+        // get a file path where the the original image should be saved if image resizing is turned on
+          String imagesOriginalDir = configuration.getString("Producer.ImagesOriginalDir.Path");
+          String imagesOriginalDirRelPath = configuration.getString("Producer.ImagesOriginalDir.RelPath");
+          String imageOriginalFilePath = imagesOriginalDir + filePath;
+          String imageOriginalRelPath = imagesOriginalDirRelPath +  filePath;
+          File originalFile = new File(imageOriginalFilePath);   
+          processor.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");
             FileRoutines.copy(imageFile, originalFile);
             reportChange(originalFile.getAbsolutePath());
           }
           // 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");
+          if (processor.getScaledFileSize() != imageFile.length()) {
+              processor.writeScaledData(imageFile, "JPEG");
           }
-          anImageEntity.setFieldValue("original_file_path",
-              imageOriginalRelPath);
-          anImageEntity.setFieldValue("img_height", Integer
-              .toString(originalProcessor.getScaledHeight()));
-          anImageEntity.setFieldValue("img_width", Integer
-              .toString(originalProcessor.getScaledWidth()));
-
-          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();
-
-      }
+        return imageOriginalRelPath;
     }
-    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 {