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; |
b6536f
|
26 |
import java.util.logging.Level; |
U |
27 |
import java.util.logging.Logger; |
e369b9
|
28 |
|
U |
29 |
/** |
b6536f
|
30 |
* Loeschen von Dateien und Ordnern |
e369b9
|
31 |
* |
U |
32 |
* @author Ulrich Hilger, 15. Januar 2024 |
|
33 |
*/ |
|
34 |
public class Eraser { |
973951
|
35 |
|
U |
36 |
public static final int OP_DELETE = 3; |
|
37 |
|
8f4ae9
|
38 |
private final String STR_DOT = "."; |
U |
39 |
|
b6536f
|
40 |
/** |
U |
41 |
* Dateien und Ordner loeschen |
|
42 |
* |
|
43 |
* @param relPath der relative Pfad, der gemeinsam mit 'base' den absoluten |
|
44 |
* Pfad zum Ordner ergibt, der die zu loeschenden Dateien enthaelt |
|
45 |
* @param fileNames Liste mit Namen von Dateien und Ordnern, die geloescht werden sollen |
|
46 |
* @param base absoluter Basispfad |
|
47 |
* @return "deleted" oder null, wenn ein Fehler auftrat |
|
48 |
*/ |
e369b9
|
49 |
public String deleteFiles(String relPath, List<String> fileNames, String base) { |
U |
50 |
String result = null; |
|
51 |
try { |
|
52 |
//logger.fine(fileNames.toString()); |
8f4ae9
|
53 |
if (!relPath.startsWith(STR_DOT)) { |
e369b9
|
54 |
File targetDir = new File(base, relPath); // getTargetDir(relPath); |
U |
55 |
//logger.fine("targetDir: " + targetDir); |
|
56 |
for (String fileName : fileNames) { |
|
57 |
File targetFile = new File(targetDir, fileName); |
|
58 |
//logger.fine(targetFile.getAbsolutePath()); |
|
59 |
if (targetFile.isDirectory()) { |
a11f6c
|
60 |
FileOpsVisitor bearbeiter = new FileOpsVisitor(); |
973951
|
61 |
bearbeiter.setOperation(OP_DELETE); |
e369b9
|
62 |
Files.walkFileTree(targetFile.toPath(), bearbeiter); |
U |
63 |
} else { |
|
64 |
/* |
|
65 |
Wenn targetFile mit jpg, jpeg oder png endet, |
|
66 |
muss eine Unterfunktion eine Liste aller Dateien bilden, |
|
67 |
die so heissen, also z.B. alle [Dateiname]*.jpg |
|
68 |
*/ |
|
69 |
String fname = targetFile.getName().toLowerCase(); |
973951
|
70 |
if (fname.endsWith(ImageFileFilter.JPEG) |
U |
71 |
|| fname.endsWith(ImageFileFilter.JPG) |
|
72 |
|| fname.endsWith(ImageFileFilter.PNG)) { |
e369b9
|
73 |
deleteImgFiles(targetDir, targetFile); |
U |
74 |
} else { |
|
75 |
targetFile.delete(); |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
result = "deleted"; |
|
80 |
} |
|
81 |
} catch (IOException ex) { |
b6536f
|
82 |
Logger.getLogger(Eraser.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); |
e369b9
|
83 |
} |
U |
84 |
return result; |
|
85 |
} |
|
86 |
|
b6536f
|
87 |
/** |
U |
88 |
* Alle Varianten einer Bilddatei loeschen |
|
89 |
* |
|
90 |
* @param targetDir der Ordner, aus dem geloescht werden soll |
|
91 |
* @param targetFile Bilddatei, deren Varianten geloescht werden sollen |
|
92 |
* @throws IOException wenn etwas schief geht |
|
93 |
*/ |
e369b9
|
94 |
private void deleteImgFiles(File targetDir, File targetFile) throws IOException { |
U |
95 |
String fnameext = targetFile.getName(); |
8f4ae9
|
96 |
int dotpos = fnameext.lastIndexOf(STR_DOT); |
e369b9
|
97 |
String fname = fnameext.substring(0, dotpos); |
U |
98 |
String ext = fnameext.substring(dotpos); |
|
99 |
//logger.fine("fname: " + fname + ", ext: " + ext); |
|
100 |
DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir.toPath(), fname + "*" + ext); //"*.{txt,doc,pdf,ppt}" |
|
101 |
for (Path path : stream) { |
|
102 |
//logger.fine(path.getFileName().toString()); |
|
103 |
Files.delete(path); |
|
104 |
} |
|
105 |
stream.close(); |
|
106 |
} |
|
107 |
|
|
108 |
} |