1.1 restoration
[mir.git] / source / mircoders / media / MediaHandlerGeneric.java
index eb53cd9..d023b3d 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;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
-
 import mir.config.MirPropertiesConfiguration;
-import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
 import mir.entity.Entity;
 import mir.log.LoggerWrapper;
 import mir.media.MediaExc;
 import mir.media.MediaFailure;
-import mir.media.MirMedia;
-import mir.misc.FileUtil;
 import mir.misc.StringUtil;
-import freemarker.template.SimpleList;
+import mir.session.UploadedFile;
+import mir.util.FileFunctions;
+
+import javax.servlet.ServletContext;
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
 
 
 /**
@@ -60,143 +58,123 @@ import freemarker.template.SimpleList;
  * we don't have entered in the media_type table, (like RTF documents,
  * PS, PDF, etc..)
  * <p>
- * Of course it implements the MirMedia interface.
+ * Of course it implements the MirMediaHandler interface.
  *
- * @see mir.media.MirMedia
+ * @see mir.media.MediaHandler
  * @author mh <mh@nadir.org>
- * @version $Id: MediaHandlerGeneric.java,v 1.18 2003/03/09 19:14:21 idfx Exp $
+ * @version $Id: MediaHandlerGeneric.java,v 1.20.2.7 2004/11/21 22:07:14 zapata Exp $
  */
 
