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; |
438b16
|
23 |
import de.uhilger.wbx.Bild; |
U |
24 |
import java.awt.Container; |
|
25 |
import java.awt.Image; |
|
26 |
import java.awt.MediaTracker; |
|
27 |
import java.awt.Toolkit; |
8e51b7
|
28 |
import java.io.File; |
3ad4db
|
29 |
import java.io.FileNotFoundException; |
U |
30 |
import java.io.FileReader; |
e5ff42
|
31 |
import java.io.FileWriter; |
U |
32 |
import java.io.IOException; |
fab80c
|
33 |
import java.io.Reader; |
e5ff42
|
34 |
import java.security.Principal; |
7342b1
|
35 |
import java.util.ArrayList; |
5bfd34
|
36 |
import java.util.Iterator; |
7342b1
|
37 |
import java.util.List; |
e5ff42
|
38 |
import java.util.logging.Level; |
8e51b7
|
39 |
import java.util.logging.Logger; |
5bfd34
|
40 |
import org.apache.commons.io.FileUtils; |
c7c502
|
41 |
|
U |
42 |
/** |
|
43 |
* |
|
44 |
* @author ulrich |
|
45 |
*/ |
8931b7
|
46 |
public class FileMgr extends Api { |
8e51b7
|
47 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
48 |
|
7342b1
|
49 |
public static final String PUB_DIR_PATH = "www/"; |
U |
50 |
public static final String HOME_DIR_PATH = "home/"; |
|
51 |
public static final String PUB_DIR_NAME = "Oeffentlich"; |
cccd2b
|
52 |
//public static final String HOME_DIR_NAME = "Persoenlicher Ordner"; |
U |
53 |
public static final String HOME_DIR_NAME = "Persoenlich"; |
c7c502
|
54 |
|
9e2964
|
55 |
public static final int OP_COPY = 1; |
U |
56 |
public static final int OP_MOVE = 2; |
|
57 |
|
ea7193
|
58 |
public String hallo() { |
U |
59 |
return "Hallo Welt!"; |
|
60 |
} |
|
61 |
|
7342b1
|
62 |
public List<FileRef> list(String relPath) { |
f7d8bf
|
63 |
Bild bild = new Bild(); |
5dfab6
|
64 |
List<FileRef> files = new ArrayList(); |
7342b1
|
65 |
if(relPath.length() == 0) { |
2121cc
|
66 |
FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
5dfab6
|
67 |
logger.finer(namedPublicFolder.getAbsolutePath()); |
2121cc
|
68 |
FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
5dfab6
|
69 |
logger.finer(namedHomeFolder.getAbsolutePath()); |
2121cc
|
70 |
files = new ArrayList(); |
7342b1
|
71 |
files.add(namedHomeFolder); |
U |
72 |
files.add(namedPublicFolder); |
ea7193
|
73 |
} else { |
2121cc
|
74 |
String path = getTargetDir(relPath).getAbsolutePath(); |
5bfd34
|
75 |
logger.fine("listing path: " + path); |
U |
76 |
File dir = new File(path); |
|
77 |
if(dir.exists()) { |
|
78 |
File[] fileArray = dir.listFiles(); |
|
79 |
for(int i = 0; i < fileArray.length; i++) { |
|
80 |
logger.fine(fileArray[i].toURI().toString()); |
|
81 |
String fname = fileArray[i].toURI().toString().replace("file:/", ""); |
|
82 |
if(fileArray[i].isDirectory()) { |
|
83 |
fname = fname.substring(0, fname.length() - 1); |
|
84 |
} |
|
85 |
logger.fine(fname); |
f7d8bf
|
86 |
FileRef ref = new FileRef(fname, fileArray[i].isDirectory()); |
U |
87 |
ref.setMimetype(bild.getMimeType(fileArray[i])); |
|
88 |
files.add(ref); |
5bfd34
|
89 |
} |
5dfab6
|
90 |
} |
c509a0
|
91 |
} |
7342b1
|
92 |
return files; |
2121cc
|
93 |
} |
U |
94 |
|
c509a0
|
95 |
public FileRef newFolder(String relPath, String folderName) { |
U |
96 |
logger.finer(relPath); |
|
97 |
String targetPath = null; |
|
98 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
99 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName; |
|
100 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
101 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName; |
|
102 |
} else { |
|
103 |
// kann eigentlich nicht sein.. |
|
104 |
} |
|
105 |
logger.finer(targetPath); |
|
106 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
107 |
targetDir.mkdirs(); |
|
108 |
return new FileRef(targetDir.getAbsolutePath(), true); |
5dfab6
|
109 |
} |
U |
110 |
|
3ad4db
|
111 |
public String getCode(String relPath, String fileName) { |
U |
112 |
String code = null; |
|
113 |
|
|
114 |
Object p = getRequest().getUserPrincipal(); |
|
115 |
if(p instanceof Principal) { |
fab80c
|
116 |
Reader reader = null; |
3ad4db
|
117 |
try { |
U |
118 |
File targetFile = new File(getTargetDir(relPath), fileName); |
fab80c
|
119 |
|
U |
120 |
//reader = new InputStreamReader(new FileInputStream(targetFile), "UTF8"); |
|
121 |
|
3ad4db
|
122 |
reader = new FileReader(targetFile); |
U |
123 |
StringBuffer buf = new StringBuffer(); |
|
124 |
char[] readBuffer = new char[1024]; |
|
125 |
int charsRead = reader.read(readBuffer); |
|
126 |
while(charsRead > -1) { |
|
127 |
buf.append(readBuffer, 0, charsRead); |
|
128 |
charsRead = reader.read(readBuffer); |
|
129 |
} |
|
130 |
code = buf.toString(); |
|
131 |
} catch (FileNotFoundException ex) { |
|
132 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
133 |
} catch (IOException ex) { |
|
134 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
135 |
} finally { |
|
136 |
try { |
|
137 |
reader.close(); |
|
138 |
} catch (IOException ex) { |
|
139 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
} |
|
144 |
|
|
145 |
return code; |
|
146 |
} |
|
147 |
|
663ee9
|
148 |
public String renameFile(String relPath, String fname, String newName) { |
U |
149 |
File targetDir = getTargetDir(relPath); |
|
150 |
File file = new File(targetDir, fname); |
|
151 |
file.renameTo(new File(targetDir, newName)); |
|
152 |
return fname + " umbenannt zu " + newName; |
|
153 |
} |
|
154 |
|
fc1897
|
155 |
public String deleteFiles(String relPath, List fileNames) { |
U |
156 |
String result = null; |
|
157 |
try { |
|
158 |
logger.fine(fileNames.toString()); |
|
159 |
File targetDir = getTargetDir(relPath); |
|
160 |
for(int i=0; i < fileNames.size(); i++) { |
|
161 |
Object o = fileNames.get(i); |
|
162 |
if(o instanceof ArrayList) { |
|
163 |
ArrayList al = (ArrayList) o; |
|
164 |
logger.fine(al.get(0).toString()); |
|
165 |
File targetFile = new File(targetDir, al.get(0).toString()); |
|
166 |
logger.fine(targetFile.getAbsolutePath()); |
5bfd34
|
167 |
if(targetFile.isDirectory()) { |
U |
168 |
FileUtils.deleteDirectory(targetFile); |
|
169 |
} else { |
|
170 |
targetFile.delete(); |
|
171 |
} |
fc1897
|
172 |
} |
U |
173 |
} |
|
174 |
result = "deleted"; |
|
175 |
} catch (Throwable ex) { |
|
176 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
177 |
} |
|
178 |
return result; |
|
179 |
} |
|
180 |
|
9e2964
|
181 |
public String copyFiles(String fromPath, String toPath, List fileNames) { |
fab629
|
182 |
return copyOrMoveFiles(fromPath, toPath, fileNames, OP_COPY); |
9e2964
|
183 |
} |
U |
184 |
|
|
185 |
public String moveFiles(String fromPath, String toPath, List fileNames) { |
fab629
|
186 |
return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE); |
U |
187 |
} |
|
188 |
|
|
189 |
private String copyOrMoveFiles(String fromPath, String toPath, List fileNames, int operation) { |
5bfd34
|
190 |
String result = null; |
U |
191 |
try { |
|
192 |
File srcDir = getTargetDir(fromPath); |
|
193 |
File targetDir = getTargetDir(toPath); |
|
194 |
Iterator i = fileNames.iterator(); |
|
195 |
while(i.hasNext()) { |
|
196 |
Object o = i.next(); |
|
197 |
if (o instanceof ArrayList) { |
|
198 |
ArrayList al = (ArrayList) o; |
|
199 |
File srcFile = new File(srcDir, al.get(0).toString()); |
|
200 |
if(srcFile.isDirectory()) { |
fab629
|
201 |
if(operation == OP_MOVE) { |
U |
202 |
FileUtils.moveDirectoryToDirectory(srcFile, targetDir, false); |
|
203 |
} else { |
|
204 |
FileUtils.copyDirectoryToDirectory(srcFile, targetDir); |
|
205 |
} |
5bfd34
|
206 |
} else { |
fab629
|
207 |
if(operation == OP_MOVE) { |
U |
208 |
FileUtils.moveFileToDirectory(srcFile, targetDir, false); |
|
209 |
} else { |
|
210 |
FileUtils.copyFileToDirectory(srcFile, targetDir); |
|
211 |
} |
5bfd34
|
212 |
} |
U |
213 |
} |
|
214 |
} |
|
215 |
} catch (IOException ex) { |
|
216 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
217 |
} |
|
218 |
return result; |
9e2964
|
219 |
} |
U |
220 |
|
47e9d4
|
221 |
public FileRef saveTextFileAs(String relPath, String fileName, String contents) { |
U |
222 |
FileRef savedFile = null; |
|
223 |
logger.fine(relPath + " " + fileName); |
|
224 |
//FileRef datenRef = getBase(); |
|
225 |
Object p = getRequest().getUserPrincipal(); |
|
226 |
if(p instanceof Principal) { |
|
227 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
228 |
if(targetFile.exists()) { |
|
229 |
targetFile = getNewFileName(targetFile); |
|
230 |
} else { |
|
231 |
targetFile.getParentFile().mkdirs(); |
|
232 |
} |
|
233 |
saveToFile(targetFile, contents); |
|
234 |
} |
|
235 |
return savedFile; |
|
236 |
} |
|
237 |
|
|
238 |
private File getNewFileName(File file) { |
|
239 |
File dir = file.getParentFile(); |
|
240 |
String targetName = file.getName(); |
|
241 |
logger.fine("targetName: " + targetName); |
|
242 |
String ext = ""; |
|
243 |
int dotpos = targetName.indexOf("."); |
|
244 |
if(dotpos > -1) { |
|
245 |
ext = targetName.substring(dotpos); |
|
246 |
targetName = targetName.substring(0, dotpos); |
|
247 |
} |
|
248 |
logger.fine("targetName: " + targetName + ", ext: " + ext); |
|
249 |
int i = 1; |
|
250 |
while(file.exists()) { |
|
251 |
StringBuffer buf = new StringBuffer(); |
|
252 |
buf.append(targetName); |
|
253 |
buf.append("-"); |
|
254 |
buf.append(i); |
|
255 |
if(ext.length() > 0) { |
|
256 |
buf.append(ext); |
|
257 |
} |
|
258 |
file = new File(dir, buf.toString()); |
|
259 |
i++; |
|
260 |
} |
|
261 |
logger.fine("new file: " + file.getName()); |
|
262 |
return file; |
942d63
|
263 |
} |
47e9d4
|
264 |
|
U |
265 |
private FileRef saveToFile(File targetFile, String contents) { |
ea7193
|
266 |
FileRef savedFile = null; |
e5ff42
|
267 |
try { |
47e9d4
|
268 |
targetFile.createNewFile(); |
U |
269 |
FileWriter w = new FileWriter(targetFile); |
942d63
|
270 |
//w.write(StringEscapeUtils.unescapeHtml(contents)); |
47e9d4
|
271 |
w.write(contents); |
U |
272 |
w.flush(); |
|
273 |
w.close(); |
|
274 |
savedFile = new FileRef( |
|
275 |
targetFile.getAbsolutePath(), |
|
276 |
targetFile.isDirectory(), |
|
277 |
targetFile.isHidden(), |
|
278 |
targetFile.lastModified(), |
|
279 |
targetFile.length()); |
e5ff42
|
280 |
} catch (IOException ex) { |
47e9d4
|
281 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
U |
282 |
} |
|
283 |
return savedFile; |
|
284 |
} |
|
285 |
|
|
286 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
|
287 |
FileRef savedFile = null; |
|
288 |
logger.fine(relPath + " " + fileName); |
|
289 |
//FileRef datenRef = getBase(); |
|
290 |
Object p = getRequest().getUserPrincipal(); |
|
291 |
if(p instanceof Principal) { |
|
292 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
293 |
if(targetFile.exists()) { |
|
294 |
/* |
|
295 |
muss delete() sein? |
|
296 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
297 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
298 |
Inhalt? |
|
299 |
*/ |
|
300 |
targetFile.delete(); |
|
301 |
} else { |
|
302 |
targetFile.getParentFile().mkdirs(); |
|
303 |
} |
|
304 |
saveToFile(targetFile, contents); |
e5ff42
|
305 |
} |
ea7193
|
306 |
return savedFile; |
U |
307 |
} |
438b16
|
308 |
|
U |
309 |
public String bildVerkleinern(String relPath, String bildName) { |
|
310 |
File dir = getTargetDir(relPath); |
|
311 |
File original = new File(dir, bildName); |
|
312 |
Bild bild = new Bild(); |
|
313 |
//for (int i = 0; i < Bild.GR.length; i++) { |
5efd94
|
314 |
|
438b16
|
315 |
//int gr = bild.getVariantenGroesse(i); |
U |
316 |
|
|
317 |
String ext = ""; |
|
318 |
String nurname = bildName; |
|
319 |
int dotpos = bildName.indexOf("."); |
|
320 |
if (dotpos > -1) { |
|
321 |
ext = bildName.substring(dotpos); |
|
322 |
nurname = bildName.substring(0, dotpos); |
|
323 |
} |
|
324 |
|
|
325 |
Image image = Toolkit.getDefaultToolkit().getImage(original.getAbsolutePath()); |
|
326 |
MediaTracker mediaTracker = new MediaTracker(new Container()); |
|
327 |
mediaTracker.addImage(image, 0); |
|
328 |
try { |
|
329 |
mediaTracker.waitForID(0); |
|
330 |
|
|
331 |
if(!mediaTracker.isErrorAny()) { |
|
332 |
for(int i = 0; i < Bild.GR.length; i++) { |
|
333 |
StringBuffer buf = new StringBuffer(); |
|
334 |
buf.append(nurname); |
|
335 |
buf.append(bild.getVariantenName(i)); |
|
336 |
buf.append(ext); |
|
337 |
File newImgFile = new File(dir, buf.toString()); |
|
338 |
if(!newImgFile.exists()) { |
|
339 |
logger.fine(original.getAbsolutePath() + " " + newImgFile.getAbsolutePath()); |
a44b26
|
340 |
bild.writeImageFile(image, bild.getVariantenGroesse(i), bild.getMimeType(original), newImgFile.getAbsolutePath()); |
438b16
|
341 |
//bild.writeImageFile(image, photo.getVariantenGroesse(i), photo.getMimetype(), photo.getAbsolutePath(basisPfad), photo.getVariantenName(basisPfad, i)); |
U |
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
} catch(IOException | InterruptedException ex) { |
|
346 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
347 |
} |
|
348 |
|
|
349 |
return "ok"; |
|
350 |
} |
|
351 |
|
5efd94
|
352 |
/* ---- Hilfsfunktionen ---- */ |
ea7193
|
353 |
|
5efd94
|
354 |
private File getTargetDir(String relPath) { |
3c4b10
|
355 |
logger.fine(relPath); |
5efd94
|
356 |
String targetPath = null; |
U |
357 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
9e2964
|
358 |
targetPath = PUB_DIR_PATH + getUserName() + relPath.substring(PUB_DIR_NAME.length()); |
5efd94
|
359 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
9e2964
|
360 |
targetPath = HOME_DIR_PATH + getUserName() + relPath.substring(HOME_DIR_NAME.length()); |
5efd94
|
361 |
} else { |
U |
362 |
// kann eigentlich nicht sein.. |
|
363 |
} |
3c4b10
|
364 |
logger.fine(targetPath); |
5efd94
|
365 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
U |
366 |
return targetDir; |
|
367 |
} |
9e2964
|
368 |
|
5efd94
|
369 |
private FileRef getBase() { |
U |
370 |
FileRef base = null; |
|
371 |
Object o = getServletContext().getAttribute(Initialiser.FILE_BASE); |
3c4b10
|
372 |
if(o instanceof String) { |
U |
373 |
String baseStr = (String) o; |
|
374 |
logger.fine(baseStr); |
|
375 |
File file = new File(baseStr); |
|
376 |
base = new FileRef(file.getAbsolutePath(), file.isDirectory()); |
5efd94
|
377 |
} |
U |
378 |
return base; |
|
379 |
} |
|
380 |
|
|
381 |
private String getUserName() { |
|
382 |
String userName = null; |
|
383 |
Object p = getRequest().getUserPrincipal(); |
|
384 |
if(p instanceof Principal) { |
|
385 |
userName = ((Principal) p).getName(); |
|
386 |
} |
|
387 |
return userName; |
547755
|
388 |
} |
U |
389 |
} |