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