*** empty log message ***
[mir.git] / source / mircoders / media / MediaHandlerImagesExtern.java
index 1f01ee5..4d15ba9 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002  The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * 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 the com.oreilly.servlet library, 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.
+ * 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;
 
 
@@ -43,6 +41,7 @@ import mir.media.MediaExc;
 import mir.media.MediaFailure;
 import mir.misc.StringUtil;
 import mircoders.storage.DatabaseUploadedMedia;
+import mircoders.module.*;
 
 
 /**
@@ -54,8 +53,25 @@ import mircoders.storage.DatabaseUploadedMedia;
 
 public class MediaHandlerImagesExtern extends MediaHandlerGeneric
 {
+  private int maxIconSize;
+  private float minDescaleRatio;
+  private int minDescaleReduction;
+
   public MediaHandlerImagesExtern() {
+
     logger = new LoggerWrapper("Media.Images.Extern");
+    try {
+      MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
+    }
+    catch (Throwable t) {
+      logger.fatal("MediaHandlerImagesExtern: can't get configuration");
+
+      throw new RuntimeException(t.toString());
+    }
+
+    maxIconSize = configuration.getInt("Producer.Image.MaxIconSize");
+    minDescaleRatio = configuration.getFloat("Producer.Image.MinDescalePercentage")/100;
+    minDescaleReduction = configuration.getInt("Producer.Image.MinDescaleReduction");
   }
 
   public void produce(Entity anImageEntity, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
@@ -63,28 +79,27 @@ public class MediaHandlerImagesExtern extends MediaHandlerGeneric
       String date = anImageEntity.getValue("date");
       String datePath = StringUtil.webdbDate2path(date);
       String ext = "." + mediaTypeEnt.getValue("name");
-      String filePath = datePath + anImageEntity.getId() + ext;
-      String iconFilePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") + getIconStoragePath() + filePath;
+      String fileBasePath = datePath + anImageEntity.getId();
+      String filePath = fileBasePath + ext;
+      String iconPath = getIconStoragePath() + fileBasePath + ".jpg";
+      String iconStoragePath = configuration.getString("Producer.StorageRoot") + iconPath;
       String imageFilePath = getStoragePath() + File.separator + filePath;
-      int maxIconSize = MirPropertiesConfiguration.instance().getInt("Producer.Image.MaxIconSize");
 
       File imageFile = new File(imageFilePath);
-      File iconFile = new File(iconFilePath);
+      File iconFile = new File(iconStoragePath);
 
       if (!imageFile.exists()) {
         throw new MediaExc("error in MediaHandlerImagesExtern.produce(): " + filePath + " does not exist!");
       }
       else {
-        ImageProcessor processor = new ImageProcessor(imageFile, "JPEG");
+        ImageProcessor processor = new ImageProcessor(imageFile);
 
-        processor.descaleImage(maxIconSize, 0.2F);
+        processor.descaleImage(maxIconSize, minDescaleRatio, minDescaleReduction);
         File dir = new File(iconFile.getParent());
           if (dir!=null && !dir.exists()){
             dir.mkdirs();
         }
-        processor.writeScaledData(iconFile);
-
-        logger.info(processor.getWidth()+"x"+processor.getHeight());
+        processor.writeScaledData(iconFile, "JPEG");
 
         anImageEntity.setValueForProperty("img_height", new Integer(processor.getHeight()).toString());
         anImageEntity.setValueForProperty("img_width", new Integer(processor.getWidth()).toString());
@@ -92,10 +107,12 @@ public class MediaHandlerImagesExtern extends MediaHandlerGeneric
         anImageEntity.setValueForProperty("icon_height", new Integer(processor.getScaledHeight()).toString());
         anImageEntity.setValueForProperty("icon_width", new Integer(processor.getScaledWidth()).toString());
 
-        anImageEntity.setValueForProperty("icon_path", getIconStoragePath()+filePath);
+        anImageEntity.setValueForProperty("icon_path", iconPath);
         anImageEntity.setValueForProperty("publish_path", filePath);
 
         anImageEntity.update();
+
+
       }
     }
     catch(Throwable t) {
@@ -108,22 +125,22 @@ public class MediaHandlerImagesExtern extends MediaHandlerGeneric
 
   public InputStream getIcon(Entity anImageEntity) throws MediaExc, MediaFailure {
     try {
-      Entity mediaType = DatabaseUploadedMedia.getInstance().getMediaType(
-          anImageEntity);
+      String filePath =
+          configuration.getString("Producer.StorageRoot") + anImageEntity.getValue("icon_path");
 
-      String date = anImageEntity.getValue("date");
-      String datePath = StringUtil.webdbDate2path(date);
-      String ext = "." + mediaType.getValue("name");
-      String filePath = MirPropertiesConfiguration.instance().getString("Producer.StorageRoot") +
-          getIconStoragePath() + datePath + anImageEntity.getId() + ext;
+      logger.info(filePath);
 
       return new FileInputStream(new File(filePath));
     }
     catch (Throwable t) {
-      throw new MediaFailure(t);
+      return null;
     }
   }
 
+  public String getIconMimeType(Entity anImageEntity, Entity aMediaType) {
+    return "image/jpeg";
+  }
+
   public String getStoragePath()
   {
     return configuration.getString("Producer.Image.Path");