1 package org.roller.business; 2 3 import java.io.File ; 4 import java.io.FileOutputStream ; 5 import java.io.InputStream ; 6 import java.io.OutputStream ; 7 import java.math.BigDecimal ; 8 import java.util.Map ; 9 import org.apache.commons.lang.StringUtils; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.roller.RollerException; 14 import org.roller.config.RollerConfig; 15 import org.roller.model.FileManager; 16 import org.roller.model.Roller; 17 import org.roller.model.RollerFactory; 18 import org.roller.pojos.RollerPropertyData; 19 import org.roller.pojos.WebsiteData; 20 import org.roller.util.RollerMessages; 21 22 29 public class FileManagerImpl implements FileManager 30 { 31 private String upload_dir = null; 32 private String upload_url = null; 33 34 private static Log mLogger = 35 LogFactory.getFactory().getInstance(FileManagerImpl.class); 36 37 38 43 public FileManagerImpl() 44 { 45 String uploaddir = RollerConfig.getProperty("uploads.dir"); 46 String uploadurl = RollerConfig.getProperty("uploads.url"); 47 48 if(uploaddir == null || uploaddir.trim().length() < 1) 49 uploaddir = "${user.home}"+File.separator+"roller_data"+File.separator+"uploads"; 50 51 if(uploaddir.startsWith("${user.home}")) 52 uploaddir = System.getProperty("user.home") + uploaddir.substring(12); 53 54 if( ! uploaddir.endsWith(File.separator)) 55 uploaddir += File.separator; 56 57 if(uploadurl == null || uploadurl.trim().length() < 1) 58 uploadurl = File.separator+"resources"; 59 60 this.upload_dir = uploaddir.replace('/',File.separatorChar); 61 this.upload_url = uploadurl; 62 } 63 64 65 68 public String getUploadDir() { 69 return this.upload_dir; 70 } 71 72 75 public String getUploadUrl() { 76 return this.upload_url; 77 } 78 79 80 83 public boolean canSave( 84 WebsiteData site, String name, long size, RollerMessages messages) 85 throws RollerException 86 { 87 Roller mRoller = RollerFactory.getRoller(); 88 Map config = mRoller.getPropertiesManager().getProperties(); 89 90 if (!((RollerPropertyData)config.get("uploads.enabled")).getValue().equalsIgnoreCase("true")) { 91 messages.addError("error.upload.disabled"); 92 return false; 93 } 94 95 String allows = ((RollerPropertyData)config.get("uploads.types.allowed")).getValue(); 96 String forbids = ((RollerPropertyData)config.get("uploads.types.forbid")).getValue(); 97 String [] allowFiles = StringUtils.split(StringUtils.deleteWhitespace(allows), ","); 98 String [] forbidFiles = StringUtils.split(StringUtils.deleteWhitespace(forbids), ","); 99 if (!checkFileType(allowFiles, forbidFiles, name)) { 100 messages.addError("error.upload.forbiddenFile", allows); 101 return false; 102 } 103 104 BigDecimal maxDirMB = new BigDecimal ( 105 ((RollerPropertyData)config.get("uploads.dir.maxsize")).getValue()); 106 int maxDirBytes = (int)(1024000 * maxDirMB.doubleValue()); 107 int userDirSize = getUserDirSize(site.getUser().getUserName(), this.upload_dir); 108 if (userDirSize + size > maxDirBytes) { 109 messages.addError("error.upload.dirmax", maxDirMB.toString()); 110 return false; 111 } 112 113 BigDecimal maxFileMB = new BigDecimal ( 114 ((RollerPropertyData)config.get("uploads.file.maxsize")).getValue()); 115 int maxFileBytes = (int)(1024000 * maxFileMB.doubleValue()); 116 mLogger.debug(""+maxFileBytes); 117 mLogger.debug(""+size); 118 if (size > maxFileBytes) { 119 messages.addError("error.upload.filemax", maxFileMB.toString()); 120 return false; 121 } 122 123 return true; 124 } 125 126 131 public File [] getFiles(WebsiteData site) throws RollerException 132 { 133 String dir = this.upload_dir + site.getUser().getUserName(); 134 File uploadDir = new File (dir); 135 return uploadDir.listFiles(); 136 } 137 138 141 public void deleteFile(WebsiteData site, String name) 142 throws RollerException 143 { 144 String dir = this.upload_dir + site.getUser().getUserName(); 145 File f = new File (dir + File.separator + name); 146 f.delete(); 147 } 148 149 156 public void saveFile(WebsiteData site, String name, long size, InputStream is) 157 throws RollerException 158 { 159 if (!canSave(site, name, size, new RollerMessages())) 160 { 161 throw new RollerException("ERROR: upload denied"); 162 } 163 164 byte[] buffer = new byte[8192]; 165 int bytesRead = 0; 166 String dir = this.upload_dir; 167 String userName = site.getUser().getUserName(); 168 169 File dirPath = new File (dir + File.separator + userName); 170 if (!dirPath.exists()) 171 { 172 dirPath.mkdirs(); 173 } 174 OutputStream bos = null; 175 try 176 { 177 bos = new FileOutputStream ( 178 dirPath.getAbsolutePath() + File.separator + name); 179 while ((bytesRead = is.read(buffer, 0, 8192)) != -1) 180 { 181 bos.write(buffer, 0, bytesRead); 182 } 183 } 184 catch (Exception e) 185 { 186 throw new RollerException("ERROR uploading file", e); 187 } 188 finally 189 { 190 try 191 { 192 bos.flush(); 193 bos.close(); 194 } 195 catch (Exception ignored) {} 196 } 197 if (mLogger.isDebugEnabled()) 198 { 199 mLogger.debug("The file has been written to \"" + dir + userName + "\""); 200 } 201 } 202 203 208 229 230 236 private int getUserDirSize(String username, String dir) 237 { 238 int userDirSize = 0; 239 File d = new File (dir + File.separator + username); 240 if (d.mkdirs() || d.exists()) 241 { 242 File [] files = d.listFiles(); 243 long dirSize = 0l; 244 for (int i=0; i<files.length; i++) 245 { 246 if (!files[i].isDirectory()) 247 { 248 dirSize = dirSize + files[i].length(); 249 } 250 } 251 userDirSize = new Long (dirSize).intValue(); 252 } 253 return userDirSize; 254 } 255 256 264 private boolean checkFileType( 265 String [] allowFiles, String [] forbidFiles, String fileName) 266 { 267 boolean allowFile = false; 269 270 if(allowFiles == null || allowFiles.length < 1) 273 allowFile = true; 274 275 if (allowFiles != null && allowFiles.length > 0) 277 { 278 for (int y=0; y<allowFiles.length; y++) 279 { 280 if (fileName.toLowerCase().endsWith( 281 allowFiles[y].toLowerCase())) 282 { 283 allowFile = true; 284 break; 285 } 286 } 287 } 288 289 if (forbidFiles != null && forbidFiles.length > 0) 291 { 292 for (int x=0; x<forbidFiles.length; x++) 293 { 294 if (fileName.toLowerCase().endsWith( 295 forbidFiles[x].toLowerCase())) 296 { 297 allowFile = false; 298 break; 299 } 300 } 301 } 302 303 return allowFile; 304 } 305 306 309 public void release() 310 { 311 313 } 314 } 315 | Popular Tags |