From 8e6840f2eaefe0f7b4763acd5edace0187bf29d3 Mon Sep 17 00:00:00 2001 From: ulrich Date: Mon, 15 Jan 2024 16:52:27 +0000 Subject: [PATCH] Umbenennen von FileManager in Klasse Renamer, Loeschen in Eraser verlegt --- src/de/uhilger/httpserver/cm/actor/Zipper.java | 24 +++ src/de/uhilger/httpserver/cm/actor/Eraser.java | 89 ++++++++++++++ src/de/uhilger/httpserver/cm/actor/Renamer.java | 94 +++++++++++++++ src/de/uhilger/httpserver/cm/FileManager.java | 119 +------------------ src/de/uhilger/httpserver/cm/actor/Unzipper.java | 24 +++ 5 files changed, 231 insertions(+), 119 deletions(-) diff --git a/src/de/uhilger/httpserver/cm/FileManager.java b/src/de/uhilger/httpserver/cm/FileManager.java index ea70988..f40b401 100644 --- a/src/de/uhilger/httpserver/cm/FileManager.java +++ b/src/de/uhilger/httpserver/cm/FileManager.java @@ -17,6 +17,10 @@ */ package de.uhilger.httpserver.cm; +import de.uhilger.httpserver.cm.actor.Zipper; +import de.uhilger.httpserver.cm.actor.Eraser; +import de.uhilger.httpserver.cm.actor.Unzipper; +import de.uhilger.httpserver.cm.actor.Renamer; import com.google.gson.Gson; import com.sun.net.httpserver.Authenticator; import com.sun.net.httpserver.Headers; @@ -194,7 +198,7 @@ } break; case P_RENAME: - String neuerDateiName = umbenennen(exchange, helper, params[1]); + String neuerDateiName = new Renamer().umbenennen(exchange, helper, params[1]); //logger.fine("neuer Name: " + neuerDateiName); standardHeaderUndAntwort(exchange, SC_OK, neuerDateiName); break; @@ -502,7 +506,7 @@ private void loeschen(HttpExchange exchange, HttpHelper helper) throws IOException { String[] dateiNamen = dateiliste(exchange); String relPfad = helper.getFileName(exchange); - deleteFiles(relPfad, Arrays.asList(dateiNamen), exchange); + new Eraser().deleteFiles(relPfad, Arrays.asList(dateiNamen), exchange.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString()); standardHeaderUndAntwort(exchange, SC_OK, "Dateien geloescht."); } @@ -530,113 +534,6 @@ return destFile.getName(); } - 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<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(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; - } - - - private String deleteFiles(String relPath, List<String> fileNames, HttpExchange e) { - String result = null; - try { - //logger.fine(fileNames.toString()); - if (!relPath.startsWith(STR_DOT)) { - File targetDir = new File(e.getHttpContext().getAttributes().get(FileHandler.ATTR_FILE_BASE).toString(), relPath); // getTargetDir(relPath); - //logger.fine("targetDir: " + targetDir); - for (String fileName : fileNames) { - File targetFile = new File(targetDir, fileName); - //logger.fine(targetFile.getAbsolutePath()); - if (targetFile.isDirectory()) { - OrdnerBearbeiter bearbeiter = new OrdnerBearbeiter(); - bearbeiter.setOperation(OP_DELETE); - Files.walkFileTree(targetFile.toPath(), bearbeiter); - } else { - /* - Wenn targetFile mit jpg, jpeg oder png endet, - muss eine Unterfunktion eine Liste aller Dateien bilden, - die so heissen, also z.B. alle [Dateiname]*.jpg - */ - String fname = targetFile.getName().toLowerCase(); - if (fname.endsWith(ImageActor.JPEG) - || fname.endsWith(ImageActor.JPG) - || fname.endsWith(ImageActor.PNG)) { - deleteImgFiles(targetDir, targetFile); - } else { - targetFile.delete(); - } - } - } - result = "deleted"; - } - } catch (Throwable ex) { - //logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); - } - return result; - } - - private void deleteImgFiles(File targetDir, File targetFile) throws IOException { - 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()); - Files.delete(path); - } - stream.close(); - } private void moveImgFilesToDirectory(File srcFile, File srcDir, File targetDir, boolean createDestDir) throws IOException { String fnameext = srcFile.getName(); @@ -659,8 +556,4 @@ resHeaders.add(CONTENT_TYPE, HttpHelper.CT_TEXT_HTML); new HttpResponder().antwortSenden(exchange, status, antwort); } - - - - } diff --git a/src/de/uhilger/httpserver/cm/actor/Eraser.java b/src/de/uhilger/httpserver/cm/actor/Eraser.java new file mode 100644 index 0000000..a485a69 --- /dev/null +++ b/src/de/uhilger/httpserver/cm/actor/Eraser.java @@ -0,0 +1,89 @@ +/* + 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 <https://www.gnu.org/licenses/>. + */ +package de.uhilger.httpserver.cm.actor; + +import de.uhilger.httpserver.cm.OrdnerBearbeiter; +import static de.uhilger.httpserver.cm.FileManager.OP_DELETE; +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; +import java.util.List; + +/** + * Eine Klasse mit Methoden zum Loeschen von Dateien + * + * @author Ulrich Hilger, 15. Januar 2024 + */ +public class Eraser { + public String deleteFiles(String relPath, List<String> fileNames, String base) { + String result = null; + try { + //logger.fine(fileNames.toString()); + if (!relPath.startsWith(STR_DOT)) { + File targetDir = new File(base, relPath); // getTargetDir(relPath); + //logger.fine("targetDir: " + targetDir); + for (String fileName : fileNames) { + File targetFile = new File(targetDir, fileName); + //logger.fine(targetFile.getAbsolutePath()); + if (targetFile.isDirectory()) { + OrdnerBearbeiter bearbeiter = new OrdnerBearbeiter(); + bearbeiter.setOperation(OP_DELETE); + Files.walkFileTree(targetFile.toPath(), bearbeiter); + } else { + /* + Wenn targetFile mit jpg, jpeg oder png endet, + muss eine Unterfunktion eine Liste aller Dateien bilden, + die so heissen, also z.B. alle [Dateiname]*.jpg + */ + String fname = targetFile.getName().toLowerCase(); + if (fname.endsWith(ImageActor.JPEG) + || fname.endsWith(ImageActor.JPG) + || fname.endsWith(ImageActor.PNG)) { + deleteImgFiles(targetDir, targetFile); + } else { + targetFile.delete(); + } + } + } + result = "deleted"; + } + } catch (IOException ex) { + //logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); + } + return result; + } + + private void deleteImgFiles(File targetDir, File targetFile) throws IOException { + 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()); + Files.delete(path); + } + stream.close(); + } + +} diff --git a/src/de/uhilger/httpserver/cm/actor/Renamer.java b/src/de/uhilger/httpserver/cm/actor/Renamer.java new file mode 100644 index 0000000..4cae6d0 --- /dev/null +++ b/src/de/uhilger/httpserver/cm/actor/Renamer.java @@ -0,0 +1,94 @@ +/* + 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 <https://www.gnu.org/licenses/>. + */ +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<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(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; + } + + + +} diff --git a/src/de/uhilger/httpserver/cm/Unzipper.java b/src/de/uhilger/httpserver/cm/actor/Unzipper.java similarity index 72% rename from src/de/uhilger/httpserver/cm/Unzipper.java rename to src/de/uhilger/httpserver/cm/actor/Unzipper.java index f47b44d..67522e7 100644 --- a/src/de/uhilger/httpserver/cm/Unzipper.java +++ b/src/de/uhilger/httpserver/cm/actor/Unzipper.java @@ -1,4 +1,21 @@ -package de.uhilger.httpserver.cm; +/* + 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 <https://www.gnu.org/licenses/>. + */ +package de.uhilger.httpserver.cm.actor; import java.io.File; import java.io.FileOutputStream; @@ -9,8 +26,9 @@ import java.util.zip.ZipFile; /** - * - * @author ulli + * Eine Klasse mit Methoden zum entpacken von Dateien + * + * @author Ulrich Hilger, 15. Januar 2024 */ public class Unzipper { /* --------- ZIP entpacken ---------------- */ diff --git a/src/de/uhilger/httpserver/cm/Zipper.java b/src/de/uhilger/httpserver/cm/actor/Zipper.java similarity index 82% rename from src/de/uhilger/httpserver/cm/Zipper.java rename to src/de/uhilger/httpserver/cm/actor/Zipper.java index f806864..81288d9 100644 --- a/src/de/uhilger/httpserver/cm/Zipper.java +++ b/src/de/uhilger/httpserver/cm/actor/Zipper.java @@ -1,4 +1,21 @@ -package de.uhilger.httpserver.cm; +/* + 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 <https://www.gnu.org/licenses/>. + */ +package de.uhilger.httpserver.cm.actor; import static de.uhilger.httpserver.cm.FileManager.STR_SLASH; import java.io.File; @@ -11,8 +28,9 @@ import java.util.zip.ZipOutputStream; /** - * - * @author ulli + * Eine Klasse mit Methoden zum Packen von Dateien + * # + * @author Ulrich Hilger, 15. Januar 2024 */ public class Zipper { -- Gitblit v1.9.3