KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > modules > filemgr > FileMgrUploadObject


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5
6 package com.raptus.owxv3.modules.filemgr;
7
8 import java.io.*;
9 import java.sql.SQLException JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
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 /**
19  *
20  * <table width="100%" border="0">
21  * <tr>
22  * <td width="24%"><b>Filename</b></td><td width="76%">FileMgrUploadObject.java</td>
23  * </tr>
24  * <tr>
25  * <td width="24%"><b>Author</b></td><td width="76%">REEA</td>
26  * </tr>
27  * <tr>
28  * <td width="24%"><b>Date</b></td><td width="76%">25th of September 2001</td>
29  * </tr>
30  * </table>
31  * <hr>
32  * <table width="100%" border="0">
33  * <tr>
34  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
35  * </tr>
36  * </table>
37  * <hr>
38  * <table width="100%" border="0">
39  * <tr>
40  * <td>This class is performing the uploading of the file. <br>
41  * It provides methods for saving the file to the disk and in the database.</td>
42  * </tr>
43  * </table>
44  * <hr>
45  */

46 public class FileMgrUploadObject extends Object JavaDoc
47 //extends BusinessObject no use for this time to extend BusinessObject
48
{
49
50     /**
51      *A GlobalResources object that will br used do the database modifications
52      */

53     protected GlobalResources gres = null;
54
55     /**
56     *The repository where the files will be saved
57     */

58
59     protected String JavaDoc repository;
60
61
62     /**
63     *The alias
64     */

65
66     protected String JavaDoc alias;
67
68
69     /*
70     *the maximum size of files that can be uploaded
71     */

72
73     protected String JavaDoc maxsize;
74
75
76     /**
77     *In this hasthtable we will put all the mimetypes that
78     *the system recognizes
79     */

80
81     protected Hashtable mimetypes;
82
83
84     /**
85     *this will hold the default file type in our case "file" i.e. if no other category like "image"
86     *is recognized that this will be used
87     */

88     protected String JavaDoc defaultfiletype;
89
90     private FormFile uploadfile;
91     private EAFileMgrUploadBean uploadbean;
92
93     private String JavaDoc realfilename;
94
95
96     /**
97      *The constructor, we need a VModule object to read entries from the owx configutation file.<br>
98      *The form-bean that contains he form field values is also needed.
99      */

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         //try
111
//{
112
//repository =vm.getStringProperty(FileMgrConstants.VMODULE_PROPERTY_REPOSITORY);
113
repository=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=repository","value");
114             //alias=vm.getStringProperty(FileMgrConstants.VMODULE_PROPERTY_ALIAS);
115
alias=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=alias","value");
116             //maxsize = vm.getStringProperty(FileMgrConstants.VMODULE_PROPERTY_MAXSIZE);
117
maxsize=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=maxsize","value");
118
119             //defaultfiletype=vm.getStringProperty(FileMgrConstants.VMODULE_PROPERTY_DEFAULTFILETYPE);
120
defaultfiletype=cm.getPropertyByTree("virtualhost/vmodules/vmodule?name="+vm.getIdentification()+"/properties/property?name=file/property?name=defaulttype","value");
121             
122
123
124             //getting file types
125
mimetypes=new Hashtable();
126             //String[] filetypes=vm.getStringArrayProperty(FileMgrConstants.VMODULE_PROPERTY_FILETYPELIST);
127
String JavaDoc[] 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 JavaDoc[] mt;
130             //LoggingManager.log("Getting file mimetypes");
131
for (i=0;i<filetypes.length;i++){
132                 //LoggingManager.log("Getting file mimetype for"+filetypes[i]);
133
//mt=vm.getStringArrayProperty(filetypes[i]+Constants.DEFAULT_SPACER+FileMgrConstants.VMODULE_PROPERTY_MIMETYPE);
134
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]); //putting in hashtable values like("image/gif","image");
136
}//end for
137

138
139         //}
140
//catch(ParseException e) {
141
// LoggingManager.log("ParseException while retrieving custom vm properties!", this);
142
//}
143
}
144
145
146
147
148     /**
149      *Method for accessing the file repository
150      */

151     public String JavaDoc getFileRepository()
152     {
153         if(repository.endsWith(File.separator)) return repository;
154         return repository+File.separator;
155     }
156
157
158     /**
159      *Method for accessing the alias
160      */

161     public String JavaDoc getAlias()
162     {
163         if(alias.endsWith(FileMgrConstants.URL_SEPARATOR)) return alias;
164         return alias+FileMgrConstants.URL_SEPARATOR;
165     }
166
167     /**
168      *Method for accessing the Maximum Size
169      */

