Videos don't actually do a "produce()" method.
[mir.git] / source / mircoders / media / MediaHandlerGeneric.java
index a759cda..e060988 100755 (executable)
-/*\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 freemarker.template.SimpleList;\r
-\r
-import mir.log.LoggerWrapper;\r
-\r
-import mir.config.MirPropertiesConfiguration;\r
-import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
-import mir.entity.Entity;\r
-import mir.media.MirMedia;\r
-import mir.media.MirMediaException;\r
-import mir.misc.FileUtil;\r
-import mir.misc.StringUtil;\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.14 2003/02/23 05:00:14 zapata 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
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * 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  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;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.entity.Entity;
+import mir.log.LoggerWrapper;
+import mir.media.MediaExc;
+import mir.media.MediaFailure;
+import mir.misc.StringUtil;
+import mir.session.UploadedFile;
+import mir.util.IORoutines;
+
+import javax.servlet.ServletContext;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+
+
+/**
+ * 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 MirMediaHandler interface.
+ *
+ * @see mir.media.MediaHandler
+ * @author mh <mh@nadir.org>
+ * @version $Id: MediaHandlerGeneric.java,v 1.20.2.10 2006/11/12 21:32:13 yossarian Exp $
+ */
+
+public class MediaHandlerGeneric extends AbstractMediaHandler {
+  protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
+
+  protected LoggerWrapper logger = new LoggerWrapper("Media.Generic");
+
+  /** {@inheritDoc} */
+  public void store(UploadedFile anUploadedFile, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    try {
+      anUploadedFile.writeToFile(getMediaStorageFile(aMedia, aMediaType));
+      aMedia.setFieldValue("publish_path", getRelativeMediaStoragePath(aMedia, aMediaType));
+      aMedia.setFieldValue("size", Long.toString(getMediaStorageFile(aMedia, aMediaType).length()));
+      aMedia.update();
+      reportChange(getMediaStorageFile(aMedia, aMediaType).getAbsolutePath());
+    }
+    catch (Throwable e) {
+      logger.error("MediaHandlerGeneric.set: " + e.toString());
+      throw new MediaFailure(e);
+    }
+  }
+
+  /** {@inheritDoc} */
+  public void store(InputStream anInputStream, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    try {
+      IORoutines.copyStream(anInputStream, new FileOutputStream(getMediaStorageFile(aMedia, aMediaType)));
+
+      aMedia.setFieldValue("publish_path", getRelativeMediaStoragePath(aMedia, aMediaType));
+      aMedia.setFieldValue("size", Long.toString(getMediaStorageFile(aMedia, aMediaType).length()));
+      aMedia.update();
+      reportChange(getMediaStorageFile(aMedia, aMediaType).getAbsolutePath());
+    }
+    catch (Throwable e) {
+      logger.error("MediaHandlerGeneric.set: " + e.toString());
+      throw new MediaFailure(e);
+    }
+  }
+
+  /** {@inheritDoc} */
+  public void produce(Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    if (!getMediaStorageFile(aMedia, aMediaType).exists()) {
+      throw new MediaExc("error in producing media:: " + getMediaStorageFile(aMedia, aMediaType) + " does not exist!");
+    }
+  }
+
+  /** {@inheritDoc} */
+  public InputStream getMedia(Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    try {
+      return new FileInputStream(getMediaStorageFile(aMedia, aMediaType));
+    }
+    catch (Throwable e) {
+      throw new MediaFailure("MediaHandlerGeneric.getMedia(): " + e.toString(), e);
+    }
+  }
+
+  public InputStream getThumbnail(Entity ent) throws MediaExc, MediaFailure {
+    return null;
+  }
+
+  public String getThumbnailMimeType(Entity aMediaEntity, Entity aMediaType) throws MediaExc, MediaFailure {
+    ServletContext servletContext = MirPropertiesConfiguration.getContext();
+    String fileName = aMediaEntity.getId() + "." + aMediaType.getFieldValue("name");
+
+    return servletContext.getMimeType(fileName);
+  }
+
+  public String getBaseStoragePath() {
+    return configuration.getString("Producer.Media.Path");
+  }
+
+  public String getBaseIconStoragePath() {
+    return configuration.getString("Producer.Image.IconPath");
+  }
+
+  public String getPublishHost() {
+    return StringUtil.removeSlash(configuration.getString("Producer.Media.Host"));
+  }
+
+  public String getTinyIconName() {
+    return configuration.getString("Producer.Icon.TinyText");
+  }
+
+  public String getBigIconName() {
+    return configuration.getString("Producer.Icon.BigText");
+  }
+
+  public String getIconAltName() {
+    return "Generic media";
+  }
+
+  public String getDescr(Entity mediaType) {
+    return mediaType.getFieldValue("mime_type");
+  }
+
+}
+
+
+