1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package wjhk.jupload2.filedata;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.security.MessageDigest;
30 import java.security.NoSuchAlgorithmException;
31 import java.text.SimpleDateFormat;
32 import java.util.Date;
33 import java.util.List;
34
35 import javax.swing.tree.TreePath;
36
37 import wjhk.jupload2.exception.JUploadException;
38 import wjhk.jupload2.exception.JUploadExceptionTooBigFile;
39 import wjhk.jupload2.exception.JUploadIOException;
40 import wjhk.jupload2.gui.filepanel.treeview.TreeFileDataNode;
41 import wjhk.jupload2.policies.DefaultUploadPolicy;
42 import wjhk.jupload2.policies.UploadPolicy;
43 import wjhk.jupload2.upload.helper.ByteArrayEncoder;
44
45
46
47
48
49
50
51
52
53
54
55
56 public class DefaultFileData implements FileData {
57
58
59 UploadPolicy uploadPolicy;
60
61
62
63
64
65
66 boolean preparedForUpload = false;
67
68 private final static int BUFLEN = 4096;
69
70
71
72
73
74
75
76
77
78 protected String mimeType = "application/octet-stream";
79
80
81
82
83
84
85
86 TreeFileDataNode treeFileDataNode = null;
87
88
89
90
91 protected File file;
92
93
94
95
96 protected long fileSize;
97
98
99
100
101 protected String fileDir;
102
103
104
105
106
107 protected String fileRoot = null;
108
109
110
111
112 protected Date fileModified;
113
114
115
116
117 protected boolean uploadFlag = true;
118
119
120
121
122
123 protected String md5sum = null;
124
125
126
127
128 protected Boolean canRead = null;
129
130
131
132
133
134
135
136
137 public DefaultFileData(File file, UploadPolicy uploadPolicyParam) {
138 if (file.isDirectory()) {
139 throw new IllegalArgumentException(
140 "Internal Error: DefaultFileData can't be created from a Directory. It needs a file");
141 }
142 uploadPolicyParam.displayDebug("Creation of the DefaultFileData for " + file.getAbsolutePath(), 10);
143 this.file = file;
144 uploadPolicy = uploadPolicyParam;
145 this.fileSize = this.file.length();
146 this.fileDir = this.file.getAbsoluteFile().getParent();
147 this.fileModified = new Date(this.file.lastModified());
148 this.mimeType = uploadPolicy.getContext().getMimeType(getFileExtension());
149 }
150
151
152 public void appendFileProperties(ByteArrayEncoder bae, int index) throws JUploadIOException {
153 bae.appendTextProperty("mimetype", getMimeType(), index);
154 bae.appendTextProperty("pathinfo", getDirectory(), index);
155 bae.appendTextProperty("relpathinfo", getRelativeDir(), index);
156
157 SimpleDateFormat dateformat = new SimpleDateFormat(uploadPolicy.getDateFormat());
158 String uploadFileModificationDate = dateformat.format(getLastModified());
159 bae.appendTextProperty("filemodificationdate", uploadFileModificationDate, index);
160 }
161
162
163 public synchronized void beforeUpload(String uploadFileRoot) throws JUploadException {
164 if (this.preparedForUpload) {
165
166
167 uploadPolicy.displayWarn("The file " + getFileName() + " is already prepared for upload");
168 } else {
169
170 this.preparedForUpload = true;
171
172 this.fileRoot = uploadFileRoot;
173
174
175 if (uploadPolicy.getSendMD5Sum()) {
176 calculateMD5Sum();
177 }
178
179
180
181 if (getUploadLength() > uploadPolicy.getMaxFileSize()) {
182 throw new JUploadExceptionTooBigFile(getFileName(), getUploadLength(), uploadPolicy);
183 }
184 }
185 }
186
187
188 public long getUploadLength() {
189 if (!this.preparedForUpload) {
190 throw new IllegalStateException("The file " + getFileName() + " is not prepared for upload");
191 }
192 return this.fileSize;
193 }
194
195
196 public synchronized void afterUpload() {
197 if (!this.preparedForUpload) {
198 throw new IllegalStateException("The file " + getFileName() + " is not prepared for upload");
199 }
200
201 this.md5sum = null;
202
203
204 this.preparedForUpload = false;
205 }
206
207
208 public synchronized InputStream getInputStream() throws JUploadException {
209 if (!this.preparedForUpload) {
210 throw new IllegalStateException("The file " + getFileName() + " is not prepared for upload");
211 }
212
213 try {
214 return new FileInputStream(this.file);
215 } catch (FileNotFoundException e) {
216 throw new JUploadIOException(e);
217 }
218 }
219
220
221 public String getFileName() {
222 return this.file.getName();
223 }
224
225
226 public String getFileExtension() {
227 return getExtension(getFileName());
228 }
229
230
231 public long getFileLength() {
232 return this.fileSize;
233 }
234
235
236 public Date getLastModified() {
237 return this.fileModified;
238 }
239
240
241 public String getDirectory() {
242 return this.fileDir;
243 }
244
245
246 public boolean getUploadFlag() {
247 return this.uploadFlag;
248 }
249
250
251 public void setUploadFlag(boolean uploadFlag) {
252 this.uploadFlag = uploadFlag;
253 }
254
255
256 public String getMD5() throws JUploadException {
257 if (this.md5sum == null) {
258 throw new JUploadException("The MD5Sum has not been calculated!");
259 }
260 return this.md5sum;
261 }
262
263
264
265
266
267
268
269 public void calculateMD5Sum() throws JUploadException {
270 StringBuffer ret = new StringBuffer();
271 MessageDigest digest = null;
272 byte md5Buffer[] = new byte[BUFLEN];
273 int nbBytes;
274
275
276
277
278
279 InputStream md5InputStream = getInputStream();
280 try {
281 digest = MessageDigest.getInstance("MD5");
282 while ((nbBytes = md5InputStream.read(md5Buffer, 0, BUFLEN)) > 0) {
283 digest.update(md5Buffer, 0, nbBytes);
284 }
285 } catch (IOException e) {
286 throw new JUploadIOException(e);
287 } catch (NoSuchAlgorithmException e) {
288 throw new JUploadException(e);
289 } finally {
290 try {
291 md5InputStream.close();
292 } catch (IOException e) {
293 throw new JUploadIOException(e);
294 }
295 }
296
297
298 byte md5sum[] = new byte[32];
299 if (digest != null)
300 md5sum = digest.digest();
301 for (int i = 0; i < md5sum.length; i++) {
302 ret.append(Integer.toHexString((md5sum[i] >> 4) & 0x0f));
303 ret.append(Integer.toHexString(md5sum[i] & 0x0f));
304 }
305
306 this.md5sum = ret.toString();
307 }
308
309
310 public String getMimeType() {
311 return this.mimeType;
312 }
313
314
315 public boolean canRead() {
316
317
318
319
320
321
322
323
324 if (this.canRead == null) {
325 try {
326 InputStream is = new FileInputStream(this.file);
327 is.close();
328 this.canRead = Boolean.valueOf(true);
329 } catch (IOException e) {
330
331 this.canRead = Boolean.valueOf(false);
332 }
333 }
334
335 return this.canRead.booleanValue();
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349 protected File getFile() {
350 return this.file;
351 }
352
353
354 public String getRelativeDir() {
355 String ret = "";
356 switch (this.uploadPolicy.getFileListViewMode()) {
357 case FLAT:
358 case TREE_VIEW:
359 if (null == this.fileRoot || this.fileRoot.equals("")) {
360
361 ret = this.fileDir;
362 } else if (this.fileDir.startsWith(this.fileRoot)) {
363 int skip = this.fileRoot.length();
364 if (this.fileRoot.endsWith(File.separator))
365 skip++;
366 if ((skip >= 0) && (skip < this.fileDir.length()))
367 ret = this.fileDir.substring(skip);
368 } else {
369
370 throw new IllegalStateException("Root (" + this.fileRoot + ") is not part of current path ("
371 + this.fileDir + ")");
372 }
373 break;
374 case INDEPENDENT_TREE_VIEW:
375 TreePath treePath = treeFileDataNode.getTreePath();
376
377 for (int i = 0; i < treePath.getPathCount() - 1; i += 1) {
378 String nodeName = ((TreeFileDataNode) treePath.getPathComponent(i)).toString();
379 if (!nodeName.equals("")) {
380 ret += (ret.equals("") ? "" : "/") + nodeName;
381 }
382 }
383 }
384
385 return ret;
386 }
387
388
389 public String getAbsolutePath() {
390 return file.getAbsolutePath();
391 }
392
393
394
395
396
397
398
399
400
401
402
403 public static String getExtension(String filename) {
404 return filename.substring(filename.lastIndexOf('.') + 1);
405 }
406
407
408
409
410
411
412
413
414 public static File getRoot(List<? extends FileData> rows) {
415
416
417 File root = ((DefaultFileData) rows.get(0)).file;
418 if (root.isDirectory()) {
419 root = root.getParentFile();
420 }
421
422 while (root != null && !root.isDirectory()) {
423
424 root = root.getParentFile();
425 }
426
427 if (root != null) {
428
429
430
431 String pathRoot = root.getAbsolutePath() + File.separator;
432 File pathCurrentFileParent;
433
434
435
436 for (int i = 1; i < rows.size() && root != null; i += 1) {
437
438 if (rows.get(i).getUploadFlag()) {
439
440 pathCurrentFileParent = ((DefaultFileData) rows.get(i)).file;
441 String pathCurrentFileParentPath = pathCurrentFileParent.getAbsolutePath() + File.separator;
442
443
444
445 do {
446 pathCurrentFileParent = pathCurrentFileParent.getParentFile();
447 pathCurrentFileParentPath = (pathCurrentFileParent == null) ? "" : pathCurrentFileParent
448 .getAbsolutePath() + File.separator;
449 } while (pathCurrentFileParent != null && !pathRoot.startsWith(pathCurrentFileParentPath));
450
451
452
453 root = pathCurrentFileParent;
454 pathRoot = pathCurrentFileParentPath;
455 }
456 }
457
458
459 if (pathRoot.equals("")) {
460
461 root = null;
462 } else {
463 root = new File(pathRoot);
464 }
465 }
466
467 return root;
468 }
469
470
471 public boolean isPreparedForUpload() {
472 return this.preparedForUpload;
473 }
474
475
476
477
478 public TreeFileDataNode getTreeFileDataNode() {
479 return treeFileDataNode;
480 }
481
482
483 public void setTreeFileDataNode(TreeFileDataNode treeFileDataNode) {
484 this.treeFileDataNode = treeFileDataNode;
485 }
486
487 }