1 5 6 package com.raptus.owxv3.modules.filemgr; 7 8 import java.io.*; 9 import java.sql.SQLException ; 10 import java.text.SimpleDateFormat ; 11 import java.util.*; 12 13 import org.apache.struts.upload.FormFile; 14 15 import com.raptus.owxv3.*; 16 import com.raptus.owxv3.api.*; 17 18 46 public class FileMgrUploadObject extends Object 47 { 49 50 53 protected GlobalResources gres = null; 54 55 58 59 protected String repository; 60 61 62 65 66 protected String alias; 67 68 69 72 73 protected String maxsize; 74 75 76 80 81 protected Hashtable mimetypes; 82 83 84 88 protected String defaultfiletype; 89 90 private FormFile uploadfile; 91 private EAFileMgrUploadBean uploadbean; 92 93 private String realfilename; 94 95 96 100 101 public FileMgrUploadObject(VModule vm, 102 EAFileMgrUploadBean bean 103 ) 104 { 105 106 gres = new GlobalResources(); 107 uploadbean=bean; 108 uploadfile=uploadbean.getUploadfile(); 109 XMLConfigManager cm = XMLConfigManager.getInstance(); 110 repository=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=repository","value"); 114 alias=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=alias","value"); 116 maxsize=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=maxsize","value"); 118 119 defaultfiletype=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=defaulttype","value"); 121 122 123 124 mimetypes=new Hashtable(); 126 String [] filetypes=cm.getStringArrayByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=type","values"); 128 int i=0,j=0; 129 String [] mt; 130 for (i=0;i<filetypes.length;i++){ 132 mt = cm.getStringArrayByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=type/property?name="+filetypes[i]+"/property?name=mimetypes","value"); 135 for(j=0;j<mt.length;j++) mimetypes.put(mt[j],filetypes[i]); } 138 139 } 144 145 146 147 148 151 public String getFileRepository() 152 { 153 if(repository.endsWith(File.separator)) return repository; 154 return repository+File.separator; 155 } 156 157 158 161 public String getAlias() 162 { 163 if(alias.endsWith(FileMgrConstants.URL_SEPARATOR)) return alias; 164 return alias+FileMgrConstants.URL_SEPARATOR; 165 } 166 167 170 public String getMaxSize() { return maxsize; } 171 172 173 174 178 public boolean isEnoughFreeSpace() throws SQLException { 179 180 if( Integer.parseInt(maxsize)==0 ) return true; 182 return !isMaxSizeReached(uploadfile.getFileSize()); 183 } 184 185 186 192 public void saveToDisk() throws FileNotFoundException,IOException 193 { 194 195 198 setRealFileName(); 199 200 203 204 File file=new File( getFileRepository()+getFileType( uploadfile.getContentType() ) ); 205 if(! file.exists()) file.mkdir(); 206 207 InputStream stream=uploadfile.getInputStream(); 208 OutputStream bos = new FileOutputStream(getFileRepository()+getFileType( uploadfile.getContentType() )+File.separator+realfilename); 209 int bytesRead = 0; 210 byte[] buffer = new byte[8192]; 211 while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) 212 { 213 bos.write(buffer, 0, bytesRead); 214 } 215 bos.close(); 216 } 217 218 219 222 public void saveAsGlobalResource() throws SQLException { 223 224 if(realfilename==null){ 225 setRealFileName(); 227 } 228 229 232 GResFile grfile=new GResFile(); 233 grfile.setUsageCount(0); 234 grfile.setFileSize(uploadfile.getFileSize()); 235 grfile.setFileURL(getAlias()+getFileType(uploadfile.getContentType())+FileMgrConstants.URL_SEPARATOR+realfilename); 236 237 String cname=uploadbean.getCategoryName(); 238 242 243 grfile.setCategory(cname); 244 grfile.setType( getFileType(uploadfile.getContentType()) ); 245 246 if( grfile.getType().equals(GlobalResources.FILETYPE_PICTURE) ) 247 { 248 251 252 PictureProperties picprops= new PictureProperties( getFileRepository()+getFileType( uploadfile.getContentType() )+File.separator+realfilename); 253 int w=picprops.getWidth(); 254 int h=picprops.getHeight(); 255 if(w!=0 && h!=0) 256 { 257 260 grfile.setInfo("width=\""+w+"\" height=\""+h+"\""); 261 } 262 else{ 263 266 grfile.setInfo(""); 267 268 } 270 } else grfile.setInfo(""); 272 273 if(uploadbean.getOverwrite()!=null) 274 { 275 gres.saveFileOverwrite(grfile); 276 } 277 else 278 { 279 gres.saveFile(grfile); 280 } 281 282 283 287 uploadbean.incUploadFileCount(); 288 } 289 290 291 292 293 294 300 private void setRealFileName(){ 301 File file=new File( getFileRepository()+getFileType( uploadfile.getContentType() )+File.separator+uploadfile.getFileName() ); 303 if(file.exists() && uploadbean.getOverwrite()==null) 304 { 305 306 309 310 String s=uploadfile.getFileName(); 311 if(s.indexOf(".")==-1) realfilename= s+"_"+getTimeStamp(); 312 else{ 313 int i=s.indexOf("."); 314 realfilename=s.substring(0,i)+"_"+getTimeStamp()+s.substring(i); 315 316 } 317 } else realfilename=uploadfile.getFileName(); 319 320 } 321 322 323 327 private boolean isMaxSizeReached(int filesize) throws SQLException { 328 329 330 int usedfilespace=0; 331 usedfilespace=gres.getUsedFileSpace(); 332 333 334 if( (usedfilespace+filesize)>Integer.parseInt(maxsize) ){ 335 338 return true; 339 } 340 341 344 return false; 345 346 347 } 348 349 350 351 355 private String getFileType(String mimetype){ 356 357 if(mimetype==null) return defaultfiletype; 358 359 if(mimetypes.containsKey(mimetype)) return (String ) mimetypes.get(mimetype); 360 else return defaultfiletype; 361 } 362 363 366 private String getTimeStamp(){ 367 SimpleDateFormat formatter=new SimpleDateFormat ("yyyyMMddhhmmss"); 368 return formatter.format(new Date()); 369 } 370 371 372 } 373 | Popular Tags |