modified this code to use the newer media handling routines.
authorjohn <john>
Fri, 2 Nov 2001 17:05:06 +0000 (17:05 +0000)
committerjohn <john>
Fri, 2 Nov 2001 17:05:06 +0000 (17:05 +0000)
images seem to work more or less fine.  may need some tweaking.  they show up on the edit menu at this point, at least.  hoorah.

theoretically, one could use this same Module "as-is" to add
non-image media to media folders and articles.  results will probably be unpredictable and potentially amusing.

source/mircoders/servlet/ServletModuleImages.java

index ac7d746..843079e 100755 (executable)
@@ -1,9 +1,11 @@
 package mircoders.servlet;
 
 import java.io.*;
+import java.lang.*;
 import java.sql.*;
 import java.util.*;
 import java.net.*;
+import java.lang.reflect.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 
@@ -16,10 +18,12 @@ import mir.module.*;
 import mir.misc.*;
 import mir.entity.*;
 import mir.storage.*;
+import mir.media.*;
 
 import mircoders.entity.*;
 import mircoders.storage.*;
 import mircoders.module.*;
+import mircoders.producer.*;
 
 /*
  *  ServletModuleBilder -
@@ -69,6 +73,7 @@ public class ServletModuleImages extends mir.servlet.ServletModule
     try {
       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
       HashMap parameters = mp.getParameters();
+      String mediaId=null;
       MpRequest mpReq = (MpRequest)mp.requestList.get(0);
       byte[] imageData=mpReq.getMedia();
       String fileName=mpReq.getFilename();
@@ -86,17 +91,66 @@ public class ServletModuleImages extends mir.servlet.ServletModule
 
       if (imageData!=null && fileName!=null) {
         String fileType = "-1";
-        if (contentType.equals("image/jpeg")) fileType="0";
-        if (contentType.equals("image/gif")) fileType="1";
+        
+       //the where clause to find the media_type entry
+        //from the content-type.
+        //we use the media type entry to lookup the 
+        //media Handler/Storage classes
+        String wc = " mime_type='"+contentType+"'";
+
+        EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
+       String mediaTypeId = null;
+        String mediaStorageName = null;
+        String mediaHandlerName = null;
+        //if we found an entry matching the
+        //content-type int the table.
+        if (mediaTypesList.size() > 0) {
+          //get the class names from the media_type table.
+          mediaTypeId = mediaTypesList.elementAt(0).getId();
+          mediaStorageName = mediaTypesList.elementAt(0).getValue("tablename");
+          mediaHandlerName = mediaTypesList.elementAt(0).getValue("classname");
+          parameters.put("to_media_type",mediaTypeId);
+         
+          //load the classes via reflection
+          String MediaId;
+          try {
+                Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
+                Method m = mediaStorageClass.getMethod("getInstance", null);
+                Database mediaStorage = (Database)m.invoke(null, null);
+                Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
+                mediaEnt.setStorage(mediaStorage);
+                mediaEnt.setValues(parameters);
+                mediaId = mediaEnt.insert();
+
+                Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
+                MirMedia mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
+                //save and store the media data/metadata
+                mediaHandler.set(mpReq.getMedia(), mediaEnt,mediaTypesList.elementAt(0));
+
+                //were done with mpReq at this point, dereference it. as it contains
+                //mucho mem. -mh 01.10.2001
+                mpReq=null;
+              
+              if(mediaId!=null){
+                new ProducerMedia().handle(null, null, false, false, mediaId);
+              }
+         } catch (Exception e) {
+             theLog.printError("setting uploaded_media failed: "+e.toString());
+          } //end try-catch
+
+
         if (fileType != "-1")
           entImage.setImage(imageData, fileType);
         else
           theLog.printError("Wrong file uploaded!");
       }
       _edit(id, req, res);
+      }
     }
     catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
     catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
+    catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
 
   }