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; |
5bfd34
|
23 |
//import de.uhilger.filesystem.FileRef; |
U |
24 |
//import de.uhilger.filesystem.FileSystem; |
|
25 |
//import de.uhilger.filesystem.LocalFileSystem; |
8e51b7
|
26 |
import java.io.File; |
3ad4db
|
27 |
import java.io.FileNotFoundException; |
U |
28 |
import java.io.FileReader; |
e5ff42
|
29 |
import java.io.FileWriter; |
U |
30 |
import java.io.IOException; |
|
31 |
import java.security.Principal; |
7342b1
|
32 |
import java.util.ArrayList; |
5bfd34
|
33 |
import java.util.Iterator; |
7342b1
|
34 |
import java.util.List; |
e5ff42
|
35 |
import java.util.logging.Level; |
8e51b7
|
36 |
import java.util.logging.Logger; |
5bfd34
|
37 |
import org.apache.commons.io.FileUtils; |
c7c502
|
38 |
|
U |
39 |
/** |
|
40 |
* |
|
41 |
* @author ulrich |
|
42 |
*/ |
8931b7
|
43 |
public class FileMgr extends Api { |
8e51b7
|
44 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
45 |
|
7342b1
|
46 |
public static final String PUB_DIR_PATH = "www/"; |
U |
47 |
public static final String HOME_DIR_PATH = "home/"; |
|
48 |
public static final String PUB_DIR_NAME = "Oeffentlich"; |
|
49 |
public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; |
c7c502
|
50 |
|
9e2964
|
51 |
public static final int OP_COPY = 1; |
U |
52 |
public static final int OP_MOVE = 2; |
|
53 |
|
ea7193
|
54 |
public String hallo() { |
U |
55 |
return "Hallo Welt!"; |
|
56 |
} |
|
57 |
|
7342b1
|
58 |
public List<FileRef> list(String relPath) { |
5dfab6
|
59 |
List<FileRef> files = new ArrayList(); |
7342b1
|
60 |
if(relPath.length() == 0) { |
2121cc
|
61 |
FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
5dfab6
|
62 |
logger.finer(namedPublicFolder.getAbsolutePath()); |
2121cc
|
63 |
FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
5dfab6
|
64 |
logger.finer(namedHomeFolder.getAbsolutePath()); |
2121cc
|
65 |
files = new ArrayList(); |
7342b1
|
66 |
files.add(namedHomeFolder); |
U |
67 |
files.add(namedPublicFolder); |
ea7193
|
68 |
} else { |
2121cc
|
69 |
String path = getTargetDir(relPath).getAbsolutePath(); |
5bfd34
|
70 |
logger.fine("listing path: " + path); |
U |
71 |
//LocalFileSystem fs = new LocalFileSystem(); |
|
72 |
//logger.fine("listing " + getTargetDir(relPath).getAbsolutePath()); |
|
73 |
File dir = new File(path); |
|
74 |
if(dir.exists()) { |
|
75 |
File[] fileArray = dir.listFiles(); |
|
76 |
|
|
77 |
//FileRef[] fileRefs = fs.list(new FileRef(getTargetDir(relPath).getAbsolutePath(), true)); |
|
78 |
for(int i = 0; i < fileArray.length; i++) { |
|
79 |
logger.fine(fileArray[i].toURI().toString()); |
|
80 |
String fname = fileArray[i].toURI().toString().replace("file:/", ""); |
|
81 |
if(fileArray[i].isDirectory()) { |
|
82 |
fname = fname.substring(0, fname.length() - 1); |
|
83 |
} |
|
84 |
logger.fine(fname); |
|
85 |
files.add(new FileRef(fname, fileArray[i].isDirectory())); |
|
86 |
} |
5dfab6
|
87 |
} |
c509a0
|
88 |
} |
7342b1
|
89 |
return files; |
2121cc
|
90 |
} |
U |
91 |
|
c509a0
|
92 |
public FileRef newFolder(String relPath, String folderName) { |
U |
93 |
logger.finer(relPath); |
|
94 |
String targetPath = null; |
|
95 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
96 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName; |
|
97 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
98 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName; |
|
99 |
} else { |
|
100 |
// kann eigentlich nicht sein.. |
|
101 |
} |
|
102 |
logger.finer(targetPath); |
|
103 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
104 |
targetDir.mkdirs(); |
|
105 |
return new FileRef(targetDir.getAbsolutePath(), true); |
5dfab6
|
106 |
} |
U |
107 |
|
3ad4db
|
108 |
public String getCode(String relPath, String fileName) { |
U |
109 |
String code = null; |
|
110 |
|
|
111 |
Object p = getRequest().getUserPrincipal(); |
|
112 |
if(p instanceof Principal) { |
|
113 |
FileReader reader = null; |
|
114 |
try { |
|
115 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
116 |
reader = new FileReader(targetFile); |
|
117 |
StringBuffer buf = new StringBuffer(); |
|
118 |
char[] readBuffer = new char[1024]; |
|
119 |
int charsRead = reader.read(readBuffer); |
|
120 |
while(charsRead > -1) { |
|
121 |
buf.append(readBuffer, 0, charsRead); |
|
122 |
charsRead = reader.read(readBuffer); |
|
123 |
} |
|
124 |
code = buf.toString(); |
|
125 |
} catch (FileNotFoundException ex) { |
|
126 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
127 |
} catch (IOException ex) { |
|
128 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
129 |
} finally { |
|
130 |
try { |
|
131 |
reader.close(); |
|
132 |
} catch (IOException ex) { |
|
133 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
} |
|
138 |
|
|
139 |
return code; |
|
140 |
} |
|
141 |
|
fc1897
|
142 |
public String deleteFiles(String relPath, List fileNames) { |
U |
143 |
String result = null; |
|
144 |
try { |
|
145 |
logger.fine(fileNames.toString()); |
|
146 |
File targetDir = getTargetDir(relPath); |
|
147 |
for(int i=0; i < fileNames.size(); i++) { |
|
148 |
Object o = fileNames.get(i); |
|
149 |
if(o instanceof ArrayList) { |
|
150 |
ArrayList al = (ArrayList) o; |
|
151 |
logger.fine(al.get(0).toString()); |
|
152 |
File targetFile = new File(targetDir, al.get(0).toString()); |
|
153 |
logger.fine(targetFile.getAbsolutePath()); |
5bfd34
|
154 |
if(targetFile.isDirectory()) { |
U |
155 |
FileUtils.deleteDirectory(targetFile); |
|
156 |
} else { |
|
157 |
targetFile.delete(); |
|
158 |
} |
fc1897
|
159 |
} |
U |
160 |
} |
|
161 |
result = "deleted"; |
|
162 |
} catch (Throwable ex) { |
|
163 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
164 |
} |
|
165 |
return result; |
|
166 |
} |
|
167 |
|
9e2964
|
168 |
public String copyFiles(String fromPath, String toPath, List fileNames) { |
5bfd34
|
169 |
String result = null; |
U |
170 |
try { |
|
171 |
File srcDir = getTargetDir(fromPath); |
|
172 |
File targetDir = getTargetDir(toPath); |
|
173 |
Iterator i = fileNames.iterator(); |
|
174 |
while(i.hasNext()) { |
|
175 |
Object o = i.next(); |
|
176 |
if (o instanceof ArrayList) { |
|
177 |
ArrayList al = (ArrayList) o; |
|
178 |
File srcFile = new File(srcDir, al.get(0).toString()); |
|
179 |
if(srcFile.isDirectory()) { |
|
180 |
logger.fine("copy dir " + srcFile.getAbsolutePath() + " to dir " + targetDir.getAbsolutePath()); |
|
181 |
FileUtils.copyDirectoryToDirectory(srcFile, targetDir); |
|
182 |
} else { |
|
183 |
logger.fine("copy srcFile " + srcFile.getAbsolutePath() + " to dir " + targetDir.getAbsolutePath()); |
|
184 |
FileUtils.copyFileToDirectory(srcFile, targetDir); |
|
185 |
} |
|
186 |
} |
|
187 |
} |
|
188 |
} catch (IOException ex) { |
|
189 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
190 |
} |
|
191 |
return result; |
9e2964
|
192 |
} |
U |
193 |
|
|
194 |
public String moveFiles(String fromPath, String toPath, List fileNames) { |
5bfd34
|
195 |
String result = null; |
U |
196 |
try { |
|
197 |
File srcDir = getTargetDir(fromPath); |
|
198 |
File targetDir = getTargetDir(toPath); |
|
199 |
Iterator i = fileNames.iterator(); |
|
200 |
while(i.hasNext()) { |
|
201 |
Object o = i.next(); |
|
202 |
if (o instanceof ArrayList) { |
|
203 |
ArrayList al = (ArrayList) o; |
|
204 |
File srcFile = new File(srcDir, al.get(0).toString()); |
|
205 |
if(srcFile.isDirectory()) { |
|
206 |
FileUtils.moveDirectoryToDirectory(srcFile, targetDir, false); |
|
207 |
} else { |
|
208 |
FileUtils.moveFileToDirectory(srcFile, targetDir, false); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
} catch (IOException ex) { |
|
213 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
214 |
} |
|
215 |
return result; |
|
216 |
//return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE); |
9e2964
|
217 |
} |
U |
218 |
|
|
219 |
/** |
|
220 |
* Dateien kopieren |
|
221 |
* |
|
222 |
* @param fromPath der relative Pfad, aus dem die Dateien stammen, die kopiert werden sollen |
|
223 |
* @param toPath der relative Pfad, an den die Dateien kopiert werden sollen |
|
224 |
* @param fileNames Liste mit Namen der Dateien, die kopiert werden sollen |
|
225 |
* @param operation OP_COPY oder OP_MOVE |
|
226 |
* @return null bei Fehler oder Quittungstext, wenn erfolgreich |
|
227 |
*/ |
5bfd34
|
228 |
/* |
9e2964
|
229 |
private String copyOrMoveFiles(String fromPath, String toPath, List fileNames, int operation) { |
U |
230 |
String result = null; |
|
231 |
try { |
|
232 |
FileRef[] files = new FileRef[fileNames.size()]; |
|
233 |
logger.fine(fileNames.toString()); |
|
234 |
File srcDir = getTargetDir(fromPath); |
|
235 |
for(int i=0; i < fileNames.size(); i++) { |
|
236 |
Object o = fileNames.get(i); |
|
237 |
if(o instanceof ArrayList) { |
|
238 |
ArrayList al = (ArrayList) o; |
|
239 |
logger.fine(al.get(0).toString()); |
|
240 |
File srcFile = new File(srcDir, al.get(0).toString()); |
|
241 |
logger.fine(srcFile.getAbsolutePath()); |
|
242 |
files[i] = new FileRef(srcFile.getAbsolutePath(), srcFile.isDirectory()); |
|
243 |
} |
|
244 |
} |
|
245 |
File targetDir = getTargetDir(toPath); |
|
246 |
FileSystem fs = new LocalFileSystem(); |
|
247 |
switch(operation) { |
|
248 |
case OP_COPY: |
|
249 |
fs.copy(files, new FileRef(targetDir.getAbsolutePath(), true)); |
5bfd34
|
250 |
FileUtils.copy |
9e2964
|
251 |
break; |
U |
252 |
case OP_MOVE: |
|
253 |
fs.move(files, new FileRef(targetDir.getAbsolutePath(), true)); |
|
254 |
break; |
|
255 |
} |
|
256 |
result = "kopiert"; |
|
257 |
} catch (Throwable ex) { |
|
258 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
259 |
} |
|
260 |
return result; |
|
261 |
} |
5bfd34
|
262 |
*/ |
9e2964
|
263 |
|
ea7193
|
264 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
U |
265 |
FileRef savedFile = null; |
e5ff42
|
266 |
try { |
U |
267 |
FileRef datenRef = getBase(); |
3ad4db
|
268 |
//File daten = new File(datenRef.getAbsolutePath()); |
e5ff42
|
269 |
Object p = getRequest().getUserPrincipal(); |
U |
270 |
if(p instanceof Principal) { |
2121cc
|
271 |
File targetFile = new File(getTargetDir(relPath), fileName); |
7342b1
|
272 |
if(targetFile.exists()) { |
U |
273 |
/* |
|
274 |
muss delete() sein? |
|
275 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
276 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
277 |
Inhalt? |
|
278 |
*/ |
|
279 |
targetFile.delete(); |
|
280 |
} else { |
915927
|
281 |
targetFile.getParentFile().mkdirs(); |
e5ff42
|
282 |
} |
7342b1
|
283 |
targetFile.createNewFile(); |
U |
284 |
FileWriter w = new FileWriter(targetFile); |
|
285 |
w.write(contents); |
|
286 |
w.flush(); |
|
287 |
w.close(); |
|
288 |
savedFile = new FileRef( |
|
289 |
targetFile.getAbsolutePath(), |
|
290 |
targetFile.isDirectory(), |
|
291 |
targetFile.isHidden(), |
|
292 |
targetFile.lastModified(), |
|
293 |
targetFile.length()); |
e5ff42
|
294 |
} |
U |
295 |
} catch (IOException ex) { |
|
296 |
logger.log(Level.SEVERE, null, ex); |
|
297 |
} |
ea7193
|
298 |
return savedFile; |
U |
299 |
} |
5efd94
|
300 |
|
U |
301 |
/* ---- Hilfsfunktionen ---- */ |
ea7193
|
302 |
|
5efd94
|
303 |
private File getTargetDir(String relPath) { |
3c4b10
|
304 |
logger.fine(relPath); |
5efd94
|
305 |
String targetPath = null; |
U |
306 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
9e2964
|
307 |
targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length()); |
5efd94
|
308 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
9e2964
|
309 |
targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length()); |
5efd94
|
310 |
} else { |
U |
311 |
// kann eigentlich nicht sein.. |
|
312 |
} |
3c4b10
|
313 |
logger.fine(targetPath); |
5efd94
|
314 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
U |
315 |
return targetDir; |
|
316 |
} |
9e2964
|
317 |
|
5efd94
|
318 |
private FileRef getBase() { |
U |
319 |
FileRef base = null; |
|
320 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
3c4b10
|
321 |
if(o instanceof String) { |
U |
322 |
String baseStr = (String) o; |
|
323 |
logger.fine(baseStr); |
|
324 |
File file = new File(baseStr); |
|
325 |
base = new FileRef(file.getAbsolutePath(), file.isDirectory()); |
5efd94
|
326 |
} |
U |
327 |
return base; |
|
328 |
} |
|
329 |
|
|
330 |
private String getUserName() { |
|
331 |
String userName = null; |
|
332 |
Object p = getRequest().getUserPrincipal(); |
|
333 |
if(p instanceof Principal) { |
|
334 |
userName = ((Principal) p).getName(); |
|
335 |
} |
|
336 |
return userName; |
|
337 |
} |
|
338 |
|
|
339 |
/* |
8e51b7
|
340 |
private File getWebappsDir() { |
8931b7
|
341 |
File cfile = new File(this.getClass().getResource( |
U |
342 |
this.getClass().getSimpleName() + ".class").getFile()); |
8e51b7
|
343 |
String path = cfile.getAbsolutePath(); |
8931b7
|
344 |
return new File(path.substring(0, path.indexOf(getRequest().getContextPath()))); |
c7c502
|
345 |
} |
5efd94
|
346 |
*/ |
7342b1
|
347 |
|
c7c502
|
348 |
} |
8a09f4
|
349 |
|
U |
350 |
|
|
351 |
/* |
|
352 |
https://stackoverflow.com/questions/300559/move-copy-file-operations-in-java |
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
Try to use org.apache.commons.io.FileUtils (General file manipulation utilities). Facilities are provided in the following methods: |
|
357 |
|
|
358 |
(1) FileUtils.moveDirectory(File srcDir, File destDir) => Moves a directory. |
|
359 |
|
|
360 |
(2) FileUtils.moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) => Moves a directory to another directory. |
|
361 |
|
|
362 |
(3) FileUtils.moveFile(File srcFile, File destFile) => Moves a file. |
|
363 |
|
|
364 |
(4) FileUtils.moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) => Moves a file to a directory. |
|
365 |
|
|
366 |
(5) FileUtils.moveToDirectory(File src, File destDir, boolean createDestDir) => Moves a file or directory to the destination directory. |
|
367 |
|
|
368 |
It's simple, easy and fast. |
|
369 |
|
|
370 |
|
|
371 |
https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html |
|
372 |
|
|
373 |
*/ |