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 |
|
6e70be
|
21 |
import de.uhilger.filecms.data.FileRef; |
d14d78
|
22 |
import de.uhilger.filecms.pub.AbstractComparator; |
U |
23 |
import de.uhilger.filecms.pub.FileNameComparator; |
bb9f8c
|
24 |
import de.uhilger.wbx.Bild; |
e639c2
|
25 |
import de.uhilger.wbx.WbxUtils; |
26115e
|
26 |
import static de.uhilger.wbx.WbxUtils.EMPTY_STRING; |
U |
27 |
import static de.uhilger.wbx.WbxUtils.WBX_FILE_BASE; |
4e2a82
|
28 |
import de.uhilger.wbx.data.Inhalt; |
8e51b7
|
29 |
import java.io.File; |
af9930
|
30 |
import java.io.FileInputStream; |
3ad4db
|
31 |
import java.io.FileNotFoundException; |
6bd2c1
|
32 |
import java.io.FileOutputStream; |
3ad4db
|
33 |
import java.io.FileReader; |
e5ff42
|
34 |
import java.io.FileWriter; |
U |
35 |
import java.io.IOException; |
6bd2c1
|
36 |
import java.io.InputStream; |
fab80c
|
37 |
import java.io.Reader; |
e5ff42
|
38 |
import java.security.Principal; |
7342b1
|
39 |
import java.util.ArrayList; |
d14d78
|
40 |
import java.util.Arrays; |
26115e
|
41 |
import java.util.Collections; |
U |
42 |
import java.util.Comparator; |
6bd2c1
|
43 |
import java.util.Enumeration; |
5bfd34
|
44 |
import java.util.Iterator; |
7342b1
|
45 |
import java.util.List; |
e5ff42
|
46 |
import java.util.logging.Level; |
8e51b7
|
47 |
import java.util.logging.Logger; |
af9930
|
48 |
import java.util.zip.Adler32; |
U |
49 |
import java.util.zip.CheckedOutputStream; |
6bd2c1
|
50 |
import java.util.zip.ZipEntry; |
U |
51 |
import java.util.zip.ZipFile; |
af9930
|
52 |
import java.util.zip.ZipOutputStream; |
4e2a82
|
53 |
import javax.servlet.http.HttpServletRequest; |
bb9f8c
|
54 |
import net.coobird.thumbnailator.Thumbnails; |
5bfd34
|
55 |
import org.apache.commons.io.FileUtils; |
c7c502
|
56 |
|
U |
57 |
/** |
fafe1b
|
58 |
* Methoden zur Verwaltung von Dateien |
c7c502
|
59 |
*/ |
8931b7
|
60 |
public class FileMgr extends Api { |
8e51b7
|
61 |
private static final Logger logger = Logger.getLogger(FileMgr.class.getName()); |
c7c502
|
62 |
|
972e94
|
63 |
|
9e2964
|
64 |
public static final int OP_COPY = 1; |
U |
65 |
public static final int OP_MOVE = 2; |
|
66 |
|
ea7193
|
67 |
public String hallo() { |
U |
68 |
return "Hallo Welt!"; |
|
69 |
} |
|
70 |
|
1f550a
|
71 |
/** |
U |
72 |
* Inhalte der WebBox listen. Hier wird nur der relative Pfad |
|
73 |
* ausgehend von www oder home ausgegeben sowie zudem ausgehend |
|
74 |
* von $daten und $basis, sofern der Benutzer die Rolle wbxAdmin hat. |
|
75 |
* |
|
76 |
* Andere Inhalte werden nicht ausgegeben. |
|
77 |
* |
|
78 |
* @param relPath |
|
79 |
* @return |
|
80 |
*/ |
7342b1
|
81 |
public List<FileRef> list(String relPath) { |
d14d78
|
82 |
return listInt(relPath, "name", AbstractComparator.ORDER_ASC); |
U |
83 |
} |
|
84 |
|
|
85 |
public List<FileRef> listOrdered(String relPath, String orderBy, String order) { |
|
86 |
return listInt(relPath, orderBy, order); |
|
87 |
} |
|
88 |
|
|
89 |
/** |
|
90 |
* Inhalte der WebBox listen. Hier wird nur der relative Pfad |
|
91 |
* ausgehend von www oder home ausgegeben sowie zudem ausgehend |
|
92 |
* von $daten und $basis, sofern der Benutzer die Rolle wbxAdmin hat. |
|
93 |
* |
|
94 |
* Andere Inhalte werden nicht ausgegeben. |
|
95 |
* |
|
96 |
* @param relPath |
|
97 |
* @param orderBy 'name' |
|
98 |
* @param order AbstractComparator.ORDER_ASC oder AbstractComparator.ORDER_DESC |
|
99 |
* @return |
|
100 |
*/ |
|
101 |
private List<FileRef> listInt(String relPath, String orderBy, String order) { |
f7d8bf
|
102 |
Bild bild = new Bild(); |
5dfab6
|
103 |
List<FileRef> files = new ArrayList(); |
ac72fe
|
104 |
if (!relPath.startsWith(".") && !relPath.contains("WEB-INF") && !relPath.contains("META-INF")) { |
3d0c6d
|
105 |
if (relPath.length() == 0) { |
U |
106 |
FileRef namedPublicFolder = new FileRef(PUB_DIR_NAME, true); |
|
107 |
logger.finer(namedPublicFolder.getAbsolutePath()); |
|
108 |
FileRef namedHomeFolder = new FileRef(HOME_DIR_NAME, true); |
|
109 |
logger.finer(namedHomeFolder.getAbsolutePath()); |
|
110 |
FileRef namedDavFolder = new FileRef(DAV_DIR_NAME, true); |
|
111 |
logger.finer(namedDavFolder.getAbsolutePath()); |
|
112 |
files = new ArrayList(); |
|
113 |
files.add(namedHomeFolder); |
|
114 |
files.add(namedPublicFolder); |
|
115 |
files.add(namedDavFolder); |
|
116 |
if (getRequest().isUserInRole(WBX_ADMIN_ROLE)) { |
|
117 |
FileRef namedBaseFolder = new FileRef(WBX_BASE, true); |
|
118 |
FileRef namedDataFolder = new FileRef(WBX_DATA, true); |
|
119 |
files.add(namedBaseFolder); |
|
120 |
files.add(namedDataFolder); |
d14d78
|
121 |
} |
3d0c6d
|
122 |
} else { |
U |
123 |
String path = getTargetDir(relPath).getAbsolutePath(); |
|
124 |
logger.fine("listing path: " + path); |
|
125 |
File dir = new File(path); |
|
126 |
if (dir.exists()) { |
|
127 |
File[] fileArray = dir.listFiles(); |
|
128 |
if (orderBy != null && orderBy.equalsIgnoreCase("name")) { |
|
129 |
Arrays.sort(fileArray, new FileNameComparator(order)); |
|
130 |
} else { |
|
131 |
Arrays.sort(fileArray, new FileNameComparator(AbstractComparator.ORDER_ASC)); |
5bfd34
|
132 |
} |
3d0c6d
|
133 |
for (int i = 0; i < fileArray.length; i++) { |
U |
134 |
logger.fine(fileArray[i].toURI().toString()); |
|
135 |
String fname = fileArray[i].toURI().toString().replace("file:/", ""); |
|
136 |
if (fileArray[i].isDirectory()) { |
|
137 |
fname = fname.substring(0, fname.length() - 1); |
|
138 |
} |
|
139 |
logger.fine(fname); |
ac72fe
|
140 |
if(!fname.contains("WEB-INF") && !fname.contains("META-INF")) { |
c5eaaa
|
141 |
long fLen = fileArray[i].length(); |
U |
142 |
long lastMod = fileArray[i].lastModified(); |
|
143 |
FileRef ref = new FileRef(fname, fileArray[i].isDirectory(), fileArray[i].isHidden(), lastMod, fLen); |
ac72fe
|
144 |
ref.setMimetype(bild.getMimeType(fileArray[i])); |
U |
145 |
files.add(ref); |
|
146 |
} |
3d0c6d
|
147 |
} |
5bfd34
|
148 |
} |
5dfab6
|
149 |
} |
3d0c6d
|
150 |
} |
7342b1
|
151 |
return files; |
2121cc
|
152 |
} |
4e2a82
|
153 |
|
26115e
|
154 |
public List<Inhalt> collect(String relativePath, int maxTiefe, int maxAnzahl, int len) { |
U |
155 |
|
4e2a82
|
156 |
WbxUtils wu = new WbxUtils(); |
U |
157 |
HttpServletRequest req = getRequest(); |
|
158 |
String requestUrl = req.getRequestURL().toString(); |
|
159 |
String contextPath = req.getContextPath(); |
26115e
|
160 |
return wu.collectFiles(requestUrl, contextPath, relativePath, maxTiefe, maxAnzahl, len); |
4e2a82
|
161 |
} |
d14d78
|
162 |
|
c509a0
|
163 |
public FileRef newFolder(String relPath, String folderName) { |
eb2a2d
|
164 |
if (!relPath.startsWith(".")) { |
U |
165 |
logger.finer(relPath); |
|
166 |
String targetPath = null; |
|
167 |
if(relPath.startsWith(PUB_DIR_NAME)) { |
|
168 |
targetPath = PUB_DIR_PATH + getUserName() + "/" + relPath.substring(PUB_DIR_NAME.length()) + "/" + folderName; |
|
169 |
} else if(relPath.startsWith(HOME_DIR_NAME)) { |
|
170 |
targetPath = HOME_DIR_PATH + getUserName() + "/" + relPath.substring(HOME_DIR_NAME.length()) + "/" + folderName; |
|
171 |
} else { |
|
172 |
// kann eigentlich nicht sein.. |
|
173 |
} |
|
174 |
logger.finer(targetPath); |
|
175 |
File targetDir = new File(getBase().getAbsolutePath(), targetPath); |
|
176 |
targetDir.mkdirs(); |
|
177 |
return new FileRef(targetDir.getAbsolutePath(), true); |
c509a0
|
178 |
} else { |
eb2a2d
|
179 |
return null; |
c509a0
|
180 |
} |
5dfab6
|
181 |
} |
U |
182 |
|
3ad4db
|
183 |
public String getCode(String relPath, String fileName) { |
U |
184 |
String code = null; |
eb2a2d
|
185 |
if (!relPath.startsWith(".")) { |
U |
186 |
Object p = getRequest().getUserPrincipal(); |
|
187 |
if (p instanceof Principal) { |
|
188 |
Reader reader = null; |
3ad4db
|
189 |
try { |
eb2a2d
|
190 |
File targetFile = new File(getTargetDir(relPath), fileName); |
U |
191 |
|
|
192 |
//reader = new InputStreamReader(new FileInputStream(targetFile), "UTF8"); |
|
193 |
reader = new FileReader(targetFile); |
|
194 |
StringBuffer buf = new StringBuffer(); |
|
195 |
char[] readBuffer = new char[1024]; |
|
196 |
int charsRead = reader.read(readBuffer); |
|
197 |
while (charsRead > -1) { |
|
198 |
buf.append(readBuffer, 0, charsRead); |
|
199 |
charsRead = reader.read(readBuffer); |
|
200 |
} |
|
201 |
code = buf.toString(); |
|
202 |
} catch (FileNotFoundException ex) { |
|
203 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
3ad4db
|
204 |
} catch (IOException ex) { |
U |
205 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
eb2a2d
|
206 |
} finally { |
U |
207 |
try { |
|
208 |
reader.close(); |
|
209 |
} catch (IOException ex) { |
|
210 |
Logger.getLogger(FileMgr.class.getName()).log(Level.SEVERE, null, ex); |
|
211 |
} |
3ad4db
|
212 |
} |
eb2a2d
|
213 |
|
3ad4db
|
214 |
} |
eb2a2d
|
215 |
} |
3ad4db
|
216 |
return code; |
U |
217 |
} |
|
218 |
|
663ee9
|
219 |
public String renameFile(String relPath, String fname, String newName) { |
eb2a2d
|
220 |
if (!relPath.startsWith(".")) { |
U |
221 |
File targetDir = getTargetDir(relPath); |
|
222 |
File file = new File(targetDir, fname); |
|
223 |
file.renameTo(new File(targetDir, newName)); |
|
224 |
return fname + " umbenannt zu " + newName; |
|
225 |
} else { |
|
226 |
return "Pfad nicht erlaubt."; |
|
227 |
} |
663ee9
|
228 |
} |
U |
229 |
|
fc1897
|
230 |
public String deleteFiles(String relPath, List fileNames) { |
U |
231 |
String result = null; |
|
232 |
try { |
|
233 |
logger.fine(fileNames.toString()); |
eb2a2d
|
234 |
if (!relPath.startsWith(".")) { |
U |
235 |
File targetDir = getTargetDir(relPath); |
|
236 |
for(int i=0; i < fileNames.size(); i++) { |
|
237 |
Object o = fileNames.get(i); |
|
238 |
if(o instanceof ArrayList) { |
|
239 |
ArrayList al = (ArrayList) o; |
|
240 |
logger.fine(al.get(0).toString()); |
|
241 |
File targetFile = new File(targetDir, al.get(0).toString()); |
|
242 |
logger.fine(targetFile.getAbsolutePath()); |
|
243 |
if(targetFile.isDirectory()) { |
|
244 |
FileUtils.deleteDirectory(targetFile); |
|
245 |
} else { |
|
246 |
targetFile.delete(); |
|
247 |
} |
5bfd34
|
248 |
} |
fc1897
|
249 |
} |
eb2a2d
|
250 |
result = "deleted"; |
fc1897
|
251 |
} |
U |
252 |
} catch (Throwable ex) { |
|
253 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
254 |
} |
|
255 |
return result; |
|
256 |
} |
|
257 |
|
9e2964
|
258 |
public String copyFiles(String fromPath, String toPath, List fileNames) { |
fab629
|
259 |
return copyOrMoveFiles(fromPath, toPath, fileNames, OP_COPY); |
9e2964
|
260 |
} |
U |
261 |
|
|
262 |
public String moveFiles(String fromPath, String toPath, List fileNames) { |
fab629
|
263 |
return copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE); |
U |
264 |
} |
|
265 |
|
|
266 |
private String copyOrMoveFiles(String fromPath, String toPath, List fileNames, int operation) { |
5bfd34
|
267 |
String result = null; |
U |
268 |
try { |
eb2a2d
|
269 |
if (!fromPath.startsWith(".")) { |
U |
270 |
File srcDir = getTargetDir(fromPath); |
|
271 |
File targetDir = getTargetDir(toPath); |
|
272 |
Iterator i = fileNames.iterator(); |
|
273 |
while(i.hasNext()) { |
|
274 |
Object o = i.next(); |
|
275 |
if (o instanceof ArrayList) { |
|
276 |
ArrayList al = (ArrayList) o; |
|
277 |
File srcFile = new File(srcDir, al.get(0).toString()); |
|
278 |
if(srcFile.isDirectory()) { |
|
279 |
if(operation == OP_MOVE) { |
|
280 |
FileUtils.moveDirectoryToDirectory(srcFile, targetDir, false); |
|
281 |
} else { |
|
282 |
FileUtils.copyDirectoryToDirectory(srcFile, targetDir); |
|
283 |
} |
fab629
|
284 |
} else { |
eb2a2d
|
285 |
if(operation == OP_MOVE) { |
U |
286 |
FileUtils.moveFileToDirectory(srcFile, targetDir, false); |
|
287 |
} else { |
|
288 |
FileUtils.copyFileToDirectory(srcFile, targetDir); |
|
289 |
} |
fab629
|
290 |
} |
5bfd34
|
291 |
} |
U |
292 |
} |
|
293 |
} |
|
294 |
} catch (IOException ex) { |
|
295 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
296 |
} |
|
297 |
return result; |
9e2964
|
298 |
} |
U |
299 |
|
47e9d4
|
300 |
public FileRef saveTextFileAs(String relPath, String fileName, String contents) { |
U |
301 |
FileRef savedFile = null; |
|
302 |
logger.fine(relPath + " " + fileName); |
eb2a2d
|
303 |
if (!relPath.startsWith(".")) { |
U |
304 |
//FileRef datenRef = getBase(); |
|
305 |
Object p = getRequest().getUserPrincipal(); |
|
306 |
if(p instanceof Principal) { |
|
307 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
308 |
if(targetFile.exists()) { |
|
309 |
targetFile = getNewFileName(targetFile); |
|
310 |
} else { |
|
311 |
targetFile.getParentFile().mkdirs(); |
|
312 |
} |
|
313 |
saveToFile(targetFile, contents); |
47e9d4
|
314 |
} |
U |
315 |
} |
|
316 |
return savedFile; |
|
317 |
} |
|
318 |
|
|
319 |
private File getNewFileName(File file) { |
|
320 |
File dir = file.getParentFile(); |
|
321 |
String targetName = file.getName(); |
|
322 |
logger.fine("targetName: " + targetName); |
|
323 |
String ext = ""; |
|
324 |
int dotpos = targetName.indexOf("."); |
|
325 |
if(dotpos > -1) { |
|
326 |
ext = targetName.substring(dotpos); |
|
327 |
targetName = targetName.substring(0, dotpos); |
|
328 |
} |
|
329 |
logger.fine("targetName: " + targetName + ", ext: " + ext); |
|
330 |
int i = 1; |
|
331 |
while(file.exists()) { |
|
332 |
StringBuffer buf = new StringBuffer(); |
|
333 |
buf.append(targetName); |
|
334 |
buf.append("-"); |
|
335 |
buf.append(i); |
|
336 |
if(ext.length() > 0) { |
|
337 |
buf.append(ext); |
|
338 |
} |
|
339 |
file = new File(dir, buf.toString()); |
|
340 |
i++; |
|
341 |
} |
|
342 |
logger.fine("new file: " + file.getName()); |
|
343 |
return file; |
942d63
|
344 |
} |
47e9d4
|
345 |
|
U |
346 |
private FileRef saveToFile(File targetFile, String contents) { |
ea7193
|
347 |
FileRef savedFile = null; |
e5ff42
|
348 |
try { |
47e9d4
|
349 |
targetFile.createNewFile(); |
U |
350 |
FileWriter w = new FileWriter(targetFile); |
942d63
|
351 |
//w.write(StringEscapeUtils.unescapeHtml(contents)); |
47e9d4
|
352 |
w.write(contents); |
U |
353 |
w.flush(); |
|
354 |
w.close(); |
|
355 |
savedFile = new FileRef( |
|
356 |
targetFile.getAbsolutePath(), |
|
357 |
targetFile.isDirectory(), |
|
358 |
targetFile.isHidden(), |
|
359 |
targetFile.lastModified(), |
|
360 |
targetFile.length()); |
e5ff42
|
361 |
} catch (IOException ex) { |
47e9d4
|
362 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
U |
363 |
} |
|
364 |
return savedFile; |
|
365 |
} |
|
366 |
|
|
367 |
public FileRef saveTextFile(String relPath, String fileName, String contents) { |
|
368 |
FileRef savedFile = null; |
|
369 |
logger.fine(relPath + " " + fileName); |
eb2a2d
|
370 |
if (!relPath.startsWith(".")) { |
U |
371 |
//FileRef datenRef = getBase(); |
|
372 |
Object p = getRequest().getUserPrincipal(); |
|
373 |
if(p instanceof Principal) { |
|
374 |
File targetFile = new File(getTargetDir(relPath), fileName); |
|
375 |
if(targetFile.exists()) { |
|
376 |
/* |
|
377 |
muss delete() sein? |
|
378 |
pruefen: ueberschreibt der FileWriter den alteen Inhalt oder |
|
379 |
entsteht eine unerwuenschte Mischung aus altem und neuem |
|
380 |
Inhalt? |
|
381 |
*/ |
|
382 |
targetFile.delete(); |
|
383 |
} else { |
|
384 |
targetFile.getParentFile().mkdirs(); |
|
385 |
} |
|
386 |
saveToFile(targetFile, contents); |
47e9d4
|
387 |
} |
e5ff42
|
388 |
} |
ea7193
|
389 |
return savedFile; |
U |
390 |
} |
438b16
|
391 |
|
U |
392 |
public String bildVerkleinern(String relPath, String bildName) { |
eb2a2d
|
393 |
if (!relPath.startsWith(".")) { |
U |
394 |
File dir = getTargetDir(relPath); |
|
395 |
File original = new File(dir, bildName); |
|
396 |
Bild bild = new Bild(); |
|
397 |
//for (int i = 0; i < Bild.GR.length; i++) { |
|
398 |
|
438b16
|
399 |
//int gr = bild.getVariantenGroesse(i); |
U |
400 |
String ext = ""; |
|
401 |
String nurname = bildName; |
|
402 |
int dotpos = bildName.indexOf("."); |
|
403 |
if (dotpos > -1) { |
|
404 |
ext = bildName.substring(dotpos); |
|
405 |
nurname = bildName.substring(0, dotpos); |
|
406 |
} |
eb2a2d
|
407 |
|
bb9f8c
|
408 |
// 120, 240, 500, 700, 1200 |
U |
409 |
|
|
410 |
|
|
411 |
for (int i = 0; i < Bild.GR.length; i++) { |
|
412 |
StringBuffer buf = new StringBuffer(); |
|
413 |
buf.append(nurname); |
|
414 |
buf.append(bild.getVariantenName(i)); |
|
415 |
buf.append(ext); |
|
416 |
File newImgFile = new File(dir, buf.toString()); |
|
417 |
try { |
|
418 |
Thumbnails.of(original) |
|
419 |
.size(bild.getVariantenGroesse(i), bild.getVariantenGroesse(i)) |
|
420 |
.keepAspectRatio(true) |
8a8152
|
421 |
.outputQuality(0.7) |
bb9f8c
|
422 |
.toFile(newImgFile); |
U |
423 |
} catch (IOException ex) { |
|
424 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
425 |
} |
|
426 |
} |
eb2a2d
|
427 |
return "ok"; |
U |
428 |
} else { |
|
429 |
return "Pfad micht erlaubt."; |
|
430 |
} |
438b16
|
431 |
} |
6bd2c1
|
432 |
|
bb9f8c
|
433 |
public String bildRotieren(String relPath, String bildName) { |
U |
434 |
if (!relPath.startsWith(".")) { |
|
435 |
File dir = getTargetDir(relPath); |
|
436 |
File original = new File(dir, bildName); |
|
437 |
|
|
438 |
String ext = ""; |
|
439 |
String nurname = bildName; |
|
440 |
int dotpos = bildName.indexOf("."); |
|
441 |
if (dotpos > -1) { |
|
442 |
ext = bildName.substring(dotpos); |
|
443 |
nurname = bildName.substring(0, dotpos); |
|
444 |
} |
|
445 |
|
|
446 |
StringBuffer buf = new StringBuffer(); |
|
447 |
buf.append(nurname); |
|
448 |
buf.append("-rot"); |
|
449 |
buf.append(ext); |
|
450 |
File newImgFile = new File(dir, buf.toString()); |
|
451 |
|
|
452 |
logger.fine("original: " + original.getAbsolutePath() + " newImgFile: " + newImgFile.getAbsolutePath()); |
|
453 |
|
|
454 |
try { |
|
455 |
Thumbnails.of(original) |
|
456 |
.scale(1) |
|
457 |
.rotate(90) |
|
458 |
.toFile(newImgFile); |
|
459 |
} catch (IOException ex) { |
|
460 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
461 |
} |
|
462 |
|
|
463 |
|
|
464 |
return "ok"; |
|
465 |
} else { |
|
466 |
return "Pfad micht erlaubt."; |
|
467 |
} |
|
468 |
} |
af9930
|
469 |
|
U |
470 |
/* --------- ZIP entpacken ---------------- */ |
|
471 |
|
6bd2c1
|
472 |
public String extractZipfile(String relPath, String filename) { |
eb2a2d
|
473 |
String result = null; |
U |
474 |
if (!relPath.startsWith(".")) { |
|
475 |
try { |
|
476 |
File targetDir = getTargetDir(relPath); |
|
477 |
File archive = new File(targetDir, filename); |
|
478 |
if(extract(archive)) { |
|
479 |
result = "ok"; |
|
480 |
} else { |
|
481 |
result = "error while extracting"; |
|
482 |
} |
|
483 |
} catch(Exception ex) { |
|
484 |
result = ex.getLocalizedMessage(); |
|
485 |
logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
6bd2c1
|
486 |
} |
U |
487 |
} |
|
488 |
return result; |
|
489 |
} |
|
490 |
|
|
491 |
/** |
|
492 |
* extract a given ZIP archive to the folder respective archive resides in |
|
493 |
* @param archive the archive to extract |
|
494 |
* @throws Exception |
|
495 |
*/ |
|
496 |
private boolean extract(File archive) throws Exception { |
|
497 |
ZipFile zipfile = new ZipFile(archive); |
|
498 |
Enumeration en = zipfile.entries(); |
|
499 |
while(en.hasMoreElements()) { |
|
500 |
ZipEntry zipentry = (ZipEntry) en.nextElement(); |
|
501 |
unzip(zipfile, zipentry, archive.getParent()); |
|
502 |
} |
|
503 |
zipfile.close(); |
|
504 |
return true; |
|
505 |
} |
|
506 |
|
|
507 |
/** |
|
508 |
* unzip a given entry of a given zip file to a given location |
|
509 |
* @param zipfile the zip file to read an entry from |
|
510 |
* @param zipentry the zip entry to read |
|
511 |
* @param destPath the path to the destination location for the extracted content |
|
512 |
* @throws IOException |
|
513 |
*/ |
|
514 |
private void unzip(ZipFile zipfile, ZipEntry zipentry, String destPath) throws IOException { |
|
515 |
byte buf[] = new byte[1024]; |
|
516 |
InputStream is = zipfile.getInputStream(zipentry); |
|
517 |
String outFileName = destPath + File.separator + zipentry.getName(); |
|
518 |
File file = new File(outFileName); |
|
519 |
if(!zipentry.isDirectory()) { |
|
520 |
file.getParentFile().mkdirs(); |
|
521 |
if(!file.exists()) |
|
522 |
file.createNewFile(); |
|
523 |
FileOutputStream fos = new FileOutputStream(file); |
|
524 |
int i = is.read(buf, 0, 1024); |
|
525 |
while(i > -1) { |
|
526 |
fos.write(buf, 0, i); |
|
527 |
i = is.read(buf, 0, 1024); |
|
528 |
} |
|
529 |
fos.close(); |
|
530 |
is.close(); |
|
531 |
} else { |
|
532 |
file.mkdirs(); |
|
533 |
} |
|
534 |
} |
|
535 |
|
af9930
|
536 |
/* ------------- Ornder als ZIP packen --------------- */ |
U |
537 |
|
|
538 |
public String packFolder(String relPath) { |
|
539 |
if (!relPath.startsWith(".")) { |
|
540 |
try { |
|
541 |
File targetDir = getTargetDir(relPath); |
|
542 |
File parentDir = targetDir.getParentFile(); |
|
543 |
StringBuffer fname = new StringBuffer(); |
|
544 |
fname.append(targetDir.getName()); |
|
545 |
fname.append(".zip"); |
|
546 |
File archiveFile = new File(parentDir, fname.toString()); |
|
547 |
FileRef folderToPack = new FileRef(targetDir.getAbsolutePath()); |
|
548 |
FileRef archive = new FileRef(archiveFile.getAbsolutePath()); |
|
549 |
pack(folderToPack, archive); |
|
550 |
return "ok"; |
|
551 |
} catch(Exception ex) { |
|
552 |
String result = ex.getLocalizedMessage(); |
|
553 |
logger.log(Level.SEVERE, result, ex); |
|
554 |
return result; |
|
555 |
} |
|
556 |
} else { |
|
557 |
return "Falsche relative Pfadangabe"; |
|
558 |
} |
|
559 |
} |
|
560 |
|
|
561 |
/** |
|
562 |
* pack the contents of a given folder into a new ZIP compressed archive |
|
563 |
* @param folder the folder to pack |
|
564 |
* @param archive the archive to create from the given files |
|
565 |
* @throws Exception |
|
566 |
*/ |
|
567 |
private boolean pack(FileRef folder, FileRef archive) throws Exception { |
|
568 |
File file = new File(archive.getAbsolutePath()); |
|
569 |
FileOutputStream fos = new FileOutputStream(file); |
|
570 |
CheckedOutputStream checksum = new CheckedOutputStream(fos, new Adler32()); |
|
571 |
ZipOutputStream zos = new ZipOutputStream(checksum); |
|
572 |
pack(zos, folder.getAbsolutePath(), ""); |
|
573 |
zos.flush(); |
|
574 |
zos.finish(); |
|
575 |
zos.close(); |
|
576 |
fos.flush(); |
|
577 |
fos.close(); |
|
578 |
return true; |
|
579 |
} |
|
580 |
|
|
581 |
/** |
|
582 |
* go through the given file structure recursively |
|
583 |
* @param zipFile the ZIP file to write to |
|
584 |
* @param srcDir the directory to pack during this cycle |
|
585 |
* @param subDir the subdirectory to append to names of file entries inside the archive |
|
586 |
* @throws IOException |
|
587 |
*/ |
|
588 |
private void pack(ZipOutputStream zipFile, String srcDir, String subDir) throws IOException { |
|
589 |
File[] files = new File(srcDir).listFiles(); |
|
590 |
for(int i = 0; i < files.length; i++) { |
|
591 |
if(files[i].isDirectory()) { |
|
592 |
pack(zipFile, files[i].getAbsolutePath(), subDir + File.separator + files[i].getName()); |
|
593 |
} |
|
594 |
else { |
|
595 |
packFile(zipFile, subDir, files[i]); |
|
596 |
} |
|
597 |
} |
|
598 |
} |
|
599 |
|
|
600 |
/** |
|
601 |
* pack a given file |
|
602 |
* @param zipFile the ZIP archive to pack to |
|
603 |
* @param dir the directory name to append to name of file entry inside archive |
|
604 |
* @param file the file to pack |
|
605 |
* @throws IOException |
|
606 |
*/ |
|
607 |
private void packFile(ZipOutputStream zipFile, String dir, File file) throws IOException |
|
608 |
{ |
|
609 |
FileInputStream fileinputstream = new FileInputStream(file); |
|
610 |
byte buf[] = new byte[fileinputstream.available()]; |
|
611 |
fileinputstream.read(buf); |
|
612 |
String dirWithSlashes = dir.replace('\\', '/'); |
|
613 |
//System.out.println("zipping " + dirWithSlashes + "/" + file.getName()); |
|
614 |
ZipEntry ze = new ZipEntry(dirWithSlashes + "/" + file.getName()); |
|
615 |
ze.setMethod(ZipEntry.DEFLATED); |
|
616 |
zipFile.putNextEntry(ze); |
|
617 |
zipFile.write(buf, 0, buf.length); |
|
618 |
zipFile.closeEntry(); |
|
619 |
fileinputstream.close(); |
|
620 |
} |
|
621 |
|
6bd2c1
|
622 |
|
438b16
|
623 |
|
5efd94
|
624 |
/* ---- Hilfsfunktionen ---- */ |
ea7193
|
625 |
|
547755
|
626 |
} |