1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor.connector.impl;
22
23 import java.io.File;
24 import java.io.FileFilter;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import javax.servlet.ServletContext;
35
36 import net.fckeditor.connector.Connector;
37 import net.fckeditor.connector.exception.FolderAlreadyExistsException;
38 import net.fckeditor.connector.exception.InvalidCurrentFolderException;
39 import net.fckeditor.connector.exception.InvalidNewFolderNameException;
40 import net.fckeditor.connector.exception.WriteException;
41 import net.fckeditor.handlers.RequestCycleHandler;
42 import net.fckeditor.handlers.ResourceType;
43 import net.fckeditor.requestcycle.ThreadLocalData;
44 import net.fckeditor.requestcycle.UserPathBuilder;
45 import net.fckeditor.tool.UtilsFile;
46
47 import org.apache.commons.io.IOUtils;
48 import org.apache.commons.io.filefilter.DirectoryFileFilter;
49 import org.apache.commons.io.filefilter.FileFileFilter;
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public abstract class AbstractLocalFileSystemConnector implements Connector {
64
65 protected ServletContext servletContext;
66
67 public String fileUpload(final ResourceType type,
68 final String currentFolder, final String fileName,
69 final InputStream inputStream)
70 throws InvalidCurrentFolderException, WriteException {
71 String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler
72 .getUserFilesAbsolutePath(ThreadLocalData.getRequest()));
73 File typeDir = getOrCreateResourceTypeDir(absolutePath, type);
74 File currentDir = new File(typeDir, currentFolder);
75 if (!currentDir.exists() || !currentDir.isDirectory())
76 throw new InvalidCurrentFolderException();
77
78 File newFile = new File(currentDir, fileName);
79 File fileToSave = UtilsFile.getUniqueFile(newFile.getAbsoluteFile());
80
81 try {
82 IOUtils.copyLarge(inputStream, new FileOutputStream(fileToSave));
83 } catch (IOException e) {
84 throw new WriteException();
85 }
86 return fileToSave.getName();
87 }
88
89 public void createFolder(final ResourceType type,
90 final String currentFolder, final String newFolder)
91 throws InvalidCurrentFolderException,
92 InvalidNewFolderNameException, FolderAlreadyExistsException {
93 String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler
94 .getUserFilesAbsolutePath(ThreadLocalData.getRequest()));
95 File typeDir = getOrCreateResourceTypeDir(absolutePath, type);
96 File currentDir = new File(typeDir, currentFolder);
97 if (!currentDir.exists() || !currentDir.isDirectory())
98 throw new InvalidCurrentFolderException();
99
100 File newDir = new File(currentDir, newFolder);
101 if (newDir.exists())
102 throw new FolderAlreadyExistsException();
103 if (!newDir.mkdir())
104 throw new InvalidNewFolderNameException();
105 }
106
107 public List<Map<String, Object>> getFiles(ResourceType type,
108 String currentFolder) throws InvalidCurrentFolderException {
109 String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler
110 .getUserFilesAbsolutePath(ThreadLocalData.getRequest()));
111 File typeDir = getOrCreateResourceTypeDir(absolutePath, type);
112 File currentDir = new File(typeDir, currentFolder);
113 if (!currentDir.exists() || !currentDir.isDirectory())
114 throw new InvalidCurrentFolderException();
115
116
117 List<Map<String, Object>> files;
118 Map<String, Object> fileMap;
119 File[] fileList = currentDir
120 .listFiles((FileFilter) FileFileFilter.FILE);
121 files = new ArrayList<Map<String, Object>>(fileList.length);
122 for (File file : fileList) {
123 fileMap = new HashMap<String, Object>(2);
124 fileMap.put(Connector.KEY_NAME, file.getName());
125 fileMap.put(Connector.KEY_SIZE, file.length());
126 files.add(fileMap);
127 }
128 return files;
129 }
130
131 public List<String> getFolders(final ResourceType type,
132 final String currentFolder) throws InvalidCurrentFolderException {
133 String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler
134 .getUserFilesAbsolutePath(ThreadLocalData.getRequest()));
135 File typeDir = getOrCreateResourceTypeDir(absolutePath, type);
136 File currentDir = new File(typeDir, currentFolder);
137 if (!currentDir.exists() || !currentDir.isDirectory())
138 throw new InvalidCurrentFolderException();
139
140 String[] fileList = currentDir.list(DirectoryFileFilter.DIRECTORY);
141 return Arrays.asList(fileList);
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156 protected abstract String getRealUserFilesAbsolutePath(String userFilesAbsolutePath);
157
158
159
160
161
162
163
164
165
166
167
168 protected static File getOrCreateResourceTypeDir(final String baseDir,
169 final ResourceType type) {
170 File dir = new File(baseDir, type.getPath());
171 if (!dir.exists())
172 dir.mkdirs();
173 return dir;
174 }
175 }