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