0
|
1 package nabble.view.web.forum;
|
|
2
|
|
3 import fschmidt.util.java.ImageUtils;
|
|
4 import fschmidt.util.java.IoUtils;
|
|
5 import fschmidt.util.servlet.AuthorizingServlet;
|
|
6 import fschmidt.util.servlet.CanonicalUrl;
|
|
7 import fschmidt.util.servlet.JtpContext;
|
|
8 import nabble.model.FileUpload;
|
|
9 import nabble.model.Message;
|
|
10 import nabble.model.Node;
|
|
11 import nabble.model.Site;
|
|
12 import nabble.model.SystemProperties;
|
|
13 import nabble.view.lib.Cache;
|
|
14 import nabble.view.lib.Jtp;
|
|
15 import nabble.view.lib.UrlMappable;
|
|
16 import org.apache.commons.fileupload.FileItem;
|
|
17
|
|
18 import javax.servlet.ServletException;
|
|
19 import javax.servlet.http.HttpServlet;
|
|
20 import javax.servlet.http.HttpServletRequest;
|
|
21 import javax.servlet.http.HttpServletResponse;
|
|
22 import java.awt.image.BufferedImage;
|
|
23 import java.io.IOException;
|
|
24 import java.io.InputStream;
|
|
25 import java.net.URL;
|
|
26 import java.util.HashMap;
|
|
27 import java.util.Map;
|
|
28 import java.util.regex.Matcher;
|
|
29 import java.util.regex.Pattern;
|
|
30
|
|
31
|
|
32 public class Thumbnail extends HttpServlet implements UrlMappable, AuthorizingServlet, CanonicalUrl {
|
|
33
|
|
34 private static final String NABBLE_IMAGE_CODE = "<nabble_img src=\"";
|
|
35 private static final Pattern YOUTUBE_PATTERN = Pattern.compile("[ ]src[ ]*=[ ]*\"(https?:)?//www\\.youtube(-nocookie)?\\.com/(v|embed)/([^&?\"]*)(.*)\"");
|
|
36 private static final Pattern EXTERNAL_IMG_PATTERN = Pattern.compile("<img(\\s|\\w|=|\"|')+src[ ]*=[ ]*[\"|'](\\S+)[\"|']");
|
|
37
|
|
38 private static final Pattern URL_PATTERN = Pattern.compile("/file/thumb/n(\\d+)(/(.+))?$");
|
|
39
|
|
40 public static String url(Node node, String fileName) {
|
|
41 return node.getSite().getBaseUrl() + path(node,fileName);
|
|
42 }
|
|
43
|
|
44 public static String path(Node node, String fileName) {
|
|
45 return "/file/thumb/n" + node.getId() + (fileName == null? "" : '/' + fileName);
|
|
46 }
|
|
47
|
|
48 public String getCanonicalUrl(HttpServletRequest request) {
|
|
49 Site site = Jtp.getSite(request);
|
|
50 if( site == null )
|
|
51 return null;
|
|
52 return url(site.getNode(Long.parseLong(request.getParameter("node"))), request.getParameter("filename"));
|
|
53 }
|
|
54
|
|
55 public Map<String,String[]> getParameterMapFromUrl(HttpServletRequest request,String mappedUrl) {
|
|
56 Matcher m = URL_PATTERN.matcher(mappedUrl);
|
|
57 if (!m.find())
|
|
58 throw new RuntimeException();
|
|
59 Map<String,String[]> params = new HashMap<String,String[]>();
|
|
60 String nodeIdS = m.group(1);
|
|
61 params.put("node", new String[]{nodeIdS});
|
|
62 String fileName = m.group(2);
|
|
63 if (fileName != null && fileName.length() > 1)
|
|
64 params.put("filename", new String[]{fileName.substring(1)});
|
|
65 return params;
|
|
66 }
|
|
67
|
|
68 public Pattern getUrlPattern() {
|
|
69 return URL_PATTERN;
|
|
70 }
|
|
71
|
|
72 public String getAuthorizationKey(HttpServletRequest request) throws ServletException {
|
|
73 return Jtp.getReadAuthorizationKey( Jtp.getSiteNotNull(request).getNode(Jtp.getLong(request,"node")));
|
|
74 }
|
|
75
|
|
76 public boolean authorize(String key,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
77 return Jtp.authorizeForRead(key,request,response);
|
|
78 }
|
|
79
|
|
80 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
81 Node node = Jtp.getSiteNotNull(request).getNode(Jtp.getLong(request, "node"));
|
|
82 if (node == null)
|
|
83 return;
|
|
84
|
|
85 JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName);
|
|
86 jtpContext.setEtag(request,response,Cache.nodeChangeEvent(node));
|
|
87
|
|
88 String fileName = request.getParameter("filename");
|
|
89 BufferedImage img = getImage(node, fileName, response);
|
|
90 if (img == null) {
|
|
91 response.sendError(HttpServletResponse.SC_NOT_FOUND);
|
|
92 return;
|
|
93 }
|
|
94
|
|
95 InputStream in = null;
|
|
96 try {
|
|
97 img = ImageUtils.getThumbnail(img, 140, 100);
|
|
98 in = ImageUtils.asInputStream(img, "jpg");
|
|
99 IoUtils.copyAll(in, response.getOutputStream());
|
|
100 } finally {
|
|
101 if (in != null)
|
|
102 in.close();
|
|
103 }
|
|
104 }
|
|
105
|
|
106 private BufferedImage getImage(Node node, String fileName, HttpServletResponse response)
|
|
107 throws ServletException
|
|
108 {
|
|
109 if (fileName == null) {
|
|
110 try {
|
|
111 String message = node.getMessage().getRaw();
|
|
112 Matcher m = EXTERNAL_IMG_PATTERN.matcher(message);
|
|
113 if (m.find()) {
|
|
114 String imgUrl = m.group(2);
|
|
115 FileItem fi = new FileUpload.UrlFileItem(new URL(imgUrl));
|
|
116 response.setContentType(FileDownload.getMimeByExtension(imgUrl));
|
|
117 return ImageUtils.getImage(fi.getInputStream());
|
|
118 }
|
|
119 } catch(IOException e) {
|
|
120 throw new ServletException("getImage failed",e);
|
|
121 }
|
|
122 } else {
|
|
123 try {
|
|
124 Message.Source src = Message.SourceType.getType('n').getSource(node.getSite(),node.getId());
|
|
125 InputStream in = FileUpload.getFileContent(src,fileName);
|
|
126 if (in != null) {
|
|
127 response.setContentType(FileDownload.getMimeByExtension(fileName));
|
|
128 try {
|
|
129 return ImageUtils.getImage(in);
|
|
130 } finally {
|
|
131 in.close();
|
|
132 }
|
|
133 }
|
|
134 } catch(IOException e) {
|
|
135 throw new RuntimeException("getImage failed",e);
|
|
136 }
|
|
137 }
|
|
138 return null;
|
|
139 }
|
|
140
|
|
141 public static String getThumbnailFile(Node node) {
|
|
142 String src = null;
|
|
143
|
|
144 String message = node.getMessage().getRaw();
|
|
145 int posStart = message.indexOf(NABBLE_IMAGE_CODE);
|
|
146 if (posStart >= 0) {
|
|
147 int srcStart = posStart + NABBLE_IMAGE_CODE.length();
|
|
148 int posEnd = message.indexOf("\"", srcStart);
|
|
149 src = "/file/thumb/n" + node.getId() + '/' + message.substring(srcStart, posEnd);
|
|
150 }
|
|
151 if (src == null) {
|
|
152 // Let's check for youtube videos
|
|
153 Matcher m = YOUTUBE_PATTERN.matcher(message);
|
|
154 if(m.find()) {
|
|
155 String code = m.group(4);
|
|
156 src = "http://i1.ytimg.com/vi/" + code + "/default.jpg";
|
|
157 }
|
|
158 }
|
|
159 if (src == null) {
|
|
160 Matcher m = EXTERNAL_IMG_PATTERN.matcher(message);
|
|
161 if(m.find()) {
|
|
162 return "/file/thumb/n" + node.getId();
|
|
163 }
|
|
164 }
|
|
165 return src;
|
|
166 }
|
|
167 }
|