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 |
|
|
26 |
/** |
e8f9a4
|
27 |
* Umbenennen von Dateien |
e369b9
|
28 |
* |
U |
29 |
* @author Ulrich Hilger, 15. Januar 2024 |
|
30 |
*/ |
|
31 |
public class Renamer { |
|
32 |
|
|
33 |
public static final String STR_DOT = "."; |
|
34 |
|
e8f9a4
|
35 |
/** |
U |
36 |
* Eine Datei oder einen Ordner umbenennen |
|
37 |
* |
|
38 |
* @param neuerName der Name fuer das Objekt |
|
39 |
* @param file das Objekt, das umbenannt werden soll |
|
40 |
* @return der neue Name |
|
41 |
* @throws IOException wenn etwas schief geht |
|
42 |
*/ |
|
43 |
public String rename(/*HttpExchange exchange, *//*String relPfad,*/ String neuerName, File file) |
973951
|
44 |
throws IOException { |
e369b9
|
45 |
File neueDatei; |
U |
46 |
String fname = file.getName().toLowerCase(); |
973951
|
47 |
if(fname.endsWith(ImageFileFilter.JPEG) || fname.endsWith(ImageFileFilter.JPG) || |
U |
48 |
fname.endsWith(ImageFileFilter.PNG)) { |
e8f9a4
|
49 |
neueDatei = renameImgFiles(file, neuerName); |
e369b9
|
50 |
} else { |
U |
51 |
neueDatei = new File(file.getParentFile(), neuerName); |
|
52 |
file.renameTo(neueDatei); |
|
53 |
} |
|
54 |
return neueDatei.getName(); |
|
55 |
} |
|
56 |
|
e8f9a4
|
57 |
/** |
U |
58 |
* Alle Varianten einer Bilddatei umbenennen |
|
59 |
* |
|
60 |
* TODO einen sinnvolleren Rueckgabewert bauen |
|
61 |
* |
|
62 |
* @param file die Origianlbilddatei |
|
63 |
* @param newName der neue Name |
|
64 |
* @return Name der letzten Datei, die umbenannt wurde |
|
65 |
* @throws IOException wenn etwas schief geht |
|
66 |
*/ |
|
67 |
public File renameImgFiles(/*File targetDir, */ File file, String newName) throws IOException { |
e369b9
|
68 |
String alt; |
U |
69 |
String neu; |
e8f9a4
|
70 |
File neueDatei = file; |
e369b9
|
71 |
|
e8f9a4
|
72 |
File targetDir = file.getParentFile(); |
e369b9
|
73 |
int newdotpos = newName.lastIndexOf(STR_DOT); |
U |
74 |
String newfname = newName.substring(0, newdotpos); |
|
75 |
String newext = newName.substring(newdotpos); |
|
76 |
//logger.fine("newfname: " + newfname + ", newext: " + newext); |
|
77 |
|
e8f9a4
|
78 |
String fnameext = file.getName(); |
e369b9
|
79 |
int dotpos = fnameext.lastIndexOf(STR_DOT); |
U |
80 |
String fname = fnameext.substring(0, dotpos); |
|
81 |
String ext = fnameext.substring(dotpos); |
|
82 |
//logger.fine("fname: " + fname + ", ext: " + ext); |
|
83 |
|
973951
|
84 |
DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir.toPath(), fname + "*" + ext); |
e369b9
|
85 |
for (Path path : stream) { |
U |
86 |
//logger.fine(path.getFileName().toString()); |
|
87 |
alt = path.getFileName().toString(); |
|
88 |
//logger.fine("alt: " + alt); |
973951
|
89 |
if(alt.contains(ImageFileFilter.TN)) { |
U |
90 |
neu = newfname + ImageFileFilter.TN + newext; |
|
91 |
} else if (alt.contains(ImageFileFilter.KL)) { |
|
92 |
neu = newfname + ImageFileFilter.KL + newext; |
|
93 |
} else if(alt.contains(ImageFileFilter.GR)) { |
|
94 |
neu = newfname + ImageFileFilter.GR + newext; |
|
95 |
} else if(alt.contains(ImageFileFilter.MT)) { |
|
96 |
neu = newfname + ImageFileFilter.MT + newext; |
|
97 |
} else if(alt.contains(ImageFileFilter.SM)) { |
|
98 |
neu = newfname + ImageFileFilter.SM + newext; |
e369b9
|
99 |
} else { |
U |
100 |
neu = newName; |
|
101 |
} |
|
102 |
neueDatei = new File(targetDir, neu); |
|
103 |
path.toFile().renameTo(neueDatei); |
|
104 |
} |
|
105 |
stream.close(); |
|
106 |
return neueDatei; |
|
107 |
} |
e8f9a4
|
108 |
} |