/*
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 .
*/
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;
/**
* Umbenennen von Dateien
*
* @author Ulrich Hilger, 15. Januar 2024
*/
public class Renamer {
public static final String STR_DOT = ".";
/**
* Eine Datei oder einen Ordner umbenennen
*
* @param neuerName der Name fuer das Objekt
* @param file das Objekt, das umbenannt werden soll
* @return der neue Name
* @throws IOException wenn etwas schief geht
*/
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, neuerName);
} else {
neueDatei = new File(file.getParentFile(), neuerName);
file.renameTo(neueDatei);
}
return neueDatei.getName();
}
/**
* Alle Varianten einer Bilddatei umbenennen
*
* TODO einen sinnvolleren Rueckgabewert bauen
*
* @param file die Origianlbilddatei
* @param newName der neue Name
* @return Name der letzten Datei, die umbenannt wurde
* @throws IOException wenn etwas schief geht
*/
public File renameImgFiles(/*File targetDir, */ File file, String newName) throws IOException {
String alt;
String neu;
File neueDatei = file;
File targetDir = file.getParentFile();
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 = file.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 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;
}
}