1 18 19 package org.apache.roller.business; 20 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.math.BigDecimal ; 26 import java.util.Map ; 27 import org.apache.commons.lang.StringUtils; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.roller.RollerException; 31 import org.apache.roller.config.RollerConfig; 32 import org.apache.roller.config.RollerRuntimeConfig; 33 import org.apache.roller.model.FileManager; 34 import org.apache.roller.model.Roller; 35 import org.apache.roller.model.RollerFactory; 36 import org.apache.roller.pojos.RollerPropertyData; 37 import org.apache.roller.util.RollerMessages; 38 39 40 44 public class FileManagerImpl implements FileManager { 45 46 private String upload_dir = null; 47 private String upload_url = null; 48 49 private static Log mLogger = LogFactory.getLog(FileManagerImpl.class); 50 51 52 55 public FileManagerImpl() { 56 String uploaddir = RollerConfig.getProperty("uploads.dir"); 57 String uploadurl = RollerConfig.getProperty("uploads.url"); 58 59 61 if(uploaddir == null || uploaddir.trim().length() < 1) 62 uploaddir = System.getProperty("user.home") + File.separator+"roller_data"+File.separator+"uploads"; 63 64 if( ! uploaddir.endsWith(File.separator)) 65 uploaddir += File.separator; 66 67 if(uploadurl == null || uploadurl.trim().length() < 1) 68 uploadurl = File.separator+"resources"; 69 70 this.upload_dir = uploaddir.replace('/',File.separatorChar); 71 this.upload_url = uploadurl; 72 } 73 74 75 78 public String getUploadDir() { 79 return this.upload_dir; 80 } 81 82 83 86 public String getUploadUrl() { 87 return this.upload_url; 88 } 89 90 91 94 public boolean canSave(String weblogHandle, String name, String contentType, 95 long size, RollerMessages messages) 96 throws RollerException { 97 98 Roller mRoller = RollerFactory.getRoller(); 99 Map config = mRoller.getPropertiesManager().getProperties(); 100 101 if (!((RollerPropertyData)config.get("uploads.enabled")).getValue().equalsIgnoreCase("true")) { 102 messages.addError("error.upload.disabled"); 103 return false; 104 } 105 106 String allows = ((RollerPropertyData)config.get("uploads.types.allowed")).getValue(); 107 String forbids = ((RollerPropertyData)config.get("uploads.types.forbid")).getValue(); 108 String [] allowFiles = StringUtils.split(StringUtils.deleteWhitespace(allows), ","); 109 String [] forbidFiles = StringUtils.split(StringUtils.deleteWhitespace(forbids), ","); 110 if (!checkFileType(allowFiles, forbidFiles, name, contentType)) { 111 messages.addError("error.upload.forbiddenFile", allows); 112 return false; 113 } 114 115 BigDecimal maxDirMB = new BigDecimal ( 116 ((RollerPropertyData)config.get("uploads.dir.maxsize")).getValue()); 117 int maxDirBytes = (int)(1024000 * maxDirMB.doubleValue()); 118 int userDirSize = getWebsiteDirSize(weblogHandle, this.upload_dir); 119 if (userDirSize + size > maxDirBytes) { 120 messages.addError("error.upload.dirmax", maxDirMB.toString()); 121 return false; 122 } 123 124 BigDecimal maxFileMB = new BigDecimal ( 125 ((RollerPropertyData)config.get("uploads.file.maxsize")).getValue()); 126 int maxFileBytes = (int)(1024000 * maxFileMB.doubleValue()); 127 mLogger.debug(""+maxFileBytes); 128 mLogger.debug(""+size); 129 if (size > maxFileBytes) { 130 messages.addError("error.upload.filemax", maxFileMB.toString()); 131 return false; 132 } 133 134 return true; 135 } 136 137 138 public boolean overQuota(String weblogHandle) throws RollerException { 139 140 String maxDir = RollerRuntimeConfig.getProperty("uploads.dir.maxsize"); 141 String maxFile = RollerRuntimeConfig.getProperty("uploads.file.maxsize"); 142 BigDecimal maxDirSize = new BigDecimal (maxDir); BigDecimal maxFileSize = new BigDecimal (maxFile); 145 int maxDirBytes = (int)(1024000 * maxDirSize.doubleValue()); 147 int userDirSize = 0; 148 String dir = getUploadDir(); 149 File d = new File (dir + weblogHandle); 150 if (d.mkdirs() || d.exists()) { 151 File [] files = d.listFiles(); 152 long dirSize = 0l; 153 for (int i=0; i<files.length; i++) { 154 if (!files[i].isDirectory()) { 155 dirSize = dirSize + files[i].length(); 156 } 157 } 158 userDirSize = new Long (dirSize).intValue(); 159 } 160 return userDirSize > maxDirBytes; 161 } 162 163 164 169 public File [] getFiles(String weblogHandle) throws RollerException { 170 String dir = this.upload_dir + weblogHandle; 171 File uploadDir = new File (dir); 172 return uploadDir.listFiles(); 173 } 174 175 176 179 public void deleteFile(String weblogHandle, String name) 180 throws RollerException { 181 String dir = this.upload_dir + weblogHandle; 182 File f = new File (dir + File.separator + name); 183 f.delete(); 184 } 185 186 187 194 public void saveFile(String weblogHandle, String name, String contentType, 195 long size, InputStream is) 196 throws RollerException { 197 198 if (!canSave(weblogHandle, name, contentType, size, new RollerMessages())) { 199 throw new RollerException("ERROR: upload denied"); 200 } 201 202 byte[] buffer = new byte[8192]; 203 int bytesRead = 0; 204 String dir = this.upload_dir; 205 206 File dirPath = new File (dir + File.separator + weblogHandle); 207 if (!dirPath.exists()) { 208 dirPath.mkdirs(); 209 } 210 OutputStream bos = null; 211 try { 212 bos = new FileOutputStream ( 213 dirPath.getAbsolutePath() + File.separator + name); 214 while ((bytesRead = is.read(buffer, 0, 8192)) != -1) { 215 bos.write(buffer, 0, bytesRead); 216 } 217 } catch (Exception e) { 218 throw new RollerException("ERROR uploading file", e); 219 } finally { 220 try { 221 bos.flush(); 222 bos.close(); 223 } catch (Exception ignored) {} 224 } 225 if (mLogger.isDebugEnabled()) { 226 mLogger.debug("The file has been written to \"" + dir + weblogHandle + "\""); 227 } 228 } 229 230 231 237 private int getWebsiteDirSize(String weblogHandle, String dir) { 238 239 int userDirSize = 0; 240 File d = new File (dir + File.separator + weblogHandle); 241 if (d.mkdirs() || d.exists()) { 242 File [] files = d.listFiles(); 243 long dirSize = 0l; 244 for (int i=0; i<files.length; i++) { 245 if (!files[i].isDirectory()) { 246 dirSize = dirSize + files[i].length(); 247 } 248 } 249 userDirSize = new Long (dirSize).intValue(); 250 } 251 return userDirSize; 252 } 253 254 255 263 private boolean checkFileType(String [] allowFiles, String [] forbidFiles, 264 String fileName, String contentType) { 265 266 273 if (contentType == null || contentType.indexOf("/") == -1) { 275 return false; 276 } 277 278 boolean allowFile = false; 280 281 if(allowFiles == null || allowFiles.length < 1) { 284 allowFile = true; 285 } 286 287 289 if (allowFiles != null && allowFiles.length > 0) { 291 for (int y=0; y<allowFiles.length; y++) { 292 if (allowFiles[y].indexOf("/") != -1) continue; 294 if (fileName.toLowerCase().endsWith( 295 allowFiles[y].toLowerCase())) { 296 allowFile = true; 297 break; 298 } 299 } 300 } 301 302 if (allowFiles != null && allowFiles.length > 0) { 304 for (int y=0; y<allowFiles.length; y++) { 305 if (allowFiles[y].indexOf("/") == -1) continue; 307 if (matchContentType(allowFiles[y], contentType)) { 308 allowFile = true; 309 break; 310 } 311 } 312 } 313 314 316 if (forbidFiles != null && forbidFiles.length > 0) { 318 for (int x=0; x<forbidFiles.length; x++) { 319 if (forbidFiles[x].indexOf("/") != -1) continue; 321 if (fileName.toLowerCase().endsWith( 322 forbidFiles[x].toLowerCase())) { 323 allowFile = false; 324 break; 325 } 326 } 327 } 328 329 330 if (forbidFiles != null && forbidFiles.length > 0) { 332 for (int x=0; x<forbidFiles.length; x++) { 333 if (forbidFiles[x].indexOf("/") == -1) continue; 335 if (matchContentType(forbidFiles[x], contentType)) { 336 allowFile = false; 337 break; 338 } 339 } 340 } 341 342 return allowFile; 343 } 344 345 346 349 private boolean matchContentType(String rangeRule, String contentType) { 350 if (rangeRule.equals("*/*")) return true; 351 if (rangeRule.equals(contentType)) return true; 352 String ruleParts[] = rangeRule.split("/"); 353 String typeParts[] = contentType.split("/"); 354 if (ruleParts[0].equals(typeParts[0]) && ruleParts[1].equals("*")) 355 return true; 356 357 return false; 358 } 359 360 361 public void release() { 362 } 363 364 } 365 | Popular Tags |