/* http-cm - File management extensions to jdk.httpserver Copyright (C) 2021 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.httpserver.cm.actor; import com.sun.net.httpserver.HttpExchange; import de.uhilger.httpserver.base.HttpHelper; import de.uhilger.httpserver.base.handler.FileHandler; import static de.uhilger.httpserver.cm.FileManager.STR_DOT; import de.uhilger.httpserver.image.ImageActor; 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 String umbenennen(HttpExchange exchange, HttpHelper helper, String neuerName) 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(ImageActor.JPEG) || fname.endsWith(ImageActor.JPG) || fname.endsWith(ImageActor.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 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(ImageActor.TN)) { neu = newfname + ImageActor.TN + newext; } else if (alt.contains(ImageActor.KL)) { neu = newfname + ImageActor.KL + newext; } else if(alt.contains(ImageActor.GR)) { neu = newfname + ImageActor.GR + newext; } else if(alt.contains(ImageActor.MT)) { neu = newfname + ImageActor.MT + newext; } else if(alt.contains(ImageActor.SM)) { neu = newfname + ImageActor.SM + newext; } else { neu = newName; } neueDatei = new File(targetDir, neu); path.toFile().renameTo(neueDatei); } stream.close(); return neueDatei; } }