Mercurial Hosting > nabble
comparison src/nabble/view/web/forum/UploadImage2.jtp @ 0:7ecd1a4ef557
add content
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 21 Mar 2019 19:15:52 -0600 |
| parents | |
| children | 18cf4872fd7f |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:7ecd1a4ef557 |
|---|---|
| 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 <html> | |
| 45 <body> | |
| 46 <script type="text/javascript"> | |
| 47 <% | |
| 48 try { | |
| 49 final Map<String,FileItem> map; | |
| 50 try { | |
| 51 map = Jtp.getFileItems(request); | |
| 52 } catch(FileUploadException e) { | |
| 53 logger.warn("",e); | |
| 54 throw ModelException.newInstance( "upload_image_failed", "upload failed - " + e.getMessage() ); | |
| 55 } | |
| 56 FileItem fi = map.get("image"); | |
| 57 | |
| 58 if (fi == null || fi.getName() == null || fi.getName().length() == 0) { | |
| 59 String urlS = map.get("imageUrl").getString(); | |
| 60 URL url; | |
| 61 try { | |
| 62 url = new URL(urlS); | |
| 63 } catch(MalformedURLException e) { | |
| 64 throw ModelException.newInstance("invalid_url", "Invalid URL: "+urlS, e); | |
| 65 } | |
| 66 fi = new FileUpload.UrlFileItem(url); | |
| 67 try { | |
| 68 fi.getInputStream(); | |
| 69 } catch (IOException e) { | |
| 70 throw ModelException.newInstance("invalid_url", "Invalid URL: "+urlS, e); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 char type = map.get("srcType").getString().charAt(0); | |
| 75 long id = Long.parseLong(map.get("srcId").getString()); | |
| 76 Site site = Jtp.getSiteNotNull(request); | |
| 77 Message.Source src = Message.SourceType.getType(type).getSource(site,id); | |
| 78 | |
| 79 FileItem resizeSizeFI = map.get("resize-size"); | |
| 80 int resize = "none".equals(resizeSizeFI.getString())? 0 : Integer.valueOf(resizeSizeFI.getString()); | |
| 81 String name; | |
| 82 try { | |
| 83 name = FileUpload.uploadImage(fi,src,resize); | |
| 84 } catch (TemplateException e) { | |
| 85 throw ModelException.newInstance( "upload_image_failed", "Upload failed: " + e.getMessage()); | |
| 86 } | |
| 87 | |
| 88 FileItem floatPositionFI = map.get("float-position"); | |
| 89 String floatPosition = floatPositionFI == null? "none" : floatPositionFI.getString(); | |
| 90 | |
| 91 FileItem descriptionFI = map.get("description"); | |
| 92 String description = descriptionFI == null? "null" : '\'' + HtmlUtils.javascriptStringEncode(HtmlUtils.htmlEncode(descriptionFI.getString())) + '\''; | |
| 93 %> | |
| 94 parent.Nabble.uploadedImage('<%=name%>', '<%=floatPosition%>', <%=description%>); | |
| 95 parent.Nabble.closeWindows(); | |
| 96 <% | |
| 97 } catch(ModelException e) { | |
| 98 logger.warn("Image upload failed",e); | |
| 99 %> | |
| 100 alert("<%=HtmlUtils.javascriptStringEncode(e.getMessage())%>"); | |
| 101 history.back(); | |
| 102 <% | |
| 103 } | |
| 104 %> | |
| 105 </script> | |
| 106 </body> | |
| 107 </html> | |
| 108 <% | |
| 109 } | |
| 110 } | |
| 111 %> |
