/*
|
fm - File management class library
|
Copyright (C) 2024 Ulrich Hilger
|
|
This program is free software: you can redistribute it and/or modify
|
it under the terms of the GNU Affero General Public License as
|
published by the Free Software Foundation, either version 3 of the
|
License, or (at your option) any later version.
|
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU Affero General Public License for more details.
|
|
You should have received a copy of the GNU Affero General Public License
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
*/
|
package de.uhilger.fm;
|
|
import java.io.File;
|
import java.io.IOException;
|
import java.nio.file.DirectoryStream;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
|
/**
|
* Eine Klasse mit Methoden zum Umbenennen von Dateien
|
*
|
* @author Ulrich Hilger, 15. Januar 2024
|
*/
|
public class Renamer {
|
|
public static final String STR_DOT = ".";
|
|
public String rename(/*HttpExchange exchange, */String relPfad, String neuerName, File file)
|
throws IOException {
|
File neueDatei;
|
String fname = file.getName().toLowerCase();
|
if(fname.endsWith(ImageFileFilter.JPEG) || fname.endsWith(ImageFileFilter.JPG) ||
|
fname.endsWith(ImageFileFilter.PNG)) {
|
neueDatei = renameImgFiles(file.getParentFile(), file, neuerName);
|
} else {
|
neueDatei = new File(file.getParentFile(), neuerName);
|
file.renameTo(neueDatei);
|
}
|
return neueDatei.getName();
|
}
|
|
public File renameImgFiles(File targetDir, File targetFile, String newName) throws IOException {
|
String alt;
|
String neu;
|
File neueDatei = targetFile;
|
|
int newdotpos = newName.lastIndexOf(STR_DOT);
|
String newfname = newName.substring(0, newdotpos);
|
String newext = newName.substring(newdotpos);
|
//logger.fine("newfname: " + newfname + ", newext: " + newext);
|
|
String fnameext = targetFile.getName();
|
int dotpos = fnameext.lastIndexOf(STR_DOT);
|
String fname = fnameext.substring(0, dotpos);
|
String ext = fnameext.substring(dotpos);
|
//logger.fine("fname: " + fname + ", ext: " + ext);
|
|
DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir.toPath(), fname + "*" + ext);
|
for (Path path : stream) {
|
//logger.fine(path.getFileName().toString());
|
alt = path.getFileName().toString();
|
//logger.fine("alt: " + alt);
|
if(alt.contains(ImageFileFilter.TN)) {
|
neu = newfname + ImageFileFilter.TN + newext;
|
} else if (alt.contains(ImageFileFilter.KL)) {
|
neu = newfname + ImageFileFilter.KL + newext;
|
} else if(alt.contains(ImageFileFilter.GR)) {
|
neu = newfname + ImageFileFilter.GR + newext;
|
} else if(alt.contains(ImageFileFilter.MT)) {
|
neu = newfname + ImageFileFilter.MT + newext;
|
} else if(alt.contains(ImageFileFilter.SM)) {
|
neu = newfname + ImageFileFilter.SM + newext;
|
} else {
|
neu = newName;
|
}
|
neueDatei = new File(targetDir, neu);
|
path.toFile().renameTo(neueDatei);
|
}
|
stream.close();
|
return neueDatei;
|
}
|
|
|
|
}
|