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