organizing imports
[mir.git] / source / mircoders / media / MediaHandlerGeneric.java
index 31eaff9..6518e61 100755 (executable)
-/*
- * put your module comment here
- */
-
-
-package  mircoders.media;
-
-import java.util.*;
-import java.io.*;
-
-import mir.media.*;
-import mir.entity.*;
-import mir.misc.*;
-import mir.storage.*;
-
-
-/**
- * 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.
- *
- * @see mir.media.MirMedia
- * @author mh <heckmann@hbe.ca>
- * @version 24.09.2001
- */
-
-public class MediaHandlerGeneric implements MirMedia
-{
-    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 mediaFname = ent.getId()+"."+ext;
-        String date = ent.getValue("date");
-        String datePath = StringUtil.webdbDate2path(date);
-        Integer size = new Integer(uploadedData.length);
-        try {
-            FileUtil.write(getStoragePath()+"/"+datePath+"/"+mediaFname,
-                            uploadedData);
-            //were done with the data, dereference.
-            uploadedData=null;
-            
-            ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
-            ent.setValueForProperty("size", size.toString());
-            ent.update();
-        } catch (Exception e) {
-            theLog.printError(e.toString()); 
-            throw new MirMediaException(e.toString());
-        }
-
-        return true;
-    }
-
-    public void produce (Entity ent, Entity mediaTypeEnt )
-      throws MirMediaException {
-      
-      //check first if the media file exist since produced
-      //location is also the storage location
-      String date = ent.getValue("date");
-      String datePath = StringUtil.webdbDate2path(date);
-      String relPath = datePath+ent.getId()+"."+mediaTypeEnt.getValue("name");
-      String fname = getStoragePath()+relPath;
-      if(! new File(fname).exists())
-        throw new MirMediaException("error in MirMedia.produce(): "+relPath+
-                                    "does not exist!");
-    }
-      
-
-    //a method that will probably never get used..
-    private byte[] getFile (String fileName)
-        throws MirMediaException {
-
-        long size = FileUtil.getSize(fileName);
-        if (size < 0) return null;
-
-        byte[] container = new byte[(int)size];
-            
-        try {
-            FileUtil.read(fileName, container);
-        } catch (Exception e) {
-            theLog.printError(e.toString()); 
-            throw new MirMediaException(e.toString());
-        }
-
-        return container;
-    }
-
-    public byte[] get (Entity ent, Entity mediaTypeEnt) {
-        return null;
-    }
-
-    public byte[] getIcon (Entity ent) {
-        return null;
-    }
-
-    public String getStoragePath()
-    {
-        return MirConfig.getProp("Producer.Media.Path");
-    }
-
-    public String getIconStoragePath()
-    {
-        return MirConfig.getProp("Producer.Image.IconPath");
-    }
-
-    public String getPublishHost()
-    {
-        return MirConfig.getProp("Producer.Media.Host");
-    }
-
-    public String getTinyIcon()
-    {
-        return MirConfig.getProp("Producer.Icon.TinyText");
-    }
-
-    public String getBigIcon()
-    {
-        return MirConfig.getProp("Producer.Icon.BigText");
-    }
-
-    public String getIconAlt()
-    {
-        return "Generic media"; 
-    }
-
-    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")+" "+
-        size+" KB";
-      return StringUtil.createURLLinks(ent.getValue("publish_server")+"/"+
-        ent.getValue("publish_path"), title, imageRoot, getBigIcon());
-    }
-
-    public String getListView(Entity ent, Entity mediaTypeEnt)
-    {
-      //String title = ent.getValue("title")+
-      //  " - "+mediaTypeEnt.getValue("name")+" "+
-      //  ent.getValue("size")+" Bytes";
-      return StringUtil.createIMGLinks(imageHost+
-        getBigIcon(), null, null, null);
-    }
-
-    public boolean isVideo()
-    {
-        return false;
-    }
-
-    public boolean isAudio()
-    {
-        return false;
-    }
-
-    public boolean isImage()
-    {
-        return false;
-    }
-
-}
-        
-        
-
+/*\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
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+import mir.config.MirPropertiesConfiguration;\r
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
+import mir.entity.Entity;\r
+import mir.log.LoggerWrapper;\r
+import mir.media.MirMedia;\r
+import mir.media.MirMediaException;\r
+import mir.misc.FileUtil;\r
+import mir.misc.StringUtil;\r
+import freemarker.template.SimpleList;\r
+\r
+\r
+/**\r
+ * This is the Generic MediaHandler. It stores the media data on\r
+ * the filesystem and keeps basic metadata  (size, type...) in the\r
+ * DB. Usually only representation needs to be overridden.\r
+ * See the MediaHandlerAudio class to see an example of how one\r
+ * could override it.\r
+ * <p>\r
+ * Most media handlers should override this class.\r
+ * <p>\r
+ * In theory, it could be used to handle miscellaneous media that\r
+ * we don't have entered in the media_type table, (like RTF documents,\r
+ * PS, PDF, etc..)\r
+ * <p>\r
+ * Of course it implements the MirMedia interface.\r
+ *\r
+ * @see mir.media.MirMedia\r
+ * @author mh <mh@nadir.org>\r
+ * @version $Id: MediaHandlerGeneric.java,v 1.15 2003/03/05 19:23:16 idfx Exp $\r
+ */\r
+\r
+public class MediaHandlerGeneric implements MirMedia\r
+{\r
+    protected static MirPropertiesConfiguration configuration;\r
+    protected static String imageHost;\r
+    protected static String imageRoot;\r
+\r
+    protected LoggerWrapper logger;\r
+\r
+    static {\r
+      try {\r
+        configuration = MirPropertiesConfiguration.instance();\r
+      } catch (PropertiesConfigExc e) {\r
+        e.printStackTrace();\r
+      }\r
+      imageHost = configuration.getString("Producer.Image.Host");\r
+      imageRoot = configuration.getString("Producer.ImageRoot");\r
+    }\r
+\r
+    public MediaHandlerGeneric() {\r
+      logger = new LoggerWrapper("Media.Generic");\r
+    }\r
+\r
+    public void set (InputStream in, Entity ent, Entity mediaTypeEnt ) throws MirMediaException {\r
+      String ext = mediaTypeEnt.getValue("name");\r
+      String mediaFname = ent.getId() + "." + ext;\r
+      String date = ent.getValue("date");\r
+      String datePath = StringUtil.webdbDate2path(date);\r
+      try {\r
+        long size = FileUtil.write(getStoragePath() + File.separator + datePath +\r
+                                   File.separator + mediaFname, in);\r
+        ent.setValueForProperty("publish_path", datePath + mediaFname);\r
+        ent.setValueForProperty("size", new Long(size).toString());\r
+        ent.update();\r
+      }\r
+      catch (Throwable e) {\r
+        logger.error("MediaHandlerGeneric.set: " + e.toString());\r
+        throw new MirMediaException(e.toString());\r
+      }\r
+    }\r
+\r
+    public void produce (Entity ent, Entity mediaTypeEnt )\r
+      throws MirMediaException {\r
+\r
+      //check first if the media file exist since produced\r
+      //location is also the storage location\r
+      String date = ent.getValue("date");\r
+      String datePath = StringUtil.webdbDate2path(date);\r
+      String relPath = datePath+ent.getId()+"."+mediaTypeEnt.getValue("name");\r
+      String fname = getStoragePath()+relPath;\r
+      if(! new File(fname).exists())\r
+        throw new MirMediaException("error in MirMedia.produce(): "+relPath+\r
+                                    " does not exist!");\r
+    }\r
+\r
+    public InputStream getMedia (Entity ent, Entity mediaTypeEnt)\r
+      throws MirMediaException {\r
+      String publishPath = ent.getValue("publish_path");\r
+      String fname = getStoragePath()+publishPath;\r
+      File f = new File(fname);\r
+      if(! f.exists())\r
+        throw new MirMediaException("error in MirMedia.getMedia(): "+fname+\r
+                                    " does not exist!");\r
+      FileInputStream in;\r
+      try {\r
+        in = new FileInputStream(f);\r
+      } catch (IOException e) {\r
+        throw new MirMediaException("getMedia(): "+e.toString());\r
+      }\r
+      return in;\r
+    }\r
+\r
+    public InputStream getIcon (Entity ent) throws MirMediaException {\r
+        return null;\r
+    }\r
+\r
+    public String getStoragePath()\r
+    {\r
+        return configuration.getString("Producer.Media.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.Media.Host"));\r
+    }\r
+\r
+    public String getTinyIconName()\r
+    {\r
+        return configuration.getString("Producer.Icon.TinyText");\r
+    }\r
+\r
+    public String getBigIconName()\r
+    {\r
+        return configuration.getString("Producer.Icon.BigText");\r
+    }\r
+\r
+    public String getIconAltName()\r
+    {\r
+        return "Generic media";\r
+    }\r
+\r
+    public SimpleList getURL(Entity ent, Entity mediaTypeEnt)\r
+    {\r
+      SimpleList theList = new SimpleList();\r
+      theList.add(ent);\r
+      return theList;\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 false;\r
+    }\r
+\r
+    public String getDescr( Entity mediaType)\r
+    {\r
+      return mediaType.getValue("mime_type");\r
+    }\r
+\r
+}\r
+\r
+\r
+\r