Mercurial Hosting > nabble
comparison src/nabble/view/web/forum/UploadFile2.java @ 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 java.io.IOException; | |
| 5 import java.io.PrintWriter; | |
| 6 import java.util.Map; | |
| 7 import javax.servlet.ServletException; | |
| 8 import javax.servlet.http.HttpServlet; | |
| 9 import javax.servlet.http.HttpServletRequest; | |
| 10 import javax.servlet.http.HttpServletResponse; | |
| 11 import org.slf4j.Logger; | |
| 12 import org.slf4j.LoggerFactory; | |
| 13 import org.apache.commons.fileupload.FileItem; | |
| 14 import org.apache.commons.fileupload.FileUploadException; | |
| 15 import fschmidt.util.servlet.JtpContext; | |
| 16 import fschmidt.util.java.HtmlUtils; | |
| 17 import nabble.view.lib.Jtp; | |
| 18 import nabble.view.lib.Shared; | |
| 19 import nabble.model.FileUpload; | |
| 20 import nabble.model.Message; | |
| 21 import nabble.model.ModelException; | |
| 22 import nabble.model.User; | |
| 23 | |
| 24 public final class UploadFile2 extends HttpServlet { | |
| 25 private static final Logger logger = LoggerFactory.getLogger(UploadFile2.class); | |
| 26 | |
| 27 protected void service(HttpServletRequest request,HttpServletResponse response) | |
| 28 throws ServletException, IOException | |
| 29 { | |
| 30 User user = Jtp.getUser(request,response); | |
| 31 if( user==null ) { | |
| 32 Jtp.login("You must login to upload a file.",request,response); | |
| 33 return; | |
| 34 } | |
| 35 PrintWriter out = response.getWriter(); | |
| 36 | |
| 37 out.print( "\r\n<html>\r\n <body>\r\n <script type=\"text/javascript\">\r\n " ); | |
| 38 | |
| 39 try { | |
| 40 final Map<String,FileItem> map; | |
| 41 try { | |
| 42 map = Jtp.getFileItems(request); | |
| 43 } catch(FileUploadException e) { | |
| 44 logger.warn("",e); | |
| 45 throw ModelException.newInstance( "upload_filed", "upload failed - " + e.getMessage() ); | |
| 46 } | |
| 47 FileItem fi = map.get("file"); | |
| 48 char type = map.get("srcType").getString().charAt(0); | |
| 49 long id = Long.parseLong(map.get("srcId").getString()); | |
| 50 Message.Source src = Message.SourceType.getType(type).getSource(user.getSite(),id); | |
| 51 String name = FileUpload.uploadFile(fi,src); | |
| 52 JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName); | |
| 53 jtpContext.setTimeLimit(request,0L); | |
| 54 | |
| 55 if (map.containsKey("textAreaId")) { | |
| 56 out.print( "\r\n parent.Nabble.uploadedFile(\"" ); | |
| 57 out.print( (name) ); | |
| 58 out.print( "\", \"" ); | |
| 59 out.print( (map.get("textAreaId").getString()) ); | |
| 60 out.print( "\");\r\n " ); | |
| 61 } else { | |
| 62 out.print( "\r\n parent.Nabble.uploadedFile(\"" ); | |
| 63 out.print( (name) ); | |
| 64 out.print( "\");\r\n " ); | |
| 65 } | |
| 66 out.print( "\r\n parent.Nabble.closeWindows();\r\n " ); | |
| 67 | |
| 68 } catch(ModelException e) { | |
| 69 logger.warn("file upload failed",e); | |
| 70 | |
| 71 out.print( "\r\nalert(\"" ); | |
| 72 out.print( (HtmlUtils.javascriptStringEncode(e.getMessage())) ); | |
| 73 out.print( "\");\r\nhistory.back();\r\n" ); | |
| 74 | |
| 75 } | |
| 76 | |
| 77 out.print( "\r\n</script>\r\n</body>\r\n</html>\r\n" ); | |
| 78 | |
| 79 } | |
| 80 } | |
| 81 |
