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