|  |  |  | 
|---|
|  |  |  | /* | 
|---|
|  |  |  | neon-fm - File management extensions to Neon | 
|---|
|  |  |  | fm - File management class library | 
|---|
|  |  |  | Copyright (C) 2024  Ulrich Hilger | 
|---|
|  |  |  |  | 
|---|
|  |  |  | This program is free software: you can redistribute it and/or modify | 
|---|
|  |  |  | 
|---|
|  |  |  | import java.nio.file.Path; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Eine Klasse mit Methoden zum Kopieren und Verschieben von Dateien | 
|---|
|  |  |  | * Verschieben und Kopieren von Dateien und Ordnern | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * 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 String copyOrMoveFiles(String fromPath, String toPath, String[] fileNames, | 
|---|
|  |  |  | public static final int OP_COPY = 1; | 
|---|
|  |  |  | public static final int OP_MOVE = 2; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Dateien und Ordner kopieren | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param fromPath der Pfad zur Quelle  der Kopieraktion | 
|---|
|  |  |  | * @param toPath der Pfad zum Ziel der Kopieraktion | 
|---|
|  |  |  | * @param fileNames die Liste der Dateien und Ordner, die kopiert werden sollen | 
|---|
|  |  |  | * @param base der Basispfad, gegen den fromPath und toPath aufgeloest werden sollen | 
|---|
|  |  |  | * @throws IOException wenn etwas schief geht | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void copy(String fromPath, String toPath, String[] fileNames, String base) | 
|---|
|  |  |  | throws IOException { | 
|---|
|  |  |  | copyOrMoveFiles(fromPath, toPath, fileNames, OP_COPY, base); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Dateien und Ordner verschieben | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param fromPath der Pfad zur Quelle  der Verschiebeaktion | 
|---|
|  |  |  | * @param toPath der Pfad zum Ziel der Verschiebeaktion | 
|---|
|  |  |  | * @param fileNames die Liste der Dateien und Ordner, die verschoben werden sollen | 
|---|
|  |  |  | * @param base der Basispfad, gegen den fromPath und toPath aufgeloest werden sollen | 
|---|
|  |  |  | * @throws IOException wenn etwas schief geht | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void move(String fromPath, String toPath, String[] fileNames, String base) | 
|---|
|  |  |  | throws IOException { | 
|---|
|  |  |  | copyOrMoveFiles(fromPath, toPath, fileNames, OP_MOVE, base); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 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 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void copyOrMoveFiles(String fromPath, String toPath, String[] fileNames, | 
|---|
|  |  |  | int operation, String base) throws IOException { | 
|---|
|  |  |  | String result = null; | 
|---|
|  |  |  | File srcDir = new File(base, fromPath); | 
|---|
|  |  |  | File targetDir = new File(base, toPath); | 
|---|
|  |  |  | for (String fileName : fileNames) { | 
|---|
|  |  |  | File srcFile = new File(srcDir, fileName); | 
|---|
|  |  |  | //logger.fine("srcFile: " + srcFile); | 
|---|
|  |  |  | if (srcFile.isDirectory()) { | 
|---|
|  |  |  | //logger.fine("srcFile is directory."); | 
|---|
|  |  |  | OrdnerBearbeiter bearbeiter = new OrdnerBearbeiter(); | 
|---|
|  |  |  | FileOpsVisitor bearbeiter = new FileOpsVisitor(); | 
|---|
|  |  |  | bearbeiter.setTargetDir(targetDir.toPath()); | 
|---|
|  |  |  | bearbeiter.setOperation(operation); | 
|---|
|  |  |  | Files.walkFileTree(srcFile.toPath(), bearbeiter); | 
|---|
|  |  |  | 
|---|
|  |  |  | if (destFile.exists()) { | 
|---|
|  |  |  | destFile = getNewFileName(destFile); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (operation == Const.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); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Files.move(source, destFile.toPath()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String fname = srcFile.getName().toLowerCase(); | 
|---|
|  |  |  | if (fname.endsWith(ImageFileFilter.JPEG) | 
|---|
|  |  |  | || fname.endsWith(ImageFileFilter.JPG) | 
|---|
|  |  |  | || fname.endsWith(ImageFileFilter.PNG)) { | 
|---|
|  |  |  | copyOrMoveImgFilesToDirectory(srcFile, srcDir, targetDir/*, false*/, operation); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Files.copy(source, destFile.toPath()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (operation == OP_MOVE) { | 
|---|
|  |  |  | Files.move(source, destFile.toPath()); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Files.copy(source, destFile.toPath()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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 copyOrMoveImgFilesToDirectory(File srcFile, File srcDir, File targetDir, | 
|---|
|  |  |  | int operation) throws IOException { | 
|---|
|  |  |  | String fnameext = srcFile.getName(); | 
|---|
|  |  |  | int dotpos = fnameext.lastIndexOf("."); | 
|---|
|  |  |  | String fname = fnameext.substring(0, dotpos); | 
|---|
|  |  |  | String ext = fnameext.substring(dotpos); | 
|---|
|  |  |  | //logger.fine("fname: " + fname + ", ext: " + ext); | 
|---|
|  |  |  | Path targetPath = targetDir.toPath(); | 
|---|
|  |  |  | DirectoryStream<Path> stream = Files.newDirectoryStream(srcDir.toPath(), fname + "*" + ext); | 
|---|
|  |  |  | //"*.{txt,doc,pdf,ppt}" | 
|---|
|  |  |  | for (Path path : stream) { | 
|---|
|  |  |  | //logger.fine(path.getFileName().toString()); | 
|---|
|  |  |  | //Files.delete(path); | 
|---|
|  |  |  | Files.move(path, targetPath.resolve(path.getFileName())); | 
|---|
|  |  |  | if(operation == OP_COPY) { | 
|---|
|  |  |  | Files.copy(path, targetPath.resolve(path.getFileName())); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Files.move(path, targetPath.resolve(path.getFileName())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | stream.close(); | 
|---|
|  |  |  | } | 
|---|