-public class MediaHandlerGeneric implements MirMedia
+public class MediaHandlerGeneric extends AbstractMediaHandler
 {
-    protected static MirPropertiesConfiguration configuration;
-    protected static String imageHost;
-    protected static String imageRoot;
-
-    protected LoggerWrapper logger;
-
-    static {
-      try {
-        configuration = MirPropertiesConfiguration.instance();
-      }
-      catch (PropertiesConfigExc e) {
-      }
-      imageHost = configuration.getString("Producer.Image.Host");
-      imageRoot = configuration.getString("Producer.ImageRoot");
-    }
+  protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
 
-    public MediaHandlerGeneric() {
-      logger = new LoggerWrapper("Media.Generic");
-    }
+  protected LoggerWrapper logger = new LoggerWrapper("Media.Generic");
+
+  /** {@inheritDoc} */
+  public void store(File aFile, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    try {
+      FileFunctions.move(aFile, getStorageFile(aMedia, aMediaType));
 
-    public void set (InputStream in, Entity ent, Entity mediaTypeEnt ) throws MediaExc, MediaFailure {
-      String ext = mediaTypeEnt.getValue("name");
-      String mediaFname = ent.getId() + "." + ext;
-      String date = ent.getValue("date");
-      String datePath = StringUtil.webdbDate2path(date);
-      try {
-        long size = FileUtil.write(getStoragePath() + File.separator + datePath +
-                                   File.separator + mediaFname, in);
-        ent.setValueForProperty("publish_path", datePath + mediaFname);
-        ent.setValueForProperty("size", new Long(size).toString());
-        ent.update();
-      }
-      catch (Throwable e) {
-        logger.error("MediaHandlerGeneric.set: " + e.toString());
-        throw new MediaFailure(e);
-      }
+      aMedia.setFieldValue("publish_path", getRelativeStorageFile(aMedia, aMediaType));
+      aMedia.setFieldValue("size", Long.toString(getStorageFile(aMedia, aMediaType).length()));
+      aMedia.update();
     }
+    catch (Throwable e) {
+      logger.error("MediaHandlerGeneric.set: " + e.toString());
+      throw new MediaFailure(e);
+    }
+  }
 
-    public void produce (Entity ent, Entity mediaTypeEnt ) throws MediaExc, MediaFailure {
-      //check first if the media file exist since produced
-      //location is also the storage location
+  /** {@inheritDoc} */
+  public void store(UploadedFile anUploadedFile, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    try {
+      anUploadedFile.writeToFile(getStorageFile(aMedia, aMediaType));
 
-      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 MediaExc("error in MirMedia.produce(): " + relPath + " does not exist!");
+      aMedia.setFieldValue("publish_path", getRelativeStorageFile(aMedia, aMediaType));
+      aMedia.setFieldValue("size", Long.toString(getStorageFile(aMedia, aMediaType).length()));
+      aMedia.update();
     }
-
-    public InputStream getMedia (Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
-      String publishPath = ent.getValue("publish_path");
-      String fname = getStoragePath()+publishPath;
-      File f = new File(fname);
-      if(! f.exists())
-        throw new MediaExc("error in MirMedia.getMedia(): " + fname + " does not exist!");
-
-      FileInputStream inputStream;
-      try {
-        inputStream = new FileInputStream(f);
-      }
-      catch (Throwable e) {
-        throw new MediaFailure("MediaHandlerGeneric.getMedia(): " + e.toString(), e);
-      }
-
-      return inputStream;
+    catch (Throwable e) {
+      logger.error("MediaHandlerGeneric.set: " + e.toString());
+      throw new MediaFailure(e);
     }
+  }
 
-    public InputStream getIcon (Entity ent) throws MediaExc, MediaFailure {
-        return null;
-    }
+  /** {@inheritDoc} */
+  public void store(InputStream anInputStream, Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    try {
+      FileFunctions.copy(anInputStream, getStorageFile(aMedia, aMediaType));
 
-    public String getStoragePath()
-    {
-        return configuration.getString("Producer.Media.Path");
+      aMedia.setFieldValue("publish_path", getRelativeStorageFile(aMedia, aMediaType));
+      aMedia.setFieldValue("size", Long.toString(getStorageFile(aMedia, aMediaType).length()));
+      aMedia.update();
     }
-
-    public String getIconStoragePath()
-    {
-        return configuration.getString("Producer.Image.IconPath");
+    catch (Throwable e) {
+      logger.error("MediaHandlerGeneric.set: " + e.toString());
+      throw new MediaFailure(e);
     }
+  }
 
-    public String getPublishHost()
-    {
-        return StringUtil.removeSlash(configuration.getString("Producer.Media.Host"));
-    }
+  public void produce(Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    if (!getStorageFile(aMedia, aMediaType).exists())
+      throw new MediaExc("error in producing media:: " + getStorageFile(aMedia, aMediaType) + " does not exist!");
+  }
 
-    public String getTinyIconName()
-    {
-        return configuration.getString("Producer.Icon.TinyText");
-    }
+  /**
+   * Get access to the raw media data by an {@link InputStream}
+   */
+  public InputStream getMedia(Entity aMedia, Entity aMediaType) throws MediaExc, MediaFailure {
+    File file = getStorageFile(aMedia, aMediaType);
+    if (!file.exists())
+      throw new MediaExc("error in MirMediaHandler.getMedia(): " + file + " does not exist!");
 
-    public String getBigIconName()
-    {
-        return configuration.getString("Producer.Icon.BigText");
+    try {
+      return new BufferedInputStream(new FileInputStream(file));
     }
-
-    public String getIconAltName()
-    {
-        return "Generic media";
+    catch (Throwable e) {
+      throw new MediaFailure("MediaHandlerGeneric.getMedia(): " + e.toString(), e);
     }
+  }
 
-    public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
-    {
-      SimpleList theList = new SimpleList();
-      theList.add(ent);
-      return theList;
-    }
+  public InputStream getThumbnail(Entity ent) throws MediaExc, MediaFailure {
+    return null;
+  }
 
-    public boolean isVideo()
-    {
-      return false;
-    }
+  public String getThumbnailMimeType(Entity aMediaEntity, Entity aMediaType) throws MediaExc, MediaFailure {
+    ServletContext servletContext = MirPropertiesConfiguration.getContext();
+    String fileName = aMediaEntity.getId() + "." + aMediaType.getFieldValue("name");
 
-    public boolean isAudio()
-    {
-      return false;
-    }
+    return servletContext.getMimeType(fileName);
+  };
 
-    public boolean isImage()
-    {
-      return false;
-    }
+  public String getStoragePath() {
+    return configuration.getString("Producer.Media.Path");
+  }
 
-    public String getDescr( Entity mediaType)
-    {
-      return mediaType.getValue("mime_type");
-    }
+  public String getIconStoragePath() {
+    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");
+  }
 
 }