1 12 13 18 package com.openedit.modules.admin.filemanager; 19 20 import java.io.IOException ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 27 import org.apache.commons.fileupload.FileItem; 28 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 29 import org.apache.commons.fileupload.servlet.ServletFileUpload; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.openedit.repository.ContentItem; 33 import org.openedit.repository.InputStreamItem; 34 35 import com.openedit.OpenEditException; 36 import com.openedit.WebPageRequest; 37 import com.openedit.page.Page; 38 import com.openedit.page.manage.PageManager; 39 import com.openedit.util.PathUtilities; 40 41 46 public class FileUpload 47 { 48 protected PageManager fieldPageManager; 49 50 51 public static final int BUFFER_SIZE = 1000000; 52 53 private static final Log log = LogFactory.getLog(FileUpload.class); 54 55 58 public Map uploadFile(WebPageRequest inContext) throws OpenEditException 59 { 60 if (inContext.getUser() == null && inContext.getUser().hasPermission("oe.edit")) 61 { 62 throw new OpenEditException("You must be logged in to upload files"); 63 } 64 65 Map props = parseArguments(inContext); 66 if( props == null) 67 { 68 return null; 69 } 70 String fullpath = (String )props.get("fullpath"); 71 String path = (String )props.get("path"); 72 73 if (path == null && fullpath == null) 74 { 75 log.error("No path specified in multipart form"); 76 throw new OpenEditException("No path passed in with the upload"); 77 } 78 String home = (String )inContext.getPageValue("home"); 79 if( home != null && home.length() > 0 && path.startsWith(home)) 80 { 81 path = path.substring(home.length()); 82 } 83 84 FileItem fileItem = (FileItem)props.get("file"); 85 86 String finalpath = null; 87 if ( fullpath != null ) 88 { 89 finalpath = fullpath; 90 } 91 else 92 { 93 String name = fileItem.getName(); 95 name = name = name.replace('\\','/'); 96 name = PathUtilities.extractFileName(name); 97 98 if( path.endsWith("/")) 99 { 100 finalpath = path + name; 101 } 102 else 103 { 104 Page page = getPageManager().getPage(path); 105 if ( page.isFolder() ) { 107 finalpath = path + "/" + name; 108 } 109 else 110 { 111 finalpath = path; 112 } 113 } 114 } 115 finalpath = finalpath.replaceAll(" ",""); 116 117 Page page = saveFile( props, finalpath, inContext ); 118 props.put("savedas",page); 119 inContext.setRequestParameter("path", page.getPath()); 120 return props; 121 } 122 123 124 128 public Page saveFile(Map inProps, String inPath, WebPageRequest inContext) throws OpenEditException 129 { 130 132 if (inPath.indexOf("..") > -1) 134 { 135 throw new OpenEditException("Illegal path name"); 136 } 137 final String path = PathUtilities.resolveRelativePath( inPath, "/"); 138 Page page = getPageManager().getPage( path ); 139 final FileItem fileItem = (FileItem)inProps.get("file"); 140 InputStreamItem revision = new InputStreamItem(); 141 if ( inContext.getUser() == null) 143 { 144 throw new IllegalArgumentException ("No user logged in"); 145 } 146 revision.setAuthor( inContext.getUser().getUserName() ); 147 revision.setType( ContentItem.TYPE_ADDED ); 148 revision.setMessage( "Uploaded file"); 149 revision.setPath(path); 150 try 151 { 152 revision.setInputStream(fileItem.getInputStream() ); 153 } catch ( IOException ex) 154 { 155 throw new OpenEditException(ex); 156 } 157 page.setContentItem(revision); 158 getPageManager().putPage(page); 159 160 inContext.putPageValue("path",path); 161 inContext.setRequestParameter("path",path); 162 163 return page; 164 } 165 166 167 168 172 public Map parseArguments(WebPageRequest inContext) throws OpenEditException 173 { 174 if ( inContext.getRequest() == null) { 176 return inContext.getPageMap(); 177 } 178 String type = inContext.getRequest().getContentType(); 179 if ( type == null || !type.startsWith("multipart")) 180 { 181 return null; 182 } 183 184 ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory()); 185 187 HttpServletRequest req = inContext.getRequest(); 188 String encode = req.getCharacterEncoding(); 189 if ( encode == null) 190 { 191 log.info("Encoding not set."); 192 encode = "UTF-8"; 193 } 194 log.info("Encoding is set to " + encode); 195 upload.setHeaderEncoding(encode); 196 197 upload.setSizeMax(-1); 201 202 List fileItems; 203 204 try 205 { 206 fileItems = upload.parseRequest(inContext.getRequest()); 207 } 208 catch (Exception e) 209 { 210 throw new OpenEditException(e); 211 } 212 213 214 Map props = new HashMap (); 218 for (int i = 0; i < fileItems.size(); i++) 219 { 220 FileItem tmp = (FileItem) fileItems.get(i); 221 if (tmp.getFieldName().equals("file")) 222 { 223 props.put(tmp.getFieldName(),tmp); 224 props.put("filepath", tmp.getName()); 225 } 226 else 227 { 228 props.put(tmp.getFieldName(),tmp.getString()); 229 } 230 } 231 return props; 232 } 233 public PageManager getPageManager() 234 { 235 return fieldPageManager; 236 } 237 public void setPageManager( PageManager pageManager ) 238 { 239 fieldPageManager = pageManager; 240 } 241 } 242 | Popular Tags |