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 | 0 | 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 | 0 | String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler |
72 | |
.getUserFilesAbsolutePath(ThreadLocalData.getRequest())); |
73 | 0 | File typeDir = getOrCreateResourceTypeDir(absolutePath, type); |
74 | 0 | File currentDir = new File(typeDir, currentFolder); |
75 | 0 | if (!currentDir.exists() || !currentDir.isDirectory()) |
76 | 0 | throw new InvalidCurrentFolderException(); |
77 | |
|
78 | 0 | File newFile = new File(currentDir, fileName); |
79 | 0 | File fileToSave = UtilsFile.getUniqueFile(newFile.getAbsoluteFile()); |
80 | |
|
81 | |
try { |
82 | 0 | IOUtils.copyLarge(inputStream, new FileOutputStream(fileToSave)); |
83 | 0 | } catch (IOException e) { |
84 | 0 | throw new WriteException(); |
85 | 0 | } |
86 | 0 | 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 | 0 | String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler |
94 | |
.getUserFilesAbsolutePath(ThreadLocalData.getRequest())); |
95 | 0 | File typeDir = getOrCreateResourceTypeDir(absolutePath, type); |
96 | 0 | File currentDir = new File(typeDir, currentFolder); |
97 | 0 | if (!currentDir.exists() || !currentDir.isDirectory()) |
98 | 0 | throw new InvalidCurrentFolderException(); |
99 | |
|
100 | 0 | File newDir = new File(currentDir, newFolder); |
101 | 0 | if (newDir.exists()) |
102 | 0 | throw new FolderAlreadyExistsException(); |
103 | 0 | if (!newDir.mkdir()) |
104 | 0 | throw new InvalidNewFolderNameException(); |
105 | 0 | } |
106 | |
|
107 | |
public List<Map<String, Object>> getFiles(ResourceType type, |
108 | |
String currentFolder) throws InvalidCurrentFolderException { |
109 | 0 | String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler |
110 | |
.getUserFilesAbsolutePath(ThreadLocalData.getRequest())); |
111 | 0 | File typeDir = getOrCreateResourceTypeDir(absolutePath, type); |
112 | 0 | File currentDir = new File(typeDir, currentFolder); |
113 | 0 | if (!currentDir.exists() || !currentDir.isDirectory()) |
114 | 0 | throw new InvalidCurrentFolderException(); |
115 | |
|
116 | |
|
117 | |
List<Map<String, Object>> files; |
118 | |
Map<String, Object> fileMap; |
119 | 0 | File[] fileList = currentDir |
120 | |
.listFiles((FileFilter) FileFileFilter.FILE); |
121 | 0 | files = new ArrayList<Map<String, Object>>(fileList.length); |
122 | 0 | for (File file : fileList) { |
123 | 0 | fileMap = new HashMap<String, Object>(2); |
124 | 0 | fileMap.put(Connector.KEY_NAME, file.getName()); |
125 | 0 | fileMap.put(Connector.KEY_SIZE, file.length()); |
126 | 0 | files.add(fileMap); |
127 | |
} |
128 | 0 | return files; |
129 | |
} |
130 | |
|
131 | |
public List<String> getFolders(final ResourceType type, |
132 | |
final String currentFolder) throws InvalidCurrentFolderException { |
133 | 0 | String absolutePath = getRealUserFilesAbsolutePath(RequestCycleHandler |
134 | |
.getUserFilesAbsolutePath(ThreadLocalData.getRequest())); |
135 | 0 | File typeDir = getOrCreateResourceTypeDir(absolutePath, type); |
136 | 0 | File currentDir = new File(typeDir, currentFolder); |
137 | 0 | if (!currentDir.exists() || !currentDir.isDirectory()) |
138 | 0 | throw new InvalidCurrentFolderException(); |
139 | |
|
140 | 0 | String[] fileList = currentDir.list(DirectoryFileFilter.DIRECTORY); |
141 | 0 | 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 | 0 | File dir = new File(baseDir, type.getPath()); |
171 | 0 | if (!dir.exists()) |
172 | 0 | dir.mkdirs(); |
173 | 0 | return dir; |
174 | |
} |
175 | |
} |