Dokumentation in Arbeit, Konstanten verschoben
| | |
| | | public class Const { |
| | | public static final String STR_SLASH = "/"; |
| | | public static final String STR_DOT = "."; |
| | | |
| | | public static final int OP_COPY = 1; |
| | | public static final int OP_MOVE = 2; |
| | | public static final int OP_DELETE = 3; |
| | | |
| | | public static final String JPG = ".jpg"; |
| | | public static final String JPEG = ".jpeg"; |
| | | public static final String PNG = ".png"; |
| | | |
| | | public static final String B64 = "_b64"; // Base64-Encoded |
| | | |
| | | public static final String TN = "_tn"; // 120 |
| | | public static final String KL = "_kl"; // 240 |
| | | public static final String SM = "_sm"; // 500 |
| | | public static final String MT = "_mt"; // 700 |
| | | public static final String GR = "_gr"; // 1200 |
| | | } |
| | |
| | | private Path targetDir; |
| | | private int operation; |
| | | |
| | | /** |
| | | * Den Zielordner fuer Kopier- oder Verschiebeoperationen angeben |
| | | * |
| | | * @param targetDir der Zielordner |
| | | */ |
| | | public void setTargetDir(Path targetDir) { |
| | | this.targetDir = targetDir; |
| | | //logger.fine("targetDir: " + targetDir.toString()); |
| | | } |
| | | |
| | | /** |
| | | * OP_COPY oder OP_MOVE |
| | | * Die gewuenschte Dateioperation angeben, |
| | | * OP_COPY, OP_MOVE oder OP_DELETE |
| | | * |
| | | * @param op |
| | | * @param op die Dateioperation |
| | | */ |
| | | public void setOperation(int op) { |
| | | this.operation = op; |
| | | } |
| | | |
| | | /** |
| | | * Dafuer sorgen, dass beim Kopieren oder Verschieben kein am Ziel bereits existierender |
| | | * Ordner ueberschrieben wird, indem am Ziel fuer einen bereits existierenden Ordner ein |
| | | * anderer Name mit laufender Nummer erzeugt wird. |
| | | * |
| | | * @param dir Zielordner |
| | | * @param attrs die gewuenschten Attribute |
| | | * @return gibt stets FileVisitResult.CONTINUE zurueck |
| | | * @throws IOException wenn etwas schief geht |
| | | */ |
| | | @Override |
| | | public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException { |
| | | if (operation != Const.OP_DELETE) { |
| | | if (operation != Eraser.OP_DELETE) { |
| | | if (dir instanceof Path) { |
| | | Path sourceDir = (Path) dir; |
| | | File destFile = targetDir.resolve(sourceDir.getFileName()).toFile(); |
| | |
| | | |
| | | @Override |
| | | public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException { |
| | | if(operation != Const.OP_DELETE) { |
| | | if(operation != Eraser.OP_DELETE) { |
| | | if (file instanceof Path) { |
| | | Path source = (Path) file; |
| | | File destFile = targetDir.resolve(source.getFileName()).toFile(); |
| | | if (destFile.exists()) { |
| | | destFile = getNewFileName(destFile); |
| | | } |
| | | if (operation == Const.OP_MOVE) { |
| | | if (operation == Mover.OP_MOVE) { |
| | | //logger.fine("move source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
| | | Files.move(source, destFile.toPath()); |
| | | } else if (operation == Const.OP_COPY) { |
| | | } else if (operation == Mover.OP_COPY) { |
| | | //logger.fine("copy source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
| | | Files.copy(source, destFile.toPath()); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException { |
| | | if (operation != Const.OP_DELETE) { |
| | | if (operation != Eraser.OP_DELETE) { |
| | | if (dir instanceof Path) { |
| | | Path finishedDir = (Path) dir; |
| | | targetDir = targetDir.getParent(); |
| | | if(operation == Const.OP_MOVE) { |
| | | if(operation == Mover.OP_MOVE) { |
| | | //logger.fine("delete " + finishedDir.toString()); |
| | | Files.delete(finishedDir); |
| | | } |
| | |
| | | * @author Ulrich Hilger, 15. Januar 2024 |
| | | */ |
| | | public class Eraser { |
| | | |
| | | public static final int OP_DELETE = 3; |
| | | |
| | | public String deleteFiles(String relPath, List<String> fileNames, String base) { |
| | | String result = null; |
| | | try { |
| | |
| | | //logger.fine(targetFile.getAbsolutePath()); |
| | | if (targetFile.isDirectory()) { |
| | | CopyMoveVisitor bearbeiter = new CopyMoveVisitor(); |
| | | bearbeiter.setOperation(Const.OP_DELETE); |
| | | bearbeiter.setOperation(OP_DELETE); |
| | | Files.walkFileTree(targetFile.toPath(), bearbeiter); |
| | | } else { |
| | | /* |
| | |
| | | die so heissen, also z.B. alle [Dateiname]*.jpg |
| | | */ |
| | | String fname = targetFile.getName().toLowerCase(); |
| | | if (fname.endsWith(Const.JPEG) |
| | | || fname.endsWith(Const.JPG) |
| | | || fname.endsWith(Const.PNG)) { |
| | | if (fname.endsWith(ImageFileFilter.JPEG) |
| | | || fname.endsWith(ImageFileFilter.JPG) |
| | | || fname.endsWith(ImageFileFilter.PNG)) { |
| | | deleteImgFiles(targetDir, targetFile); |
| | | } else { |
| | | targetFile.delete(); |
| | |
| | | * @version 1, 12. Mai 2021 |
| | | */ |
| | | public class ImageFileFilter implements FileFilter { |
| | | |
| | | public static final String JPG = ".jpg"; |
| | | public static final String JPEG = ".jpeg"; |
| | | public static final String PNG = ".png"; |
| | | |
| | | public static final String B64 = "_b64"; // Base64-Encoded |
| | | |
| | | public static final String TN = "_tn"; // 120 |
| | | public static final String KL = "_kl"; // 240 |
| | | public static final String SM = "_sm"; // 500 |
| | | public static final String MT = "_mt"; // 700 |
| | | public static final String GR = "_gr"; // 1200 |
| | | |
| | | @Override |
| | | public boolean accept(File pathname) { |
| | | boolean pass = true; |
| | | String fname = pathname.getName().toLowerCase(); |
| | | if(fname.endsWith(Const.JPEG) || |
| | | fname.endsWith(Const.JPG) || fname.endsWith(Const.PNG)) { |
| | | if(fname.contains(Const.GR) || fname.contains(Const.KL) || |
| | | fname.contains(Const.MT) || fname.contains(Const.SM) || |
| | | fname.contains(Const.TN) || fname.contains(Const.B64)) { |
| | | if(fname.endsWith(JPEG) || |
| | | fname.endsWith(JPG) || fname.endsWith(PNG)) { |
| | | if(fname.contains(GR) || fname.contains(KL) || |
| | | fname.contains(MT) || fname.contains(SM) || |
| | | fname.contains(TN) || fname.contains(B64)) { |
| | | pass = false; |
| | | } |
| | | } |
| | |
| | | datei.setTyp(Datei.TYP_DATEI); |
| | | } |
| | | String lowerName = dateiName.toLowerCase(); |
| | | if (lowerName.endsWith(Const.JPEG) |
| | | || lowerName.endsWith(Const.JPG) |
| | | || lowerName.endsWith(Const.PNG)) { |
| | | if (lowerName.endsWith(ImageFileFilter.JPEG) |
| | | || lowerName.endsWith(ImageFileFilter.JPG) |
| | | || lowerName.endsWith(ImageFileFilter.PNG)) { |
| | | datei.setBild(true); |
| | | String ext = dateiName.substring(dateiName.lastIndexOf(STR_DOT)); |
| | | String ohneExt = dateiName.substring(0, dateiName.lastIndexOf(STR_DOT)); |
| | | datei.setMiniurl(ctxPath + /*"/" + */ fName + ohneExt + Const.TN + ext); |
| | | datei.setMiniurl(ctxPath + /*"/" + */ fName + ohneExt + ImageFileFilter.TN + ext); |
| | | //buildImgSrc(file, datei, ohneExt, ext); |
| | | } |
| | | liste.add(datei); |
| | |
| | | import java.nio.file.Path; |
| | | |
| | | /** |
| | | * Eine Klasse mit Methoden zum Kopieren und Verschieben von Dateien |
| | | * Die Klasse Mover verschiebt und kopiert Dateien und Ordner |
| | | * |
| | | * Handhabung von Bilddateien: |
| | | * |
| | | * Fuer jede Datei mit Endung jpg, jpeg und png werden alle Varianten wie zum Beispiel |
| | | * dateiname.jpg, dateiname_kl.jpg, dateiname_gr.jpg, dateiname_gr_b64.jpg usw. |
| | | * beruecksichtigt. |
| | | * |
| | | * @author Ulrich Hilger, 15. Janaur 2024 |
| | | */ |
| | | public class Mover extends FileHelper { |
| | | |
| | | public static final int OP_COPY = 1; |
| | | public static final int OP_MOVE = 2; |
| | | |
| | | public String copyOrMoveFiles(String fromPath, String toPath, String[] fileNames, |
| | | /** |
| | | * Dateien und Ordner verschieben oder kopieren |
| | | * |
| | | * @param fromPath der Pfad zur Quelle der Verschiebe- oder Kopieraktion |
| | | * @param toPath der Pfad zum Ziel der Verschiebe- oder Kopieraktion |
| | | * @param fileNames die Liste der Dateien und Ordner, die verschoben oder kopiert werden sollen |
| | | * @param operation die gewuenschte Dateioperation, OP_COPY oder OP_MOVE |
| | | * @param base der Basispfad, gegen den fromPath und toPath aufgeloest werden sollen |
| | | * @throws IOException wenn etwas schief geht |
| | | */ |
| | | public void copyOrMoveFiles(String fromPath, String toPath, String[] fileNames, |
| | | int operation, String base) throws IOException { |
| | | String result = null; |
| | | //String result = null; |
| | | File srcDir = new File(base, fromPath); |
| | | File targetDir = new File(base, toPath); |
| | | for (String fileName : fileNames) { |
| | |
| | | if (destFile.exists()) { |
| | | destFile = getNewFileName(destFile); |
| | | } |
| | | if (operation == Const.OP_MOVE) { |
| | | if (operation == OP_MOVE) { |
| | | String fname = srcFile.getName().toLowerCase(); |
| | | if (fname.endsWith(Const.JPEG) |
| | | || fname.endsWith(Const.JPG) |
| | | || fname.endsWith(Const.PNG)) { |
| | | moveImgFilesToDirectory(srcFile, srcDir, targetDir, false); |
| | | if (fname.endsWith(ImageFileFilter.JPEG) |
| | | || fname.endsWith(ImageFileFilter.JPG) |
| | | || fname.endsWith(ImageFileFilter.PNG)) { |
| | | moveImgFilesToDirectory(srcFile, srcDir, targetDir/*, false*/); |
| | | } else { |
| | | Files.move(source, destFile.toPath()); |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | //return result; |
| | | } |
| | | |
| | | private void moveImgFilesToDirectory(File srcFile, File srcDir, File targetDir, |
| | | boolean createDestDir) throws IOException { |
| | | /** |
| | | * Eine Bilddatei mit allen Varianten verschieben oder kopieren |
| | | * |
| | | * Fuer jede Datei mit Endung jpg, jpeg und png werden alle Varianten wie zum Beispiel |
| | | * dateiname.jpg, dateiname_kl.jpg, dateiname_gr.jpg, dateiname_gr_b64.jpg usw. |
| | | * beruecksichtigt, also dateiname*.jpg. |
| | | * |
| | | * @param srcFile die Bilddatei, deren Varianten beruecksichtigt werden sollen |
| | | * @param srcDir der Herkunftsort |
| | | * @param targetDir der Zielort |
| | | * @throws IOException wenn etwas schief geht |
| | | */ |
| | | private void moveImgFilesToDirectory(File srcFile, File srcDir, File targetDir/*, |
| | | boolean createDestDir*/) throws IOException { |
| | | String fnameext = srcFile.getName(); |
| | | int dotpos = fnameext.lastIndexOf("."); |
| | | String fname = fnameext.substring(0, dotpos); |
| | |
| | | |
| | | public static final String STR_DOT = "."; |
| | | |
| | | public String umbenennen(HttpExchange exchange, String relPfad, String neuerName, File file) throws IOException { |
| | | 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)) { |
| | | if(fname.endsWith(ImageFileFilter.JPEG) || fname.endsWith(ImageFileFilter.JPG) || |
| | | fname.endsWith(ImageFileFilter.PNG)) { |
| | | neueDatei = renameImgFiles(file.getParentFile(), file, neuerName); |
| | | } else { |
| | | neueDatei = new File(file.getParentFile(), neuerName); |
| | |
| | | String ext = fnameext.substring(dotpos); |
| | | //logger.fine("fname: " + fname + ", ext: " + ext); |
| | | |
| | | DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir.toPath(), fname + "*" + ext); //"*.{txt,doc,pdf,ppt}" |
| | | DirectoryStream<Path> stream = Files.newDirectoryStream(targetDir.toPath(), fname + "*" + ext); |
| | | 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; |
| | | if(alt.contains(ImageFileFilter.TN)) { |
| | | neu = newfname + ImageFileFilter.TN + newext; |
| | | } else if (alt.contains(ImageFileFilter.KL)) { |
| | | neu = newfname + ImageFileFilter.KL + newext; |
| | | } else if(alt.contains(ImageFileFilter.GR)) { |
| | | neu = newfname + ImageFileFilter.GR + newext; |
| | | } else if(alt.contains(ImageFileFilter.MT)) { |
| | | neu = newfname + ImageFileFilter.MT + newext; |
| | | } else if(alt.contains(ImageFileFilter.SM)) { |
| | | neu = newfname + ImageFileFilter.SM + newext; |
| | | } else { |
| | | neu = newName; |
| | | } |