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