0
|
1
|
|
2 package nabble.view.web.forum;
|
|
3
|
|
4 import fschmidt.util.java.HtmlUtils;
|
|
5 import fschmidt.util.servlet.JtpContext;
|
|
6 import nabble.model.FileUpload;
|
|
7 import nabble.model.Message;
|
|
8 import nabble.model.ModelException;
|
|
9 import nabble.model.Site;
|
|
10 import nabble.model.User;
|
|
11 import nabble.naml.namespaces.TemplateException;
|
|
12 import nabble.view.lib.Jtp;
|
|
13 import org.apache.commons.fileupload.FileItem;
|
|
14 import org.apache.commons.fileupload.FileUploadException;
|
|
15 import org.slf4j.Logger;
|
|
16 import org.slf4j.LoggerFactory;
|
|
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.io.IOException;
|
|
23 import java.io.PrintWriter;
|
|
24 import java.net.MalformedURLException;
|
|
25 import java.net.URL;
|
|
26 import java.util.Map;
|
|
27
|
|
28
|
|
29 public final class UploadImage2 extends HttpServlet {
|
|
30 private static final Logger logger = LoggerFactory.getLogger(UploadImage2.class);
|
|
31
|
|
32 protected void service(HttpServletRequest request,HttpServletResponse response)
|
|
33 throws ServletException, IOException
|
|
34 {
|
|
35 JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName);
|
|
36 jtpContext.setTimeLimit(request,0L);
|
|
37 User user = Jtp.getUser(request,response);
|
|
38 if( user==null ) {
|
|
39 Jtp.login("You must login to upload an image.",request,response);
|
|
40 return;
|
|
41 }
|
|
42 PrintWriter out = response.getWriter();
|
|
43
|
|
44 out.print( "\r\n<html>\r\n<body>\r\n<script type=\"text/javascript\">\r\n" );
|
|
45
|
|
46 try {
|
|
47 final Map<String,FileItem> map;
|
|
48 try {
|
|
49 map = Jtp.getFileItems(request);
|
|
50 } catch(FileUploadException e) {
|
|
51 logger.warn("",e);
|
|
52 throw ModelException.newInstance( "upload_image_failed", "upload failed - " + e.getMessage() );
|
|
53 }
|
|
54 FileItem fi = map.get("image");
|
|
55
|
|
56 if (fi == null || fi.getName() == null || fi.getName().length() == 0) {
|
|
57 String urlS = map.get("imageUrl").getString();
|
|
58 URL url;
|
|
59 try {
|
|
60 url = new URL(urlS);
|
|
61 } catch(MalformedURLException e) {
|
|
62 throw ModelException.newInstance("invalid_url", "Invalid URL: "+urlS, e);
|
|
63 }
|
|
64 fi = new FileUpload.UrlFileItem(url);
|
|
65 try {
|
|
66 fi.getInputStream();
|
|
67 } catch (IOException e) {
|
|
68 throw ModelException.newInstance("invalid_url", "Invalid URL: "+urlS, e);
|
|
69 }
|
|
70 }
|
|
71
|
|
72 char type = map.get("srcType").getString().charAt(0);
|
|
73 long id = Long.parseLong(map.get("srcId").getString());
|
|
74 Site site = Jtp.getSiteNotNull(request);
|
|
75 Message.Source src = Message.SourceType.getType(type).getSource(site,id);
|
|
76
|
|
77 FileItem resizeSizeFI = map.get("resize-size");
|
|
78 int resize = "none".equals(resizeSizeFI.getString())? 0 : Integer.valueOf(resizeSizeFI.getString());
|
|
79 String name;
|
|
80 try {
|
|
81 name = FileUpload.uploadImage(fi,src,resize);
|
|
82 } catch (TemplateException e) {
|
|
83 throw ModelException.newInstance( "upload_image_failed", "Upload failed: " + e.getMessage());
|
|
84 }
|
|
85
|
|
86 FileItem floatPositionFI = map.get("float-position");
|
|
87 String floatPosition = floatPositionFI == null? "none" : floatPositionFI.getString();
|
|
88
|
|
89 FileItem descriptionFI = map.get("description");
|
|
90 String description = descriptionFI == null? "null" : '\'' + HtmlUtils.javascriptStringEncode(HtmlUtils.htmlEncode(descriptionFI.getString())) + '\'';
|
|
91
|
|
92 out.print( "\r\nparent.Nabble.uploadedImage('" );
|
|
93 out.print( (name) );
|
|
94 out.print( "', '" );
|
|
95 out.print( (floatPosition) );
|
|
96 out.print( "', " );
|
|
97 out.print( (description) );
|
|
98 out.print( ");\r\nparent.Nabble.closeWindows();\r\n" );
|
|
99
|
|
100 } catch(ModelException e) {
|
|
101 logger.warn("Image upload failed",e);
|
|
102
|
|
103 out.print( "\r\nalert(\"" );
|
|
104 out.print( (HtmlUtils.javascriptStringEncode(e.getMessage())) );
|
|
105 out.print( "\");\r\nhistory.back();\r\n" );
|
|
106
|
|
107 }
|
|
108
|
|
109 out.print( "\r\n</script>\r\n</body>\r\n</html>\r\n" );
|
|
110
|
|
111 }
|
|
112 }
|
|
113
|