|
1 |
| package photospace.web.spring; |
|
2 |
| |
|
3 |
| import java.io.*; |
|
4 |
| import java.util.*; |
|
5 |
| import java.util.regex.*; |
|
6 |
| import org.apache.commons.lang.*; |
|
7 |
| import org.apache.commons.logging.*; |
|
8 |
| import org.springframework.validation.*; |
|
9 |
| import org.springframework.web.multipart.*; |
|
10 |
| import com.drew.imaging.jpeg.*; |
|
11 |
| import photospace.vfs.*; |
|
12 |
| |
|
13 |
| public class UploadValidator |
|
14 |
| implements Validator { |
|
15 |
| |
|
16 |
| private static final Log log = LogFactory.getLog(UploadValidator.class); |
|
17 |
| |
|
18 |
| private List contentTypes = Arrays.asList(new String[] { "image/jpeg", "image/pjpeg" }); |
|
19 |
| private FileSystem filesystem; |
|
20 |
| |
|
21 |
0
| public boolean supports(Class clazz)
|
|
22 |
| { |
|
23 |
0
| return UploadCommand.class.isAssignableFrom(clazz);
|
|
24 |
| } |
|
25 |
| |
|
26 |
0
| public void validate(Object obj, Errors errors)
|
|
27 |
| { |
|
28 |
0
| UploadCommand upload = (UploadCommand) obj;
|
|
29 |
| |
|
30 |
0
| File base = null;
|
|
31 |
0
| try
|
|
32 |
| { |
|
33 |
0
| base = filesystem.getDirectory(upload.getPath());
|
|
34 |
| } |
|
35 |
| catch (Exception e) |
|
36 |
| { |
|
37 |
0
| errors.rejectValue("path", "error.invalid.path",
|
|
38 |
| new Object[] { upload.getPath() }, e.getMessage()); |
|
39 |
| } |
|
40 |
| |
|
41 |
0
| if (!StringUtils.isEmpty(upload.getNewFolderName()))
|
|
42 |
| { |
|
43 |
0
| if (Pattern.matches("\\W", upload.getNewFolderName()))
|
|
44 |
| { |
|
45 |
0
| errors.rejectValue("newFolderName", "error.invalid",
|
|
46 |
| new Object[] { upload.getNewFolderName(), "folder name" }, |
|
47 |
| "Invalid folder name"); |
|
48 |
| } |
|
49 |
0
| else if (base != null && new File(base, upload.getNewFolderName()).exists())
|
|
50 |
| { |
|
51 |
0
| errors.rejectValue("newFolderName", "error.upload.exists",
|
|
52 |
| new Object[] { upload.getNewFolderName() }, |
|
53 |
| upload.getNewFolderName() + " already exists"); |
|
54 |
| } |
|
55 |
| } |
|
56 |
| |
|
57 |
0
| boolean hasUpload = false;
|
|
58 |
0
| for (int i = 0; i < upload.getFiles().length; i++)
|
|
59 |
| { |
|
60 |
0
| MultipartFile file = upload.getFiles()[i];
|
|
61 |
0
| if (file == null) continue;
|
|
62 |
0
| if (file.getSize() == 0) continue;
|
|
63 |
0
| hasUpload = true;
|
|
64 |
| |
|
65 |
| |
|
66 |
0
| if (!contentTypes.contains(file.getContentType()))
|
|
67 |
| { |
|
68 |
0
| errors.rejectValue("file" + i, "error.upload.contentType",
|
|
69 |
| new Object[] {file.getContentType()}, file.getContentType() + " must be one of " + contentTypes); |
|
70 |
0
| continue;
|
|
71 |
| } |
|
72 |
| |
|
73 |
0
| try
|
|
74 |
| { |
|
75 |
0
| JpegMetadataReader.readMetadata(file.getInputStream());
|
|
76 |
| } |
|
77 |
| catch (Exception e) |
|
78 |
| { |
|
79 |
0
| log.error("Exception parsing uploaded jpeg", e);
|
|
80 |
0
| errors.rejectValue("file" + i, "error.upload.file",
|
|
81 |
| new Object[] {file.getOriginalFilename(), e.getMessage()}, "Bad file " + file.getOriginalFilename() + ": " + e.getMessage()); |
|
82 |
| } |
|
83 |
| } |
|
84 |
| |
|
85 |
0
| if (!hasUpload)
|
|
86 |
| { |
|
87 |
0
| errors.reject("error.upload.required", "At least one file is required");
|
|
88 |
| } |
|
89 |
| } |
|
90 |
| |
|
91 |
0
| public void setFilesystem(FileSystem filesystem)
|
|
92 |
| { |
|
93 |
0
| this.filesystem = filesystem;
|
|
94 |
| } |
|
95 |
| } |