From: mh Date: Sun, 21 Jul 2002 22:38:39 +0000 (+0000) Subject: parses a multipart request's files and makes media Entity's out of them. Basically... X-Git-Tag: MIR_1_0_0_RC1~45 X-Git-Url: http://erislabs.net/gitweb/?p=mir.git;a=commitdiff_plain;h=6044a10ad2ea48a2f39e3aa0ff0ea1c1c4f527ab parses a multipart request's files and makes media Entity's out of them. Basically the old code from insposting() in ServletModuleOpenIndy --- diff --git a/source/mircoders/media/MediaRequest.java b/source/mircoders/media/MediaRequest.java new file mode 100755 index 00000000..5d472a36 --- /dev/null +++ b/source/mircoders/media/MediaRequest.java @@ -0,0 +1,237 @@ +package mircoders.media; + +import java.util.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.ServletContext; + +import mircoders.storage.DatabaseMediaType; +import mircoders.producer.ProducerMedia; +import mir.storage.StorageObjectException; +import mir.storage.Database; +import mir.module.ModuleException; +import mir.entity.*; +import mir.misc.*; +import mir.media.*; + +/* + * MediaRequest.java - + * Takes an HTTPServletRequest from a mutltipart form and finds the files + * uploaded via the com.oreilly.servlet.multipart package. Finally the + * appropriate media objects are set. + * + * @author $Author: mh $ + * @version $Revision: 1.1 $ + * + * $Log: MediaRequest.java,v $ + * Revision 1.1 2002/07/21 22:38:39 mh + * parses a multipart request's files and makes media Entity's out of them. Basically the old code from insposting() in ServletModuleOpenIndy + * + * + */ + +public class MediaRequest +{ + + WebdbMultipartRequest _mp; + String _user; + + public MediaRequest(WebdbMultipartRequest mPreq, String user) { + _mp = mPreq; + _user = user; + } + + /* + * parses the files in the uploaded media and creates media Entity's out of + * them. Produces them if the "produce" argument is true. The "publish" + * parameter determines if it should publish per default in the case where no + * is_published parameter (from the upload form) is supplied. (for backwards + * compatibility.) + */ + public EntityList getMedia(boolean produce, boolean publish) + throws MirMediaException, + MirMediaUserException { + String mediaId=null; + HashMap mediaValues = _mp.getParameters(); + EntityList returnList = new EntityList(); + MirMedia mediaHandler; + Database mediaStorage = null; + ProducerMedia mediaProducer = null; + + int i=1; + for(Iterator it = _mp.requestList.iterator(); it.hasNext();){ + try { + MpRequest mpReq = (MpRequest)it.next(); + String fileName = mpReq.getFilename(); + + //get the content-type from what the client browser + //sends us. (the "Oreilly method") + String contentType = mpReq.getContentType(); + + //theLog.printInfo("FROM BROWSER: "+contentType); + + //if the client browser sent us unknown (text/plain is default) + //or if we got application/octet-stream, it's possible that + //the browser is in error, better check against the file extension + if (contentType.equals("text/plain") || + contentType.equals("application/octet-stream")) { + /** + * Fallback to finding the mime-type through the standard ServletApi + * ServletContext getMimeType() method. + * + * This is a way to get the content-type via the .extension, + * we could maybe use a magic method as an additional method of + * figuring out the content-type, by looking at the header (first + * few bytes) of the file. (like the file(1) command). We could + * also call the "file" command through Runtime. This is an + * option that I almost prefer as it is already implemented and + * exists with an up-to-date map on most modern Unix like systems. + * I haven't found a really nice implementation of the magic method + * in pure java yet. + * + * The first method we try thought is the "Oreilly method". It + * relies on the content-type that the client browser sends and + * that sometimes is application-octet stream with + * broken/mis-configured browsers. + * + * The map file we use for the extensions is the standard web-app + * deployment descriptor file (web.xml). See Mir's web.xml or see + * your Servlet containers (most likely Tomcat) documentation. + * So if you support a new media type you have to make sure that + * it is in this file -mh + */ + ServletContext ctx = + (ServletContext)MirConfig.getPropAsObject("ServletContext"); + contentType = ctx.getMimeType(fileName); + if (contentType==null) + contentType = "text/plain"; // rfc1867 says this is the default + } + //theLog.printInfo("CONTENT TYPE IS: "+contentType); + + if (contentType.equals("text/plain") || + contentType.equals("application/octet-stream")) { + _throwBadContentType(fileName, contentType); + } + + String mediaTitle = (String)mediaValues.get("media_title"+i); + i++; + if ( (mediaTitle == null) || (mediaTitle.length() == 0)) + throw new MirMediaUserException("Missing field: media title"); + + // TODO: need to add all the extra fields that can be present in the + // admin upload form. -mh + mediaValues.put("title", mediaTitle); + mediaValues.put("date", StringUtil.date2webdbDate( + new GregorianCalendar())); + mediaValues.put("to_publisher", _user); + //mediaValues.put("to_media_folder", "7"); // op media_folder + mediaValues.put("is_produced", "0"); + + // icky backwards compatibility code -mh + if (publish == true) { + mediaValues.put("is_published", "1"); + } else { + if (!mediaValues.containsKey("is_published")) + mediaValues.put("is_published", "0"); + } + + // @todo this should probably be moved to DatabaseMediaType -mh + String[] cTypeSplit = StringUtil.split(contentType, "/"); + String wc = " mime_type LIKE '"+cTypeSplit[0]+"%'"; + + DatabaseMediaType mediaTypeStor = DatabaseMediaType.getInstance(); + EntityList mediaTypesList = mediaTypeStor.selectByWhereClause(wc); + + String mediaTypeId = null; + + //if we didn't find an entry matching the + //content-type int the table. + if (mediaTypesList.size() == 0) { + _throwBadContentType(fileName, contentType); + } + + Entity mediaType = null; + Entity mediaType2 = null; + + // find out if we an exact content-type match if so take it. + // otherwise try to match majortype/* + // @todo this should probably be moved to DatabaseMediaType -mh + for(int j=0;j