commit | author | age
|
e369b9
|
1 |
/* |
c45b52
|
2 |
fm - File management class library |
e369b9
|
3 |
Copyright (C) 2024 Ulrich Hilger |
U |
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 <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.fm; |
|
19 |
|
|
20 |
import java.io.File; |
|
21 |
import java.io.IOException; |
|
22 |
import java.nio.file.DirectoryStream; |
|
23 |
import java.nio.file.Files; |
|
24 |
import java.nio.file.Path; |
|
25 |
import java.util.List; |
|
26 |
|
|
27 |
/** |
|
28 |
* Eine Klasse mit Methoden zum Loeschen von Dateien |
|
29 |
* |
|
30 |
* @author Ulrich Hilger, 15. Januar 2024 |
|
31 |
*/ |
|
32 |
public class Eraser { |
973951
|
33 |
|
U |
34 |
public static final int OP_DELETE = 3; |
|
35 |
|
e369b9
|
36 |
public String deleteFiles(String relPath, List<String> fileNames, String base) { |
U |
37 |
String result = null; |
|
38 |
try { |
|
39 |
//logger.fine(fileNames.toString()); |
|
40 |
if (!relPath.startsWith(Const.STR_DOT)) { |
|
41 |
File targetDir = new File(base, relPath); // getTargetDir(relPath); |
|
42 |
//logger.fine("targetDir: " + targetDir); |
|
43 |
for (String fileName : fileNames) { |
|
44 |
File targetFile = new File(targetDir, fileName); |
|
45 |
//logger.fine(targetFile.getAbsolutePath()); |
|
46 |
if (targetFile.isDirectory()) { |
c45b52
|
47 |
CopyMoveVisitor bearbeiter = new CopyMoveVisitor(); |
973951
|
48 |
bearbeiter.setOperation(OP_DELETE); |
e369b9
|
49 |
Files.walkFileTree(targetFile.toPath(), bearbeiter); |
U |
50 |
} else { |
|
51 |
/* |
|
52 |
Wenn targetFile mit jpg, jpeg oder png endet, |
|
53 |
muss eine Unterfunktion eine Liste aller Dateien bilden, |
|
54 |
die so heissen, also z.B. alle [Dateiname]*.jpg |
|
55 |
*/ |
|
56 |
String fname = targetFile.getName().toLowerCase(); |
973951
|
57 |
if (fname.endsWith(ImageFileFilter.JPEG) |
U |
58 |
|| fname.endsWith(ImageFileFilter.JPG) |
|
59 |
|| fname.endsWith(ImageFileFilter.PNG)) { |
e369b9
|
60 |
deleteImgFiles(targetDir, targetFile); |
U |
61 |
} else { |
|
62 |
targetFile.delete(); |
|
63 |
} |
|
64 |
} |
|
65 |
} |
|
66 |
result = "deleted"; |
|
67 |
} |
|
68 |
} catch (IOException ex) { |
|
69 |
//logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
70 |
} |
|
71 |
return result; |
|
72 |
} |
|
73 |
|
|
74 |
private void deleteImgFiles(File targetDir, File targetFile) throws IOException { |
|
75 |
String fnameext = targetFile.getName(); |
|
76 |
int dotpos = fnameext.lastIndexOf(Const.STR_DOT); |
|
77 |
String fname = fnameext.substring(0, dotpos); |
|
78 |
String ext = fnameext.substring(dotpos); |
|
79 |
//logger.fine("fname: " + fname + ", ext: " + ext); |
|
80 |
DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir.toPath(), fname + "*" + ext); //"*.{txt,doc,pdf,ppt}" |
|
81 |
for (Path path : stream) { |
|
82 |
//logger.fine(path.getFileName().toString()); |
|
83 |
Files.delete(path); |
|
84 |
} |
|
85 |
stream.close(); |
|
86 |
} |
|
87 |
|
|
88 |
} |