From f7b9adf186cdda03ef787dd8064066e790710a69 Mon Sep 17 00:00:00 2001
From: ulrich
Date: Sun, 25 Feb 2024 11:45:13 +0000
Subject: [PATCH] Matcher um Pattern-Attribut erweitert und in Filter verlagert; Berichtigungen am ImageWorker

---
 src/de/uhilger/neon/image/ImageFilter.java |   30 +++++++++-----
 src/de/uhilger/neon/image/ImageWorker.java |   61 +++++++++++++++---------------
 2 files changed, 50 insertions(+), 41 deletions(-)

diff --git a/src/de/uhilger/neon/image/ImageFilter.java b/src/de/uhilger/neon/image/ImageFilter.java
index 1f02274..c1b3f29 100644
--- a/src/de/uhilger/neon/image/ImageFilter.java
+++ b/src/de/uhilger/neon/image/ImageFilter.java
@@ -60,18 +60,26 @@
   
   @Override
   public void doFilter(HttpExchange exchange, Chain chain) throws IOException {
+    /*
+      Regex fuer Bilder
+      .+\.jpg|.+\.jpeg|.+\.png
+      (Testen z.B. auf https://regexr.com/)
+    */
+    String pattern = (String) exchange.getHttpContext().getAttributes()
+            .getOrDefault("imageFilterPattern", ".+\\.jpg|.+\\.jpeg|.+\\.png");
     String uriStr = exchange.getRequestURI().toString();
-    //String fName = new HttpHelper().getFileName(exchange);
-    String fName = exchange
-            .getRequestURI()
-            .getPath()
-            .substring(exchange
-                    .getHttpContext()
-                    .getPath()
-                    .length());          
-    ImageWorker worker = new ImageWorker();
-    worker.createImages(exchange.getHttpContext().getAttributes().get(FileServer.ATTR_FILE_BASE).toString(), 
-            fName, uriStr);
+    if(uriStr.matches(pattern)) {
+      String fileBase = (String) exchange.getHttpContext().getAttributes()
+              .get(FileServer.ATTR_FILE_BASE);
+      String fileName = exchange
+              .getRequestURI()
+              .getPath()
+              .substring(exchange
+                      .getHttpContext()
+                      .getPath()
+                      .length());          
+      new ImageWorker().createImages(fileBase, fileName, uriStr);
+    }
     chain.doFilter(exchange);
   }
 
diff --git a/src/de/uhilger/neon/image/ImageWorker.java b/src/de/uhilger/neon/image/ImageWorker.java
index 97bedcb..0f2ef17 100644
--- a/src/de/uhilger/neon/image/ImageWorker.java
+++ b/src/de/uhilger/neon/image/ImageWorker.java
@@ -42,40 +42,40 @@
   /**
    * Diese String-Konstanten noetigenfalls in eine Konfigurationsdatei auslagern
    */
-  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 final String TN = "_tn"; // 120
+  private final String KL = "_kl"; // 240
+  private final String SM = "_sm"; // 500
+  private final String MT = "_mt"; // 700
+  private final String GR = "_gr"; // 1200
 
-  public static final String B64 = "_b64"; // Base64-Encoded
+  private final String B64 = "_b64"; // Base64-Encoded
 
-  public static final String JPG = ".jpg";
-  public static final String JPEG = ".jpeg";
-  public static final String PNG = ".png";
+  //public static final String JPG = ".jpg";
+  //public static final String JPEG = ".jpeg";
+  //public static final String PNG = ".png";
   
   public void createImages(String baseDir, String  fName, String uriStr) throws IOException {
-    if(uriStr.endsWith(ImageWorker.JPG) || uriStr.endsWith(ImageWorker.JPEG) || uriStr.endsWith(ImageWorker.PNG)) {
+    //if(uriStr.endsWith(ImageWorker.JPG) || uriStr.endsWith(ImageWorker.JPEG) || uriStr.endsWith(ImageWorker.PNG)) {
       File dir = new File(baseDir);
       File imgfile = new File(dir, fName);    
-      ImageWorker actor = new ImageWorker();
-      if(uriStr.contains(ImageWorker.TN)) {
-        actor.createImage(dir, fName, ImageWorker.TN, 120, imgfile);
-      } else if(uriStr.contains(ImageWorker.KL)) {
-        actor.createImage(dir, fName, ImageWorker.KL, 240, imgfile);
-      } else if(uriStr.contains(ImageWorker.SM)) {
-        actor.createImage(dir, fName, ImageWorker.SM, 500, imgfile);
-      } else if(uriStr.contains(ImageWorker.MT)) {
-        actor.createImage(dir, fName, ImageWorker.MT, 700, imgfile);
-      } else if(uriStr.contains(ImageWorker.GR)) {
-        actor.createImage(dir, fName, ImageWorker.GR, 1200, imgfile);
-      } else if(uriStr.contains(ImageWorker.B64)) {
+      //ImageWorker actor = new ImageWorker();
+      if(uriStr.contains(TN)) {
+        createImage(dir, fName, TN, 120, imgfile);
+      } else if(uriStr.contains(KL)) {
+        createImage(dir, fName, KL, 240, imgfile);
+      } else if(uriStr.contains(SM)) {
+        createImage(dir, fName, SM, 500, imgfile);
+      } else if(uriStr.contains(MT)) {
+        createImage(dir, fName, MT, 700, imgfile);
+      } else if(uriStr.contains(GR)) {
+        createImage(dir, fName, GR, 1200, imgfile);
+      } else if(uriStr.contains(B64)) {
         File b64File = imgfile;
-        String fromName = fName.replace(ImageWorker.B64, "");
+        String fromName = fName.replace(B64, "");
         File fromfile = new File(dir, fromName);        
-        actor.b64Image(fromfile, b64File);
+        b64Image(fromfile, b64File);
       }     
-    }    
+    //}    
   }
 
   /**
@@ -138,11 +138,12 @@
   public void setImgSrc(Datei datei, String ext, File b64File) throws IOException {
     StringBuilder sb = new StringBuilder();
     sb.append("data:image/");
-    if(ext.equalsIgnoreCase(ImageWorker.JPEG) || ext.equalsIgnoreCase(ImageWorker.JPG)) {
-      sb.append("jpeg");
-    } else if(ext.equalsIgnoreCase(ImageWorker.PNG)) {
-      sb.append("png");
-    }
+    //if(ext.equalsIgnoreCase(ImageWorker.JPEG) || ext.equalsIgnoreCase(ImageWorker.JPG)) {
+    //  sb.append("jpeg");
+    //} else if(ext.equalsIgnoreCase(ImageWorker.PNG)) {
+    //  sb.append("png");
+    //}
+    sb.append(ext.toLowerCase());
     sb.append(";base64,");
     byte[] buf = new byte[4096];
     InputStream is = new FileInputStream(b64File);

--
Gitblit v1.9.3