From e2808f9cb42bec32955e7f960bb289f56714d5c1 Mon Sep 17 00:00:00 2001
From: ulrich <undisclosed>
Date: Fri, 17 Jan 2020 16:32:38 +0000
Subject: [PATCH] Thumbnails werden nun im jeweiligen Bildordner mit abgelegt und nicht jedesmal 'on the fly' erzeugt.

---
 src/de/uhilger/wbx/web/TNServlet.java |   55 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/src/de/uhilger/wbx/web/TNServlet.java b/src/de/uhilger/wbx/web/TNServlet.java
index 98fd667..c2c4c27 100644
--- a/src/de/uhilger/wbx/web/TNServlet.java
+++ b/src/de/uhilger/wbx/web/TNServlet.java
@@ -44,11 +44,12 @@
    * Diese String-Konstanten noetigenfalls in eine 
    * Konfigurationsdatei auslagern
    */
-  public static final String TN = "_tn";
-  public static final String KL = "_kl";
-  public static final String SM = "_sm";
-  public static final String MT = "_mt";
-  public static final String GR = "_gr";
+  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
+  
   public static final String JPG = ".jpg";
   public static final String JPEG = ".jpeg";
   public static final String PNG = ".png";
@@ -69,10 +70,11 @@
     String relname = uriStr.substring(request.getContextPath().length());
     
     // --- Logausgabe Start
+    /*
     File logurifile = new File(uriStr);
     String loguriStr = uriStr.substring(request.getContextPath().length());
     File logdir = new File(request.getServletContext().getRealPath("/"));
-    File logimgfile = new File(logdir, loguriStr);    
+    File imgfile = new File(logdir, loguriStr);    
     StringBuffer buf = new StringBuffer();
     buf.append("contextPath: ");
     buf.append(request.getContextPath());
@@ -84,40 +86,53 @@
     buf.append(logurifile.getAbsolutePath());
     buf.append("\n");
     buf.append("imgfile: ");
-    buf.append(logimgfile.getAbsolutePath());
+    buf.append(imgfile.getAbsolutePath());
     logger.fine(buf.toString());
+    //System.out.println("TNServlet uri parts " + buf.toString());
+    */
     // --- Logausgabe Ende
         
     if(uriStr.endsWith(JPG) || uriStr.endsWith(JPEG) || uriStr.endsWith(PNG)) {
+      File dir = new File(request.getServletContext().getRealPath("/"));
+      String suburiStr = uriStr.substring(request.getContextPath().length());
+      File imgfile = new File(dir, suburiStr);    
       if(uriStr.contains(TN)) {
-        bildAusgeben(request, response, relname, TN, 120);
+        bildErzeugen(dir, relname, TN, 120, imgfile);
       } else if(uriStr.contains(KL)) {
-        bildAusgeben(request, response, relname, KL, 240);
+        bildErzeugen(dir, relname, KL, 240, imgfile);
       } else if(uriStr.contains(SM)) {
-        bildAusgeben(request, response, relname, SM, 500);
+        bildErzeugen(dir, relname, SM, 500, imgfile);
       } else if(uriStr.contains(MT)) {
-        bildAusgeben(request, response, relname, MT, 700);
+        bildErzeugen(dir, relname, MT, 700, imgfile);
       } else if(uriStr.contains(GR)) {
-        bildAusgeben(request, response, relname, GR, 1200);
-      } else {
-        super.doGet(request, response);
+        bildErzeugen(dir, relname, GR, 1200, imgfile);
       }      
-    } else {
-      super.doGet(request, response);
     }
+    super.doGet(request, response);
   }
   
-  private void bildAusgeben(HttpServletRequest request, HttpServletResponse response, String relname, String indicator, int gr) throws UnsupportedEncodingException, IOException {
-    File dir = new File(request.getServletContext().getRealPath("/"));
+  private void bildErzeugen(File dir, String relname, String indicator, int gr, File tnfile) 
+          throws UnsupportedEncodingException, IOException {
+    //File dir = new File(request.getServletContext().getRealPath("/"));
+    // System.out.println("TNServlet dir: " + dir);
     relname = relname.replace(indicator, "");
+    // System.out.println("TNServlet bildAusgeben relname: " + relname);
+    // relname: /test/img/IMG_0524.png
     File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8"));
     
     // 120, 240, 500, 700, 1200
 
-    Thumbnails.of(imgfile)
+    /*Thumbnails.of(imgfile)
             .size(gr, gr)
             .keepAspectRatio(true)
             .outputQuality(0.7)
-            .toOutputStream(response.getOutputStream());
+            .toOutputStream(response.getOutputStream());*/
+    if(!tnfile.exists()) {
+      Thumbnails.of(imgfile)
+              .size(gr, gr)
+              .keepAspectRatio(true)
+              .outputQuality(0.7)
+              .toFile(tnfile);
+    }
   }  
 }

--
Gitblit v1.9.3