/*
|
neon-fm - File management extensions to Neon
|
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 com.sun.net.httpserver.HttpExchange;
|
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 umbenennen(HttpExchange exchange, String relPfad, String neuerName, File file) throws IOException {
|
File neueDatei;
|
//String relPfad = helper.getFileName(exchange);
|
//File file = new File(exchange.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(), relPfad);
|
String fname = file.getName().toLowerCase();
|
if(fname.endsWith(Const.JPEG) || fname.endsWith(Const.JPG) || fname.endsWith(Const.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); //"*.{txt,doc,pdf,ppt}"
|
for (Path path : stream) {
|
//logger.fine(path.getFileName().toString());
|
alt = path.getFileName().toString();
|
//logger.fine("alt: " + alt);
|
if(alt.contains(Const.TN)) {
|
neu = newfname + Const.TN + newext;
|
} else if (alt.contains(Const.KL)) {
|
neu = newfname + Const.KL + newext;
|
} else if(alt.contains(Const.GR)) {
|
neu = newfname + Const.GR + newext;
|
} else if(alt.contains(Const.MT)) {
|
neu = newfname + Const.MT + newext;
|
} else if(alt.contains(Const.SM)) {
|
neu = newfname + Const.SM + newext;
|
} else {
|
neu = newName;
|
}
|
neueDatei = new File(targetDir, neu);
|
path.toFile().renameTo(neueDatei);
|
}
|
stream.close();
|
return neueDatei;
|
}
|
|
|
|
}
|