commit | author | age
|
8931b7
|
1 |
/* |
U |
2 |
Dateiverwaltung - File management in your browser |
|
3 |
Copyright (C) 2017 Ulrich Hilger, http://uhilger.de |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
|
c7c502
|
19 |
package de.uhilger.filecms.api; |
U |
20 |
|
5bfd34
|
21 |
import de.uhilger.filecms.data.FileRef; |
7342b1
|
22 |
import de.uhilger.filecms.web.Initialiser; |
8e51b7
|
23 |
import java.io.File; |
3ad4db
|
24 |
import java.io.FileNotFoundException; |
U |
25 |
import java.io.FileReader; |
e5ff42
|
26 |
import java.io.FileWriter; |
U |
27 |
import java.io.IOException; |
|
28 |
import java.security.Principal; |
7342b1
|
29 |
import java.util.ArrayList; |
5bfd34
|
30 |
import java.util.Iterator; |
7342b1
|
31 |
import java.util.List; |
e5ff42
|
32 |
import java.util.logging.Level; |
8e51b7
|
33 |
import java.util.logging.Logger; |
5bfd34
|
34 |
import org.apache.commons.io.FileUtils; |
942d63
|
35 |
import org.apache.commons.lang.StringEscapeUtils; |
c7c502
|
36 |
|
U |
37 |
/** |
|
38 |
* |
|
39 |
* @author ulrich |
|
40 |
*/ |
8931b7
|
41 |
public class FileMgr extends Api { |
8e51b7
|
42 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
43 |
|
7342b1
|
44 |
public static final String PUB_DIR_PATH = "www/"; |
U |
45 |
public static final String HOME_DIR_PATH = "home/"; |
|
46 |
public static final String PUB_DIR_NAME = "Oeffentlich"; |
|
47 |
public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; |
c7c502
|
48 |
|
9e2964
|
49 |
public static final int OP_COPY = 1; |
U |
50 |
public static final int OP_MOVE = 2; |
|
51 |
|
ea7193
|
52 |
public String hallo() { |
U |
53 |
return "Hallo Welt!"; |
|
54 |
} |
|
55 |
|
7342b1
|
56 |
public List<FileRef> list(String relPath) { |
5dfab6
|
57 |
List<FileRef> files = new ArrayList(); |
7342b1
|
58 |
if(relPath.length() == 0) { |
2121cc
|
59 |
FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
5dfab6
|
60 |
logger.finer(namedPublicFolder.getAbsolutePath()); |
2121cc
|
61 |
FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
5dfab6
|
62 |
logger.finer(namedHomeFolder.getAbsolutePath()); |
2121cc
|
63 |
files = new ArrayList(); |
7342b1
|
64 |
files.add(namedHomeFolder); |
U |
65 |
files.add(namedPublicFolder); |
ea7193
|
66 |
} else { |
2121cc
|
67 |
String path = getTargetDir(relPath).getAbsolutePath(); |
5bfd34
|
68 |
logger.fine("listing path: " + path); |
U |
69 |
File dir = new File(path); |
|
70 |
if(dir.exists()) { |
|
71 |
File[] fileArray = dir.listFiles(); |
|
72 |
for(int i = 0; i < fileArray.length; i++) { |
|
73 |
logger.fine(fileArray[i].toURI().toString()); |
|
74 |
String fname = fileArray[i].toURI().toString().replace("file:/", ""); |
|
75 |
if(fileArray[i].isDirectory()) { |
|
76 |
fname = fname.substring(0, fname.length() - 1); |
|
77 |
} |
|
78 |
logger.fine(fname); |
|
79 |
files.add(new FileRef(fname, fileArray[i].isDirectory())); |
|
80 |
} |
5dfab6
|
81 |
} |
c509a0
|
82 |
} |
7342b1
|
83 |
return files; |
2121cc
|
84 |
} |
U |
85 |
|
c509a0
|
86 |
public FileRef newFolder(String relPath, String folderName) { |
U |
87 |
logger.finer(relPath); |
|
88 |
String targetPath = null; |
|
89 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
90 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName; |
|
91 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
92 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName; |
|
93 |
} else { |
|
94 |
// kann eigentlich nicht sein.. |
|
95 |
} |
|
96 |
logger.finer(targetPath); |
|
97 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
98 |
targetDir.mkdirs(); |
|
99 |
return new FileRef(targetDir.getAbsolutePath(), true); |
5dfab6
|
100 |
} |
U |
101 |
|
3ad4db
|
102 |
public String getCode(String relPath, String fileName) { |
U |
103 |
String code = null; |
|
104 |
|
|
105 |
Object p = getRequest().getUserPrincipal(); |
|
106 |
if(p instanceof Principal) { |
|
107 |
FileReader reader = null; |
|
108 |
try { |
|
109 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
110 |
reader = new FileReader(targetFile); |
|
111 |
StringBuffer buf = new StringBuffer(); |
|
112 |
char[] readBuffer = new char[1024]; |
|
113 |
int charsRead = reader.read(readBuffer); |
|
114 |
while(charsRead > -1) { |
|
115 |
buf.append(readBuffer, 0, charsRead); |
|
116 |
charsRead = reader.read(readBuffer); |
|
117 |
} |
|
118 |
code = buf.toString(); |
|
119 |
} catch (FileNotFoundException ex) { |
|
120 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
121 |
} catch (IOException ex) { |
|
122 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
123 |
} finally { |
|
124 |
try { |
|
125 |
reader.close(); |
|
126 |
} catch (IOException ex) { |
|
127 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
} |
|
132 |
|
|
133 |
return code; |
|
134 |
} |
|
135 |
|
663ee9
|
136 |
public String renameFile(String relPath, String fname, String newName) { |
U |
137 |
File targetDir = getTargetDir(relPath); |
|
138 |
File file = new File(targetDir, fname); |
|
139 |
file.renameTo(new File(targetDir, newName)); |
|
140 |
return fname + " umbenannt zu " + newName; |
|
141 |
} |
|
142 |
|
fc1897
|
143 |
public String deleteFiles(String relPath, List fileNames) { |
U |
144 |
String result = null; |
|
145 |
try { |
|
146 |
logger.fine(fileNames.toString()); |
|
147 |
File targetDir = getTargetDir(relPath); |
|
148 |
for(int i=0; i < fileNames.size(); i++) { |
|
149 |
Object o = fileNames.get(i); |
|
150 |
if(o instanceof ArrayList) { |
|
151 |
ArrayList al = (ArrayList) o; |
|
152 |
logger.fine(al.get(0).toString()); |
|
153 |
File targetFile = new File(targetDir, al.get(0).toString()); |
|
154 |
logger.fine(targetFile.getAbsolutePath()); |
5bfd34
|
155 |
if(targetFile.isDirectory()) { |
U |
156 |
FileUtils.deleteDirectory(targetFile); |
|
157 |
} else { |
|
158 |
targetFile.delete(); |
|
159 |
} |
fc1897
|
160 |
} |
U |
161 |
} |
|
162 |
result = "deleted"; |
|
163 |
} catch (Throwable ex) { |
|
164 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
165 |
} |
|
166 |
return result; |
|
167 |
} |
|
168 |
|
9e2964
|
169 |
public String copyFiles(String fromPath, String toPath, List fileNames) { |
fab629
|
170 |
return copyOrMoveFiles(fromPath, toPath, fileNames, OP_COPY); |
9e2964
|
171 |
} |
U |
172 |
|
|
173 |
public String moveFiles(String fromPath, String toPath, List fileNames) { |
fab629
|
174 |
return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE); |
U |
175 |
} |
|
176 |
|
|
177 |
private String copyOrMoveFiles(String fromPath, String toPath, List fileNames, int operation) { |
5bfd34
|
178 |
String result = null; |
U |
179 |
try { |
|
180 |
File srcDir = getTargetDir(fromPath); |
|
181 |
File targetDir = getTargetDir(toPath); |
|
182 |
Iterator i = fileNames.iterator(); |
|
183 |
while(i.hasNext()) { |
|
184 |
Object o = i.next(); |
|
185 |
if (o instanceof ArrayList) { |
|
186 |
ArrayList al = (ArrayList) o; |
|
187 |
File srcFile = new File(srcDir, al.get(0).toString()); |
|
188 |
if(srcFile.isDirectory()) { |
fab629
|
189 |
if(operation == OP_MOVE) { |
U |
190 |
FileUtils.moveDirectoryToDirectory(srcFile, targetDir, false); |
|
191 |
} else { |
|
192 |
FileUtils.copyDirectoryToDirectory(srcFile, targetDir); |
|
193 |
} |
5bfd34
|
194 |
} else { |
fab629
|
195 |
if(operation == OP_MOVE) { |
U |
196 |
FileUtils.moveFileToDirectory(srcFile, targetDir, false); |
|
197 |
} else { |
|
198 |
FileUtils.copyFileToDirectory(srcFile, targetDir); |
|
199 |
} |
5bfd34
|
200 |
} |
U |
201 |
} |
|
202 |
} |
|
203 |
} catch (IOException ex) { |
|
204 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
205 |
} |
|
206 |
return result; |
9e2964
|
207 |
} |
U |
208 |
|
47e9d4
|
209 |
public FileRef saveTextFileAs(String relPath, String fileName, String contents) { |
U |
210 |
FileRef savedFile = null; |
|
211 |
logger.fine(relPath + " " + fileName); |
|
212 |
//FileRef datenRef = getBase(); |
|
213 |
Object p = getRequest().getUserPrincipal(); |
|
214 |
if(p instanceof Principal) { |
|
215 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
216 |
if(targetFile.exists()) { |
|
217 |
targetFile = getNewFileName(targetFile); |
|
218 |
} else { |
|
219 |
targetFile.getParentFile().mkdirs(); |
|
220 |
} |
|
221 |
saveToFile(targetFile, contents); |
|
222 |
} |
|
223 |
return savedFile; |
|
224 |
} |
|
225 |
|
|
226 |
private File getNewFileName(File file) { |
|
227 |
File dir = file.getParentFile(); |
|
228 |
String targetName = file.getName(); |
|
229 |
logger.fine("targetName: " + targetName); |
|
230 |
String ext = ""; |
|
231 |
int dotpos = targetName.indexOf("."); |
|
232 |
if(dotpos > -1) { |
|
233 |
ext = targetName.substring(dotpos); |
|
234 |
targetName = targetName.substring(0, dotpos); |
|
235 |
} |
|
236 |
logger.fine("targetName: " + targetName + ", ext: " + ext); |
|
237 |
int i = 1; |
|
238 |
while(file.exists()) { |
|
239 |
StringBuffer buf = new StringBuffer(); |
|
240 |
buf.append(targetName); |
|
241 |
buf.append("-"); |
|
242 |
buf.append(i); |
|
243 |
if(ext.length() > 0) { |
|
244 |
buf.append(ext); |
|
245 |
} |
|
246 |
file = new File(dir, buf.toString()); |
|
247 |
i++; |
|
248 |
} |
|
249 |
logger.fine("new file: " + file.getName()); |
|
250 |
return file; |
942d63
|
251 |
} |
47e9d4
|
252 |
|
U |
253 |
private FileRef saveToFile(File targetFile, String contents) { |
ea7193
|
254 |
FileRef savedFile = null; |
e5ff42
|
255 |
try { |
47e9d4
|
256 |
targetFile.createNewFile(); |
U |
257 |
FileWriter w = new FileWriter(targetFile); |
942d63
|
258 |
//w.write(StringEscapeUtils.unescapeHtml(contents)); |
47e9d4
|
259 |
w.write(contents); |
U |
260 |
w.flush(); |
|
261 |
w.close(); |
|
262 |
savedFile = new FileRef( |
|
263 |
targetFile.getAbsolutePath(), |
|
264 |
targetFile.isDirectory(), |
|
265 |
targetFile.isHidden(), |
|
266 |
targetFile.lastModified(), |
|
267 |
targetFile.length()); |
e5ff42
|
268 |
} catch (IOException ex) { |
47e9d4
|
269 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
U |
270 |
} |
|
271 |
return savedFile; |
|
272 |
} |
|
273 |
|
|
274 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
|
275 |
FileRef savedFile = null; |
|
276 |
logger.fine(relPath + " " + fileName); |
|
277 |
//FileRef datenRef = getBase(); |
|
278 |
Object p = getRequest().getUserPrincipal(); |
|
279 |
if(p instanceof Principal) { |
|
280 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
281 |
if(targetFile.exists()) { |
|
282 |
/* |
|
283 |
muss delete() sein? |
|
284 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
285 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
286 |
Inhalt? |
|
287 |
*/ |
|
288 |
targetFile.delete(); |
|
289 |
} else { |
|
290 |
targetFile.getParentFile().mkdirs(); |
|
291 |
} |
|
292 |
saveToFile(targetFile, contents); |
e5ff42
|
293 |
} |
ea7193
|
294 |
return savedFile; |
U |
295 |
} |
5efd94
|
296 |
|
U |
297 |
/* ---- Hilfsfunktionen ---- */ |
ea7193
|
298 |
|
5efd94
|
299 |
private File getTargetDir(String relPath) { |
3c4b10
|
300 |
logger.fine(relPath); |
5efd94
|
301 |
String targetPath = null; |
U |
302 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
9e2964
|
303 |
targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length()); |
5efd94
|
304 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
9e2964
|
305 |
targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length()); |
5efd94
|
306 |
} else { |
U |
307 |
// kann eigentlich nicht sein.. |
|
308 |
} |
3c4b10
|
309 |
logger.fine(targetPath); |
5efd94
|
310 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
U |
311 |
return targetDir; |
|
312 |
} |
9e2964
|
313 |
|
5efd94
|
314 |
private FileRef getBase() { |
U |
315 |
FileRef base = null; |
|
316 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
3c4b10
|
317 |
if(o instanceof String) { |
U |
318 |
String baseStr = (String) o; |
|
319 |
logger.fine(baseStr); |
|
320 |
File file = new File(baseStr); |
|
321 |
base = new FileRef(file.getAbsolutePath(), file.isDirectory()); |
5efd94
|
322 |
} |
U |
323 |
return base; |
|
324 |
} |
|
325 |
|
|
326 |
private String getUserName() { |
|
327 |
String userName = null; |
|
328 |
Object p = getRequest().getUserPrincipal(); |
|
329 |
if(p instanceof Principal) { |
|
330 |
userName = ((Principal) p).getName(); |
|
331 |
} |
|
332 |
return userName; |
547755
|
333 |
} |
U |
334 |
} |