170     public String JavaDoc getMaxSize() { return maxsize; }
171
172
173
174     /**
175      *Method that checks that there is enough disk space available.<br>
176      *This method should be called before the saveToDisk() method.
177      */

178     public boolean isEnoughFreeSpace() throws SQLException JavaDoc{
179
180         //if maxsize is 0 it means no limit
181
if( Integer.parseInt(maxsize)==0 ) return true;
182         return !isMaxSizeReached(uploadfile.getFileSize());
183     }
184
185
186     /**
187      *This method is saving the file to the disk. The file will be saved
188      *in the directory specified by repository and in the filetype subdirectory.
189      *The filename remains the same if there is not another file with the same name, otherwise
190      *it will be filename_yyyymmddhhmmss.extesion
191      */

192     public void saveToDisk() throws FileNotFoundException,IOException
193     {
194
195         /**
196          *This method will set the filename
197          */

198         setRealFileName();
199
200         /**
201          *checking if the directory in wich the file should be saved exists
202          */

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     /**
220      *This method is saving the file as a GlobalResource (i.e. in the database)
221      */

222    public void saveAsGlobalResource() throws SQLException JavaDoc{
223
224        if(realfilename==null){
225            //shouldn't be null
226
setRealFileName();
227        }
228
229        /**
230         *Creating a new GResFile object
231         */

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 JavaDoc cname=uploadbean.getCategoryName();
238        /*if(uploadbean.getCategorytype()==FileMgrConstants.EXISTING_CATEGORY)
239            cname=uploadbean.getCatname();
240        else
241            cname=uploadbean.getNewcatname();*/

242
243        grfile.setCategory(cname);
244        grfile.setType( getFileType(uploadfile.getContentType()) );
245
246        if( grfile.getType().equals(GlobalResources.FILETYPE_PICTURE) )
247        {
248            /**
249             *The file is a picture we should set his width and height using a PictureProperties object.
250             */

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                /**
258                 *The picture's width and height was recognized
259                 */

260                grfile.setInfo("width=\""+w+"\" height=\""+h+"\"");
261            }
262            else{
263                /**
264                 *The picture doesn't respect a standard leave the info blank
265                 */

266                grfile.setInfo("");
267
268            }//end else
269

270        }//end if picture
271
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        /**
284         *incrementing uploadFileCount in the FileUploadBean
285         *
286         */

287        uploadbean.incUploadFileCount();
288    }
289
290
291
292
293
294     /**
295      *Method for setting the name of the new file.
296      *The filename remains the same if there is not another file with the same name, otherwise
297      *it will be filename_yyyymmddhhmmss.extesion
298      *
299      */

300     private void setRealFileName(){
301        //cheging for existing file
302
File file=new File( getFileRepository()+getFileType( uploadfile.getContentType() )+File.separator+uploadfile.getFileName() );
303         if(file.exists() && uploadbean.getOverwrite()==null)
304         {
305
306             /**
307              *the file exists and overwrite not alloved, we should append a timpestamp in the filename
308              */

309
310             String JavaDoc 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         }//end if
318
else realfilename=uploadfile.getFileName();
319
320     }
321
322
323     /**
324      *Method that will determine that if a new file upload is allowed then the
325      *maximum configured space is reached
326      */

327     private boolean isMaxSizeReached(int filesize) throws SQLException JavaDoc{
328
329
330         int usedfilespace=0;
331         usedfilespace=gres.getUsedFileSpace();
332
333
334         if( (usedfilespace+filesize)>Integer.parseInt(maxsize) ){
335             /**
336              *if we allow ulpading the currect file the maxsize will be reached
337              */

338             return true;
339         }
340
341         /**
342          *he upload can proceed we have enough disk space
343          */

344         return false;
345
346
347     }
348
349
350
351     /**
352      *Method for determimg the filetype based on the contenttype (mimetype)
353      *provided by browser
354      */

355     private String JavaDoc getFileType(String JavaDoc mimetype){
356
357         if(mimetype==null) return defaultfiletype;
358
359         if(mimetypes.containsKey(mimetype)) return (String JavaDoc) mimetypes.get(mimetype);
360         else return defaultfiletype;
361     }
362
363     /**
364      *Method for generating a timestamp like: yyyymmddhhmmss
365      */

366     private String JavaDoc getTimeStamp(){
367         SimpleDateFormat JavaDoc formatter=new SimpleDateFormat JavaDoc("yyyyMMddhhmmss");
368         return formatter.format(new Date());
369     }
370
371
372 }
373 // eof
374
Popular Tags