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 com.sun.net.httpserver.HttpExchange; |
|
21 |
import java.io.File; |
|
22 |
import java.io.IOException; |
|
23 |
import java.nio.file.DirectoryStream; |
|
24 |
import java.nio.file.Files; |
|
25 |
import java.nio.file.Path; |
|
26 |
|
|
27 |
/** |
|
28 |
* Eine Klasse mit Methoden zum Umbenennen von Dateien |
|
29 |
* |
|
30 |
* @author Ulrich Hilger, 15. Januar 2024 |
|
31 |
*/ |
|
32 |
public class Renamer { |
|
33 |
|
|
34 |
public static final String STR_DOT = "."; |
|
35 |
|
|
36 |
public String umbenennen(HttpExchange exchange, String relPfad, String neuerName, File file) throws IOException { |
|
37 |
File neueDatei; |
|
38 |
//String relPfad = helper.getFileName(exchange); |
|
39 |
//File file = new File(exchange.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(), relPfad); |
|
40 |
String fname = file.getName().toLowerCase(); |
|
41 |
if(fname.endsWith(Const.JPEG) || fname.endsWith(Const.JPG) || fname.endsWith(Const.PNG)) { |
|
42 |
neueDatei = renameImgFiles(file.getParentFile(), file, neuerName); |
|
43 |
} else { |
|
44 |
neueDatei = new File(file.getParentFile(), neuerName); |
|
45 |
file.renameTo(neueDatei); |
|
46 |
} |
|
47 |
return neueDatei.getName(); |
|
48 |
} |
|
49 |
|
|
50 |
public File renameImgFiles(File targetDir, File targetFile, String newName) throws IOException { |
|
51 |
String alt; |
|
52 |
String neu; |
|
53 |
File neueDatei = targetFile; |
|
54 |
|
|
55 |
int newdotpos = newName.lastIndexOf(STR_DOT); |
|
56 |
String newfname = newName.substring(0, newdotpos); |
|
57 |
String newext = newName.substring(newdotpos); |
|
58 |
//logger.fine("newfname: " + newfname + ", newext: " + newext); |
|
59 |
|
|
60 |
String fnameext = targetFile.getName(); |
|
61 |
int dotpos = fnameext.lastIndexOf(STR_DOT); |
|
62 |
String fname = fnameext.substring(0, dotpos); |
|
63 |
String ext = fnameext.substring(dotpos); |
|
64 |
//logger.fine("fname: " + fname + ", ext: " + ext); |
|
65 |
|
|
66 |
DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir.toPath(), fname + "*" + ext); //"*.{txt,doc,pdf,ppt}" |
|
67 |
for (Path path : stream) { |
|
68 |
//logger.fine(path.getFileName().toString()); |
|
69 |
alt = path.getFileName().toString(); |
|
70 |
//logger.fine("alt: " + alt); |
|
71 |
if(alt.contains(Const.TN)) { |
|
72 |
neu = newfname + Const.TN + newext; |
|
73 |
} else if (alt.contains(Const.KL)) { |
|
74 |
neu = newfname + Const.KL + newext; |
|
75 |
} else if(alt.contains(Const.GR)) { |
|
76 |
neu = newfname + Const.GR + newext; |
|
77 |
} else if(alt.contains(Const.MT)) { |
|
78 |
neu = newfname + Const.MT + newext; |
|
79 |
} else if(alt.contains(Const.SM)) { |
|
80 |
neu = newfname + Const.SM + newext; |
|
81 |
} else { |
|
82 |
neu = newName; |
|
83 |
} |
|
84 |
neueDatei = new File(targetDir, neu); |
|
85 |
path.toFile().renameTo(neueDatei); |
|
86 |
} |
|
87 |
stream.close(); |
|
88 |
return neueDatei; |
|
89 |
} |
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
} |