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; |
828ffa
|
26 |
import javax.servlet.http.HttpServlet; |
7bcebf
|
27 |
import javax.servlet.http.HttpServletRequest; |
U |
28 |
import javax.servlet.http.HttpServletResponse; |
35827a
|
29 |
import net.coobird.thumbnailator.Thumbnails; |
c2c6fb
|
30 |
import org.apache.catalina.servlets.DefaultServlet; |
7bcebf
|
31 |
|
U |
32 |
/** |
|
33 |
* Mit dem TNServlet kann für eine Bilddatei eine |
|
34 |
* Miniaturansicht erzeugt werden, wie sie im Dateimanager |
|
35 |
* der WebBox verwendet wird. |
|
36 |
* |
91d228
|
37 |
* Unterstuetzt werden die Varianten |
U |
38 |
* 120 (_tn), 240 (_kl), 500 (_dd), 700 (_mt), 1200 (_gr) |
7bcebf
|
39 |
*/ |
c2c6fb
|
40 |
public class TNServlet extends DefaultServlet { |
61b659
|
41 |
|
U |
42 |
private static final Logger logger = Logger.getLogger(TNServlet.class.getName()); |
7bcebf
|
43 |
|
U |
44 |
/** |
|
45 |
* Diese String-Konstanten noetigenfalls in eine |
|
46 |
* Konfigurationsdatei auslagern |
|
47 |
*/ |
e2808f
|
48 |
public static final String TN = "_tn"; // 120 |
U |
49 |
public static final String KL = "_kl"; // 240 |
|
50 |
public static final String SM = "_sm"; // 500 |
|
51 |
public static final String MT = "_mt"; // 700 |
|
52 |
public static final String GR = "_gr"; // 1200 |
|
53 |
|
7bcebf
|
54 |
public static final String JPG = ".jpg"; |
U |
55 |
public static final String JPEG = ".jpeg"; |
|
56 |
public static final String PNG = ".png"; |
|
57 |
|
|
58 |
/** |
|
59 |
* Handles the HTTP <code>GET</code> method. |
|
60 |
* |
|
61 |
* @param request servlet request |
|
62 |
* @param response servlet response |
|
63 |
* @throws ServletException if a servlet-specific error occurs |
|
64 |
* @throws IOException if an I/O error occurs |
|
65 |
*/ |
|
66 |
@Override |
|
67 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
|
68 |
throws ServletException, IOException { |
|
69 |
|
61b659
|
70 |
String uriStr = request.getRequestURI(); |
1e5346
|
71 |
String relname = uriStr.substring(request.getContextPath().length()); |
61b659
|
72 |
|
0622f3
|
73 |
// --- Logausgabe Start |
e2808f
|
74 |
/* |
0622f3
|
75 |
File logurifile = new File(uriStr); |
U |
76 |
String loguriStr = uriStr.substring(request.getContextPath().length()); |
|
77 |
File logdir = new File(request.getServletContext().getRealPath("/")); |
e2808f
|
78 |
File imgfile = new File(logdir, loguriStr); |
1e5346
|
79 |
StringBuffer buf = new StringBuffer(); |
U |
80 |
buf.append("contextPath: "); |
|
81 |
buf.append(request.getContextPath()); |
|
82 |
buf.append("\n"); |
|
83 |
buf.append("realpath of /: "); |
|
84 |
buf.append(request.getServletContext().getRealPath("/")); |
|
85 |
buf.append("\n"); |
|
86 |
buf.append("urifile: "); |
0622f3
|
87 |
buf.append(logurifile.getAbsolutePath()); |
1e5346
|
88 |
buf.append("\n"); |
U |
89 |
buf.append("imgfile: "); |
e2808f
|
90 |
buf.append(imgfile.getAbsolutePath()); |
1e5346
|
91 |
logger.fine(buf.toString()); |
e2808f
|
92 |
//System.out.println("TNServlet uri parts " + buf.toString()); |
U |
93 |
*/ |
0622f3
|
94 |
// --- Logausgabe Ende |
U |
95 |
|
91d228
|
96 |
if(uriStr.endsWith(JPG) || uriStr.endsWith(JPEG) || uriStr.endsWith(PNG)) { |
828ffa
|
97 |
File dir = new File(request.getSession().getServletContext().getRealPath("/")); |
e2808f
|
98 |
String suburiStr = uriStr.substring(request.getContextPath().length()); |
U |
99 |
File imgfile = new File(dir, suburiStr); |
91d228
|
100 |
if(uriStr.contains(TN)) { |
e2808f
|
101 |
bildErzeugen(dir, relname, TN, 120, imgfile); |
91d228
|
102 |
} else if(uriStr.contains(KL)) { |
e2808f
|
103 |
bildErzeugen(dir, relname, KL, 240, imgfile); |
91d228
|
104 |
} else if(uriStr.contains(SM)) { |
e2808f
|
105 |
bildErzeugen(dir, relname, SM, 500, imgfile); |
91d228
|
106 |
} else if(uriStr.contains(MT)) { |
e2808f
|
107 |
bildErzeugen(dir, relname, MT, 700, imgfile); |
91d228
|
108 |
} else if(uriStr.contains(GR)) { |
e2808f
|
109 |
bildErzeugen(dir, relname, GR, 1200, imgfile); |
91d228
|
110 |
} |
7bcebf
|
111 |
} |
e2808f
|
112 |
super.doGet(request, response); |
7bcebf
|
113 |
} |
91d228
|
114 |
|
e2808f
|
115 |
private void bildErzeugen(File dir, String relname, String indicator, int gr, File tnfile) |
U |
116 |
throws UnsupportedEncodingException, IOException { |
|
117 |
//File dir = new File(request.getServletContext().getRealPath("/")); |
|
118 |
// System.out.println("TNServlet dir: " + dir); |
91d228
|
119 |
relname = relname.replace(indicator, ""); |
e2808f
|
120 |
// System.out.println("TNServlet bildAusgeben relname: " + relname); |
U |
121 |
// relname: /test/img/IMG_0524.png |
91d228
|
122 |
File imgfile = new File(dir, URLDecoder.decode(relname, "utf-8")); |
35827a
|
123 |
|
U |
124 |
// 120, 240, 500, 700, 1200 |
91d228
|
125 |
|
e2808f
|
126 |
/*Thumbnails.of(imgfile) |
35827a
|
127 |
.size(gr, gr) |
U |
128 |
.keepAspectRatio(true) |
1475ef
|
129 |
.outputQuality(0.7) |
e2808f
|
130 |
.toOutputStream(response.getOutputStream());*/ |
56c51f
|
131 |
if(imgfile.exists() && !tnfile.exists()) { |
e2808f
|
132 |
Thumbnails.of(imgfile) |
U |
133 |
.size(gr, gr) |
|
134 |
.keepAspectRatio(true) |
|
135 |
.outputQuality(0.7) |
|
136 |
.toFile(tnfile); |
|
137 |
} |
35827a
|
138 |
} |
7bcebf
|
139 |
} |