1 18 19 package org.apache.roller.ui.authoring.struts.actions; 20 21 import java.io.File ; 22 import java.io.InputStream ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import javax.servlet.ServletContext ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.apache.struts.action.ActionError; 34 import org.apache.struts.action.ActionErrors; 35 import org.apache.struts.action.ActionForm; 36 import org.apache.struts.action.ActionForward; 37 import org.apache.struts.action.ActionMapping; 38 import org.apache.struts.action.ActionMessage; 39 import org.apache.struts.action.ActionMessages; 40 import org.apache.struts.actions.DispatchAction; 41 import org.apache.struts.upload.FormFile; 42 import org.apache.roller.RollerException; 43 import org.apache.roller.config.RollerRuntimeConfig; 44 import org.apache.roller.model.FileManager; 45 import org.apache.roller.model.PropertiesManager; 46 import org.apache.roller.model.Roller; 47 import org.apache.roller.model.RollerFactory; 48 import org.apache.roller.pojos.WebsiteData; 49 import org.apache.roller.ui.core.BasePageModel; 50 import org.apache.roller.ui.core.RollerContext; 51 import org.apache.roller.ui.core.RollerRequest; 52 import org.apache.roller.ui.core.RollerSession; 53 import org.apache.roller.ui.authoring.struts.formbeans.UploadFileForm; 54 import org.apache.roller.ui.core.RequestConstants; 55 import org.apache.roller.util.RollerMessages; 56 import org.apache.roller.util.URLUtilities; 57 58 59 65 public final class UploadFileFormAction extends DispatchAction { 66 67 private static Log mLogger = LogFactory.getLog(UploadFileFormAction.class); 68 69 72 public ActionForward unspecified( 73 ActionMapping mapping, 74 ActionForm actionForm, 75 HttpServletRequest request, 76 HttpServletResponse response) 77 throws Exception { 78 79 ActionForward fwd = mapping.findForward("access-denied"); 80 RollerSession rses = RollerSession.getRollerSession(request); 81 WebsiteData website = getWebsite(request); 82 83 if (rses.isUserAuthorizedToAuthor(website)) { 84 UploadFilePageModel pageModel = new UploadFilePageModel( 85 request, response, mapping, website); 86 pageModel.setWebsite(website); 87 request.setAttribute("model", pageModel); 88 fwd = mapping.findForward("uploadFiles.page"); 89 } 90 return fwd; 91 } 92 93 94 97 public ActionForward upload( 98 ActionMapping mapping, 99 ActionForm actionForm, 100 HttpServletRequest request, 101 HttpServletResponse response) 102 throws Exception { 103 104 ActionForward fwd = mapping.findForward("access-denied"); 105 WebsiteData website = getWebsite(request); 106 RollerMessages rollerMessages = new RollerMessages(); 107 RollerSession rses = RollerSession.getRollerSession(request); 108 List lastUploads = new ArrayList (); 109 110 if ( rses.isUserAuthorizedToAuthor(website)) { 111 112 FileManager fmgr = RollerFactory.getRoller().getFileManager(); 113 fwd = mapping.findForward("uploadFiles.page"); 114 ActionMessages messages = new ActionMessages(); 115 ActionErrors errors = new ActionErrors(); 116 UploadFileForm theForm = (UploadFileForm)actionForm; 117 if (theForm.getUploadedFiles().length > 0) { 118 ServletContext app = servlet.getServletConfig().getServletContext(); 119 120 boolean uploadEnabled = 121 RollerRuntimeConfig.getBooleanProperty("uploads.enabled"); 122 123 if ( !uploadEnabled ) { 124 errors.add(ActionErrors.GLOBAL_ERROR, 125 new ActionError("error.upload.disabled", "")); 126 saveErrors(request, errors); 127 return fwd; 128 } 129 130 String encoding = request.getCharacterEncoding(); 133 if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8"))) { 134 response.setContentType("text/html; charset=utf-8"); 135 } 136 137 FormFile[] files = theForm.getUploadedFiles(); 139 int fileSize = 0; 140 try { 141 for (int i=0; i<files.length; i++) { 142 if (files[i] == null) continue; 143 144 String fileName= files[i].getFileName(); 146 int terminated = fileName.indexOf("\000"); 147 if (terminated != -1) { 148 fileName = fileName.substring(0, terminated).trim(); 150 } 151 152 fileSize = files[i].getFileSize(); 153 154 if (fmgr.canSave(website.getHandle(), fileName, 156 files[i].getContentType(), fileSize, rollerMessages)) { 157 InputStream stream = files[i].getInputStream(); 158 fmgr.saveFile(website.getHandle(), fileName, 159 files[i].getContentType(), fileSize, stream); 160 lastUploads.add(fileName); 161 } 162 163 files[i].destroy(); 165 } 166 } catch (Exception e) { 167 errors.add(ActionErrors.GLOBAL_ERROR, 168 new ActionError("error.upload.file",e.toString())); 169 mLogger.error("Error saving uploaded file", e); 170 } 171 } 172 173 UploadFilePageModel pageModel = new UploadFilePageModel( 174 request, response, mapping, website, lastUploads); 175 request.setAttribute("model", pageModel); 176 pageModel.setWebsite(website); 177 178 RollerContext rctx = RollerContext.getRollerContext(); 179 String baseURL = RollerRuntimeConfig.getAbsoluteContextURL(); 180 String resourcesBaseURL = baseURL + fmgr.getUploadUrl() + "/" + website.getHandle(); 181 Iterator uploads = lastUploads.iterator(); 182 if (uploads.hasNext()) { 183 messages.add(ActionMessages.GLOBAL_MESSAGE, 184 new ActionMessage("uploadFiles.uploadedFiles")); 185 } 186 while (uploads.hasNext()) { 187 messages.add(ActionMessages.GLOBAL_MESSAGE, 188 new ActionMessage("uploadFiles.uploadedFile", 189 URLUtilities.getWeblogResourceURL(website, (String )uploads.next(), true))); 190 } 191 saveMessages(request, messages); 192 193 Iterator iter = rollerMessages.getErrors(); 194 while (iter.hasNext()) { 195 RollerMessages.RollerMessage error = 196 (RollerMessages.RollerMessage)iter.next(); 197 errors.add(ActionErrors.GLOBAL_ERROR, 198 new ActionError(error.getKey(), error.getArgs())); 199 } 200 saveErrors(request, errors); 201 } 202 return fwd; 203 } 204 205 206 209 public ActionForward delete( 210 ActionMapping mapping, 211 ActionForm actionForm, 212 HttpServletRequest request, 213 HttpServletResponse response) 214 throws Exception { 215 216 ActionMessages messages = new ActionMessages(); 217 ActionErrors errors = new ActionErrors(); 218 UploadFileForm theForm = (UploadFileForm)actionForm; 219 ActionForward fwd = mapping.findForward("access-denied"); 220 WebsiteData website = getWebsite(request); 221 222 int count = 0; 223 RollerSession rses = RollerSession.getRollerSession(request); 224 if (rses.isUserAuthorizedToAuthor(website)) { 225 fwd = mapping.findForward("uploadFiles.page"); 226 try { 227 FileManager fmgr = RollerFactory.getRoller().getFileManager(); 228 String [] deleteFiles = theForm.getDeleteFiles(); 229 for (int i=0; i<deleteFiles.length; i++) { 230 if ( deleteFiles[i].trim().startsWith("/") 231 || deleteFiles[i].trim().startsWith("\\") 232 || deleteFiles[i].indexOf("..") != -1) { 233 } else { 235 fmgr.deleteFile(website.getHandle(), deleteFiles[i]); 236 count++; 237 } 238 } 239 } catch (Exception e) { 240 errors.add(ActionErrors.GLOBAL_ERROR, 241 new ActionError("error.upload.file",e.toString())); 242 saveErrors(request,errors); 243 } 244 245 messages.add(ActionMessages.GLOBAL_MESSAGE, 246 new ActionMessage("uploadFiles.deletedFiles", new Integer (count))); 247 saveMessages(request, messages); 248 249 UploadFilePageModel pageModel = new UploadFilePageModel( 250 request, response, mapping, website); 251 pageModel.setWebsite(website); 252 request.setAttribute("model", pageModel); 253 } 254 return fwd; 255 } 256 257 258 263 public static WebsiteData getWebsite(HttpServletRequest request) throws RollerException { 264 RollerRequest rreq = RollerRequest.getRollerRequest(request); 265 WebsiteData website = rreq.getWebsite(); 266 if (website != null) { 267 request.getSession().setAttribute(RequestConstants.WEBLOG_SESSION_STASH, website.getHandle()); 268 } else { 269 String handle = (String )request.getSession().getAttribute(RequestConstants.WEBLOG_SESSION_STASH); 270 Roller roller = RollerFactory.getRoller(); 271 website = roller.getUserManager().getWebsiteByHandle(handle); 272 } 273 return website; 274 } 275 276 277 278 public class UploadFilePageModel extends BasePageModel { 279 private String resourcesBaseURL = null; 280 private boolean uploadEnabled = true; 281 private boolean overQuota = false; 282 private String maxDirMB = null; private String maxFileMB = null; private List files = null; 285 private long totalSize = 0; 286 private List lastUploads = null; 287 288 public UploadFilePageModel( 289 HttpServletRequest req, 290 HttpServletResponse res, 291 ActionMapping mapping, 292 WebsiteData weblog) throws RollerException { 293 this(req, res, mapping, weblog, null); 294 } 295 296 public UploadFilePageModel( 297 HttpServletRequest req, 298 HttpServletResponse res, 299 ActionMapping mapping, 300 WebsiteData weblog, 301 List lastUploads) throws RollerException { 302 303 super("uploadFiles.title", req, res, mapping); 304 305 Roller roller = RollerFactory.getRoller(); 306 PropertiesManager pmgr = roller.getPropertiesManager(); 307 FileManager fmgr = roller.getFileManager(); 308 309 String dir = fmgr.getUploadDir(); 310 resourcesBaseURL = URLUtilities.getWeblogResourceURL(weblog, "", false); 311 312 RollerRequest rreq = RollerRequest.getRollerRequest(req); 313 WebsiteData website = UploadFileFormAction.getWebsite(req); 314 maxDirMB = RollerRuntimeConfig.getProperty("uploads.dir.maxsize"); 315 maxFileMB = RollerRuntimeConfig.getProperty("uploads.file.maxsize"); 316 317 overQuota = fmgr.overQuota(weblog.getHandle()); 318 uploadEnabled = RollerRuntimeConfig.getBooleanProperty("uploads.enabled"); 319 320 files = new ArrayList (); 321 File [] rawFiles = fmgr.getFiles(weblog.getHandle()); 322 for (int i=0; i<rawFiles.length; i++) { 323 files.add(new FileBean(rawFiles[i])); 324 totalSize += rawFiles[i].length(); 325 } 326 Collections.sort(files, new FileBeanNameComparator()); 327 } 328 public boolean isUploadEnabled() { 329 return uploadEnabled; 330 } 331 public boolean isOverQuota() { 332 return overQuota; 333 } 334 public String getMaxDirMB() { 335 return maxDirMB; 336 } 337 public String getMaxFileMB() { 338 return maxFileMB; 339 } 340 public String getResourcesBaseURL() { 341 return resourcesBaseURL; 342 } 343 public long getTotalSize() { 344 return totalSize; 345 } 346 public List getFiles() { 347 return files; 348 } 349 public List getLastUploads() { 350 return lastUploads; 351 } 352 } 353 354 355 359 public class FileBean { 360 private File file; 361 public FileBean(File file) { 362 this.file = file; 363 } 364 public String getName() { return file.getName(); } 365 public long getLength() { return file.length(); } 366 } 367 368 369 public class FileBeanNameComparator implements Comparator { 370 public int compare(Object o1, Object o2) { 371 FileBean fb1 = (FileBean)o1; 372 FileBean fb2 = (FileBean)o2; 373 return fb1.getName().compareTo(fb2.getName()); 374 } 375 public boolean equals(Object o1, Object o2) { 376 FileBean fb1 = (FileBean)o1; 377 FileBean fb2 = (FileBean)o2; 378 return fb1.getName().equals(fb2.getName()); 379 } 380 } 381 382 } 383 | Popular Tags |