rebuilding head
[mir.git] / source / mircoders / media / MediaUploadProcessor.java
index f731863..327eb11 100755 (executable)
  * 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.  
+ * 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.util.GregorianCalendar;
-import java.util.HashMap;
-import java.util.Map;
-
 import mir.entity.Entity;
 import mir.media.MediaExc;
 import mir.media.MediaFailure;
-import mir.media.MediaHelper;
-import mir.media.MirMedia;
+import mir.media.MediaHandler;
 import mir.misc.StringUtil;
 import mir.session.UploadedFile;
 import mir.storage.Database;
+import mir.log.LoggerWrapper;
+import mir.util.FileFunctions;
 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 MediaExc, MediaFailure {
-    String mediaId;
-    MirMedia mediaHandler;
+    MediaHandler mediaHandler;
     Entity mediaType;
-    ModuleMediaType mediaTypeModule;
     Database mediaStorage;
     Map values = new HashMap();
-    String MediaId;
     Entity mediaEntity;
 
+    String contentType = aFile.getContentType();
+    logger.debug("processing media upload of " + aFile.getFileName() + " (content type = " + contentType + ")");
 
-    try {
-      String contentType = aFile.getContentType();
-
+    if (contentType!=null) {
       if (contentType.equals("text/plain") ||
           contentType.equals("application/octet-stream") ||
           contentType == null) {
         throw new MediaExc("Invalid content-type: " + contentType);
       }
+    }
 
-      values.putAll(aValues);
-      values.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
-      values.put("is_produced", "0");
-
-      mediaTypeModule = new ModuleMediaType();
-      mediaType = mediaTypeModule.findMediaTypeForMimeType(contentType);
-
-      try {
-        mediaHandler = MediaHelper.getHandler(mediaType);
-        mediaStorage = MediaHelper.getStorage(mediaType, "mircoders.storage.Database");
+    try {
+      if (contentType!=null) {
+        mediaType = mediaTypeModule.findMediaTypeForExtension(contentType);
       }
-      catch (Throwable e) {
-        throw new MediaFailure(e);
+      else {
+        String extension = FileFunctions.getExtension(aFile.getFileName());
+        mediaType = mediaTypeModule.findMediaTypeForExtension(extension);
       }
+    }
+    catch (Throwable t) {
+      throw new MediaFailure(t);
+    }
 
-      values.put("to_media_type", mediaType.getId());
+    values.putAll(aValues);
+    values.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
+    values.put("is_produced", "0");
 
-      try {
-        mediaEntity = (Entity) mediaStorage.getEntityClass().newInstance();
-        mediaEntity.setStorage(mediaStorage);
-      }
-      catch (Throwable e) {
-        throw new MediaFailure(e);
-      }
+    try {
+      mediaHandler = MediaHelper.getHandler(mediaType);
+      mediaStorage = MediaHelper.getStorage(mediaType, mediaType.getFieldValue("tablename"));
+    }
+    catch (Throwable e) {
+      throw new MediaFailure(e);
+    }
 
-      mediaEntity.setValues(values);
-      mediaId = mediaEntity.insert();
+    values.put("to_media_type", mediaType.getId());
 
-      try {
-        mediaHandler.set(aFile.getInputStream(), mediaEntity, mediaType);
-      }
-      catch (Throwable e) {
-        throw new MediaFailure(e);
-      }
+    try {
+      mediaEntity = (Entity) mediaStorage.getEntityClass().newInstance();
+      mediaEntity.setStorage(mediaStorage);
+    }
+    catch (Throwable e) {
+      throw new MediaFailure(e);
+    }
 
-      return mediaEntity;
+    mediaEntity.setFieldValues(values);
+    try {
+      mediaEntity.insert();
+      mediaHandler.store(aFile, mediaEntity, mediaType);
     }
     catch (Throwable e) {
       throw new MediaFailure(e);
     }
 
+    return mediaEntity;
   }
 }
\ No newline at end of file