Added some comments, but mostly translated them to English.
[mir.git] / source / mir / media / MediaHandlerGeneric.java
index 253d871..a466a98 100755 (executable)
@@ -13,21 +13,34 @@ import mir.storage.*;
 
 
 /**
- * Interfacedefinition für Datenbank-Adpatoren. Die Adaptoren legen
- * jeweils das Verhalten und die Befehlsmächtigkeit der Datenbank
- * fest.
+ * This is the Generic MediaHandler. It stores the media data on
+ * the filesystem and keeps basic metadata  (size, type...) in the
+ * DB. Usually only representation needs to be overridden.
+ * See the MediaHandlerAudio class to see an example of how one
+ * could override it.
+ * <p>
+ * Most media handlers should override this class.
+ * <p>
+ * In theory, it could be used to handle miscellaneous media that
+ * we don't have entered in the media_type table, (like RTF documents,
+ * PS, PDF, etc..)
+ * <p>
+ * Of course it implements the MirMedia interface.
  *
- * @author <mh>
+ * @see mir.media.MirMedia
+ * @author mh <heckmann@hbe.ca>
  * @version 24.09.2001
  */
 
 public class MediaHandlerGeneric implements MirMedia
 {
 
-    private String imageHost = MirConfig.getProp("Producer.Image.Host");
-    private static String imageRoot = MirConfig.getProp("Producer.ImageRoot");
-    private Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home")+"log/media.log");
-    public boolean set (byte[] uploadedData, Entity ent, Entity mediaTypeEnt ) {
+    protected String imageHost = MirConfig.getProp("Producer.Image.Host");
+    protected String imageRoot = MirConfig.getProp("Producer.ImageRoot");
+    protected Logfile theLog = Logfile.getInstance(MirConfig.getProp("Home")+"log/media.log");
+    public boolean set (byte[] uploadedData, Entity ent, Entity mediaTypeEnt )
+        throws MirMediaException {
+
         String ext = mediaTypeEnt.getValue("name");
         String dir = MirConfig.getProp("Producer.Media.Path");
         String mediaHost = MirConfig.getProp("Producer.Media.Host");
@@ -35,44 +48,42 @@ public class MediaHandlerGeneric implements MirMedia
         String date = ent.getValue("date");
         String datePath = StringUtil.webdbDate2path(date);
         Integer size = new Integer(uploadedData.length);
-        //hack: make it a config option to use "dated" dirs
-        //we can't cause of stallman -mh
-        //if(FileUtil.write(dir+"/"+datePath+"/"+mediaFname, uploadedData)) {
-        if(FileUtil.write(dir+"/"+mediaFname, uploadedData)) {
+        try {
+            FileUtil.write(dir+"/"+datePath+"/"+mediaFname, uploadedData);
+            //if(FileUtil.write(dir+"/"+mediaFname, uploadedData)) {
             //were done with the data, dereference.
             uploadedData=null;
             
-            try {
-                ent.setValueForProperty("is_produced", "1");
-                ent.setValueForProperty("icon_is_produced", "1");
-                //hack: make it a config option to use "dated" dirs
-                //we can't cause of stallman -mh
-                //ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
-                ent.setValueForProperty("publish_path", mediaFname);
-                ent.setValueForProperty("publish_server", mediaHost);
-                ent.setValueForProperty("size", size.toString());
-                ent.update();
-            } catch (StorageObjectException e) {
-                theLog.printError("StorageObjectException: "+e.toString()); 
-                return false;
-            }
-        } else {
-            theLog.printError("could not write FILE!"); 
-            return false;
+            ent.setValueForProperty("is_produced", "1");
+            ent.setValueForProperty("icon_is_produced", "1");
+            ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
+            //ent.setValueForProperty("publish_path", mediaFname);
+            ent.setValueForProperty("publish_server", mediaHost);
+            ent.setValueForProperty("size", size.toString());
+            ent.update();
+        } catch (Exception e) {
+            theLog.printError(e.toString()); 
+            throw new MirMediaException(e.toString());
         }
 
         return true;
     }
 
     //a class that will probably never get used..
-    private byte[] getFile (String fileName) {
+    private byte[] getFile (String fileName)
+        throws MirMediaException {
+
         long size = FileUtil.getSize(fileName);
         if (size < 0) return null;
 
         byte[] container = new byte[(int)size];
             
-        if(!FileUtil.read(fileName, container))
-            return null;
+        try {
+            FileUtil.read(fileName, container);
+        } catch (Exception e) {
+            theLog.printError(e.toString()); 
+            throw new MirMediaException(e.toString());
+        }
 
         return container;
     }
@@ -82,8 +93,7 @@ public class MediaHandlerGeneric implements MirMedia
     }
 
     public byte[] getIcon (Entity ent) {
-        String name = "/path/to/some/generic/icon";
-        return getFile(name);
+        return null;
     }
 
     public String getStoragePath()
@@ -118,9 +128,13 @@ public class MediaHandlerGeneric implements MirMedia
 
     public String getURL(Entity ent, Entity mediaTypeEnt)
     {
+      String stringSize = ent.getValue("size");
+      if (stringSize == null)
+        return null;
+      int size = Integer.parseInt(stringSize, 10)/1024;
       String title = ent.getValue("title")+
         " - "+mediaTypeEnt.getValue("name")+" "+
-        ent.getValue("size")+" Bytes";
+        size+" KB";
       return StringUtil.createURLLinks(ent.getValue("publish_server")+"/"+
         ent.getValue("publish_path"), title, imageRoot, getBigIcon());
     }