commit | author | age
|
e369b9
|
1 |
/* |
U |
2 |
neon-fm - File management extensions to Neon |
|
3 |
Copyright (C) 2024 Ulrich Hilger |
|
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 { |
|
33 |
public String deleteFiles(String relPath, List<String> fileNames, String base) { |
|
34 |
String result = null; |
|
35 |
try { |
|
36 |
//logger.fine(fileNames.toString()); |
|
37 |
if (!relPath.startsWith(Const.STR_DOT)) { |
|
38 |
File targetDir = new File(base, relPath); // getTargetDir(relPath); |
|
39 |
//logger.fine("targetDir: " + targetDir); |
|
40 |
for (String fileName : fileNames) { |
|
41 |
File targetFile = new File(targetDir, fileName); |
|
42 |
//logger.fine(targetFile.getAbsolutePath()); |
|
43 |
if (targetFile.isDirectory()) { |
|
44 |
OrdnerBearbeiter bearbeiter = new OrdnerBearbeiter(); |
|
45 |
bearbeiter.setOperation(Const.OP_DELETE); |
|
46 |
Files.walkFileTree(targetFile.toPath(), bearbeiter); |
|
47 |
} else { |
|
48 |
/* |
|
49 |
Wenn targetFile mit jpg, jpeg oder png endet, |
|
50 |
muss eine Unterfunktion eine Liste aller Dateien bilden, |
|
51 |
die so heissen, also z.B. alle [Dateiname]*.jpg |
|
52 |
*/ |
|
53 |
String fname = targetFile.getName().toLowerCase(); |
|
54 |
if (fname.endsWith(Const.JPEG) |
|
55 |
|| fname.endsWith(Const.JPG) |
|
56 |
|| fname.endsWith(Const.PNG)) { |
|
57 |
deleteImgFiles(targetDir, targetFile); |
|
58 |
} else { |
|
59 |
targetFile.delete(); |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
result = "deleted"; |
|
64 |
} |
|
65 |
} catch (IOException ex) { |
|
66 |
//logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
|
67 |
} |
|
68 |
return result; |
|
69 |
} |
|
70 |
|
|
71 |
private void deleteImgFiles(File targetDir, File targetFile) throws IOException { |
|
72 |
String fnameext = targetFile.getName(); |
|
73 |
int dotpos = fnameext.lastIndexOf(Const.STR_DOT); |
|
74 |
String fname = fnameext.substring(0, dotpos); |
|
75 |
String ext = fnameext.substring(dotpos); |
|
76 |
//logger.fine("fname: " + fname + ", ext: " + ext); |
|
77 |
DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir.toPath(), fname + "*" + ext); //"*.{txt,doc,pdf,ppt}" |
|
78 |
for (Path path : stream) { |
|
79 |
//logger.fine(path.getFileName().toString()); |
|
80 |
Files.delete(path); |
|
81 |
} |
|
82 |
stream.close(); |
|
83 |
} |
|
84 |
|
|
85 |
} |