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/FileManager.java | 119 +++-------------------------------------------------------- 1 files changed, 6 insertions(+), 113 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); } - - - - } -- Gitblit v1.9.3