| | |
| | | */ |
| | | 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; |
| | |
| | | } |
| | | 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; |
| | |
| | | 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."); |
| | | } |
| | | |
| | |
| | | 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(); |
| | |
| | | resHeaders.add(CONTENT_TYPE, HttpHelper.CT_TEXT_HTML); |
| | | new HttpResponder().antwortSenden(exchange, status, antwort); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |