*** empty log message ***
[mir.git] / source / mircoders / media / MediaUploadProcessor.java
index bfe1782..f17b16a 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.util.GregorianCalendar;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-\r
-import mir.entity.Entity;\r
-import mir.media.MediaExc;\r
-import mir.media.MediaFailure;\r
-import mir.media.MediaHelper;\r
-import mir.media.MirMedia;\r
-import mir.misc.StringUtil;\r
-import mir.session.UploadedFile;\r
-import mir.storage.Database;\r
-\r
-import mircoders.module.ModuleMediaType;\r
-\r
-public class MediaUploadProcessor {\r
-  public static Entity processMediaUpload(UploadedFile aFile, Map aValues) throws MediaExc, MediaFailure {\r
-    String mediaId;\r
-    MirMedia mediaHandler;\r
-    Entity mediaType;\r
-    ModuleMediaType mediaTypeModule;\r
-    Database mediaStorage;\r
-    Map values = new HashMap();\r
-    String MediaId;\r
-    Entity mediaEntity;\r
-\r
-\r
-    try {\r
-      String contentType = aFile.getContentType();\r
-\r
-      if (contentType.equals("text/plain") ||\r
-          contentType.equals("application/octet-stream") ||\r
-          contentType == null) {\r
-        throw new MediaExc("Invalid content-type: " + contentType);\r
-      }\r
-\r
-      values.putAll(aValues);\r
-      values.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));\r
-      values.put("is_produced", "0");\r
-      values.put("is_published", "1");\r
-      values.put("to_publisher", "0");\r
-      values.put("to_media_folder", "7");\r
-\r
-      mediaTypeModule = new ModuleMediaType();\r
-      mediaType = mediaTypeModule.findMediaTypeForMimeType(contentType);\r
-\r
-      try {\r
-        mediaHandler = MediaHelper.getHandler(mediaType);\r
-        mediaStorage = MediaHelper.getStorage(mediaType, "mircoders.storage.Database");\r
-      }\r
-      catch (Throwable e) {\r
-        throw new MediaFailure(e);\r
-      }\r
-\r
-      values.put("to_media_type", mediaType.getId());\r
-\r
-      try {\r
-        mediaEntity = (Entity) mediaStorage.getEntityClass().newInstance();\r
-        mediaEntity.setStorage(mediaStorage);\r
-      }\r
-      catch (Throwable e) {\r
-        throw new MediaFailure(e);\r
-      }\r
-\r
-      mediaEntity.setValues(values);\r
-      mediaId = mediaEntity.insert();\r
-\r
-      try {\r
-        mediaHandler.set(aFile.getInputStream(), mediaEntity, mediaType);\r
-      }\r
-      catch (Throwable e) {\r
-        throw new MediaFailure(e);\r
-      }\r
-\r
-      return mediaEntity;\r
-    }\r
-    catch (Throwable e) {\r
-      throw new MediaFailure(e);\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.entity.Entity;
+import mir.log.LoggerWrapper;
+import mir.media.MediaFailure;
+import mir.media.MediaHandler;
+import mir.media.MediaExc;
+import mir.misc.StringUtil;
+import mir.session.UploadedFile;
+import mir.storage.Database;
+import mir.storage.DatabaseExc;
+import mir.util.FileRoutines;
+import mircoders.module.ModuleMediaType;
+
+import java.util.GregorianCalendar;
+import java.util.HashMap;
+import java.util.Map;
+
+public class MediaUploadProcessor {
+  private static LoggerWrapper logger = new LoggerWrapper("Media.UploadProcessor");
+  private static ModuleMediaType mediaTypeModule = new ModuleMediaType();
+
+  /**
+   * Processes an uploaded media file.
+   * Will create the media entity and so on. 
+   */
+  public static Entity processMediaUpload(UploadedFile aFile, Map aValues) 
+      throws UnsupportedMediaTypeExc, MediaExc, DatabaseExc {
+    MediaHandler mediaHandler;
+    Entity mediaType = null;
+    Database mediaStorage;
+    Map values = new HashMap();
+    Entity mediaEntity;
+
+    String contentType = aFile.getContentType();
+    logger.debug("processing media upload of " + aFile.getFileName() + " (content type = " + contentType + ")");
+
+    if (contentType!=null) {
+      mediaType = mediaTypeModule.findMediaTypeForMimeType(contentType);
+    }
+
+    if (mediaType==null) {
+      String extension = FileRoutines.getExtension(aFile.getFileName());
+      mediaType = mediaTypeModule.findMediaTypeForExtension(extension);
+    }
+
+    if (mediaType==null) {
+      throw new UnsupportedMediaTypeExc("Media " + aFile.getFileName() + " " +
+          (aFile!=null?"("+aFile.getContentType()+") ":"") + " not supported", aFile.getContentType());
+    }
+
+    values.putAll(aValues);
+    values.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
+    values.put("is_produced", "0");
+
+    try {
+      mediaHandler = MediaHelper.getHandler(mediaType);
+      mediaStorage = MediaHelper.getStorage(mediaType, mediaType.getFieldValue("tablename"));
+    }
+    catch (Throwable e) {
+      throw new MediaFailure(e);
+    }
+
+    values.put("to_media_type", mediaType.getId());
+
+    mediaEntity = mediaStorage.createNewEntity();
+    mediaEntity.setFieldValues(values);
+    mediaEntity.insert();
+    mediaHandler.store(aFile, mediaEntity, mediaType);
+    try {
+    }
+    catch (Throwable e) {
+      throw new MediaFailure(e);
+    }
+
+    return mediaEntity;
+  }
 }
\ No newline at end of file