From 973951fe36062a8250b6890213b1e0c3e71da8b2 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Wed, 13 Nov 2024 15:11:00 +0000
Subject: [PATCH] Dokumentation in Arbeit, Konstanten verschoben
---
src/de/uhilger/fm/Eraser.java | 11 ++-
src/de/uhilger/fm/Renamer.java | 30 +++++-----
src/de/uhilger/fm/Mover.java | 54 ++++++++++++++----
src/de/uhilger/fm/CopyMoveVisitor.java | 33 ++++++++---
src/de/uhilger/fm/ImageFileFilter.java | 23 ++++++-
src/de/uhilger/fm/Lister.java | 8 +-
src/de/uhilger/fm/Const.java | 16 -----
7 files changed, 110 insertions(+), 65 deletions(-)
diff --git a/src/de/uhilger/fm/Const.java b/src/de/uhilger/fm/Const.java
index 3b98211..1ac26db 100644
--- a/src/de/uhilger/fm/Const.java
+++ b/src/de/uhilger/fm/Const.java
@@ -25,20 +25,4 @@
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
}
diff --git a/src/de/uhilger/fm/CopyMoveVisitor.java b/src/de/uhilger/fm/CopyMoveVisitor.java
index f7c5087..07fea91 100644
--- a/src/de/uhilger/fm/CopyMoveVisitor.java
+++ b/src/de/uhilger/fm/CopyMoveVisitor.java
@@ -37,23 +37,38 @@
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();
@@ -72,17 +87,17 @@
@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());
}
@@ -100,11 +115,11 @@
@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);
}
diff --git a/src/de/uhilger/fm/Eraser.java b/src/de/uhilger/fm/Eraser.java
index 9378b12..d42f12f 100644
--- a/src/de/uhilger/fm/Eraser.java
+++ b/src/de/uhilger/fm/Eraser.java
@@ -30,6 +30,9 @@
* @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 {
@@ -42,7 +45,7 @@
//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 {
/*
@@ -51,9 +54,9 @@
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();
diff --git a/src/de/uhilger/fm/ImageFileFilter.java b/src/de/uhilger/fm/ImageFileFilter.java
index 5472799..fc222f7 100644
--- a/src/de/uhilger/fm/ImageFileFilter.java
+++ b/src/de/uhilger/fm/ImageFileFilter.java
@@ -27,15 +27,28 @@
* @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;
}
}
diff --git a/src/de/uhilger/fm/Lister.java b/src/de/uhilger/fm/Lister.java
index f237ac1..c54f4e9 100644
--- a/src/de/uhilger/fm/Lister.java
+++ b/src/de/uhilger/fm/Lister.java
@@ -56,13 +56,13 @@
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);
diff --git a/src/de/uhilger/fm/Mover.java b/src/de/uhilger/fm/Mover.java
index f7b6ea6..66986aa 100644
--- a/src/de/uhilger/fm/Mover.java
+++ b/src/de/uhilger/fm/Mover.java
@@ -24,16 +24,34 @@
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 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 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) {
@@ -51,12 +69,12 @@
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());
}
@@ -65,11 +83,23 @@
}
}
}
- 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);
diff --git a/src/de/uhilger/fm/Renamer.java b/src/de/uhilger/fm/Renamer.java
index 75db53b..856ed88 100644
--- a/src/de/uhilger/fm/Renamer.java
+++ b/src/de/uhilger/fm/Renamer.java
@@ -33,12 +33,12 @@
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);
@@ -63,21 +63,21 @@
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;
}
--
Gitblit v1.9.3