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