WebBox Klassenbibliothek
ulrich@undisclosed
2020-05-28 bf412a896b52694891bc4b72dee092e026f6e667
commit | author | age
7bcebf 1 /*
U 2     WebBox - Dein Server.
3     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Affero General Public License as
7     published by the Free Software Foundation, either version 3 of the
8     License, or (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Affero General Public License for more details.
14
15     You should have received a copy of the GNU Affero General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package de.uhilger.wbx.web;
19
61b659 20 import java.io.File;
7bcebf 21 import java.io.IOException;
91d228 22 import java.io.UnsupportedEncodingException;
0622f3 23 import java.net.URLDecoder;
61b659 24 import java.util.logging.Logger;
7bcebf 25 import javax.servlet.ServletException;
U 26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
35827a 28 import net.coobird.thumbnailator.Thumbnails;
7bcebf 29 import org.apache.catalina.servlets.DefaultServlet;
U 30
31 /**
32  * Mit dem TNServlet kann f&uuml;r eine Bilddatei eine 
33  * Miniaturansicht erzeugt werden, wie sie im Dateimanager 
34  * der WebBox verwendet wird.
35  * 
91d228 36  * Unterstuetzt werden die Varianten
U 37  * 120 (_tn), 240 (_kl), 500 (_dd), 700 (_mt), 1200 (_gr)
7bcebf 38  */
U 39 public class TNServlet extends DefaultServlet {
61b659 40   
U 41   private static final Logger logger = Logger.getLogger(TNServlet.class.getName());
7bcebf 42   
U 43   /**
44    * Diese String-Konstanten noetigenfalls in eine 
45    * Konfigurationsdatei auslagern
46    */
e2808f 47   public static final String TN = "_tn"; // 120
U 48   public static final String KL = "_kl"; // 240
49   public static final String SM = "_sm"; // 500
50   public static final String MT = "_mt"; // 700
51   public static final String GR = "_gr"; // 1200
52   
7bcebf 53   public static final String JPG = ".jpg";
U 54   public static final String JPEG = ".jpeg";
55   public static final String PNG = ".png";
56   
57   /**
58    * Handles the HTTP <code>GET</code> method.
59    *
60    * @param request servlet request
61    * @param response servlet response
62    * @throws ServletException if a servlet-specific error occurs
63    * @throws IOException if an I/O error occurs
64    */
65   @Override
66   protected void doGet(HttpServletRequest request, HttpServletResponse response)
67           throws ServletException, IOException {
68     
61b659 69     String uriStr = request.getRequestURI();
1e5346 70     String relname = uriStr.substring(request.getContextPath().length());
61b659 71     
0622f3 72     // --- Logausgabe Start
e2808f 73     /*
0622f3 74     File logurifile = new File(uriStr);
U 75     String loguriStr = uriStr.substring(request.getContextPath().length());
76     File logdir = new File(request.getServletContext().getRealPath("/"));
e2808f 77     File imgfile = new File(logdir, loguriStr);    
1e5346 78     StringBuffer buf = new StringBuffer();
U 79     buf.append("contextPath: ");
80     buf.append(request.getContextPath());
81     buf.append("\n");
82     buf.append("realpath of /: ");
83     buf.append(request.getServletContext().getRealPath("/"));
84     buf.append("\n");
85     buf.append("urifile: ");
0622f3 86     buf.append(logurifile.getAbsolutePath());
1e5346 87     buf.append("\n");
U 88     buf.append("imgfile: ");
e2808f 89     buf.append(imgfile.getAbsolutePath());
1e5346 90     logger.fine(buf.toString());
e2808f 91     //System.out.println("TNServlet uri parts " + buf.toString());
U 92     */
0622f3 93     // --- Logausgabe Ende
U 94         
91d228 95     if(uriStr.endsWith(JPG) || uriStr.endsWith(JPEG) || uriStr.endsWith(PNG)) {
e2808f 96       File dir = new File(request.getServletContext().getRealPath("/"));
U 97       String suburiStr = uriStr.substring(request.getContextPath().length());
98       File imgfile = new File(dir, suburiStr);    
91d228 99       if(uriStr.contains(TN)) {
e2808f 100         bildErzeugen(dir, relname, TN, 120, imgfile);
91d228 101       } else if(uriStr.contains(KL)) {
e2808f 102         bildErzeugen(dir, relname, KL, 240, imgfile);
91d228 103       } else if(uriStr.contains(SM)) {
e2808f 104         bildErzeugen(dir, relname, SM, 500, imgfile);
91d228 105       } else if(uriStr.contains(MT)) {
e2808f 106         bildErzeugen(dir, relname, MT, 700, imgfile);
91d228 107       } else if(uriStr.contains(GR)) {
e2808f 108         bildErzeugen(dir, relname, GR, 1200, imgfile);
91d228 109       }      
7bcebf 110     }
e2808f 111     super.doGet(request, response);
7bcebf 112   }
91d228 113   
e2808f 114   private void bildErzeugen(File dir, String relname, String indicator, int gr, File tnfile) 
U 115           throws UnsupportedEncodingException, IOException {
116     //File dir = new File(request.getServletContext().getRealPath("/"));
117     // System.out.println("TNServlet dir: " + dir);
91d228 118     relname = relname.replace(indicator, "");
e2808f 119     // System.out.println("TNServlet bildAusgeben relname: " + relname);
U 120     // relname: /test/img/IMG_0524.png
91d228 121     File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8"));
35827a 122     
U 123     // 120, 240, 500, 700, 1200
91d228 124
e2808f 125     /*Thumbnails.of(imgfile)
35827a 126             .size(gr, gr)
U 127             .keepAspectRatio(true)
1475ef 128             .outputQuality(0.7)
e2808f 129             .toOutputStream(response.getOutputStream());*/
56c51f 130     if(imgfile.exists() && !tnfile.exists()) {
e2808f 131       Thumbnails.of(imgfile)
U 132               .size(gr, gr)
133               .keepAspectRatio(true)
134               .outputQuality(0.7)
135               .toFile(tnfile);
136     }
35827a 137   }  
7bcebf 138 }