]> git.ipfire.org Git - ipfire.org.git/blame_incremental - src/web/nopaste.py
wiki: Remove superfluous slash when creating user links
[ipfire.org.git] / src / web / nopaste.py
... / ...
CommitLineData
1#!/usr/bin/python
2
3import tornado.web
4
5from . import base
6from . import ui_modules
7
8class CreateHandler(base.AnalyticsMixin, base.BaseHandler):
9 @tornado.web.authenticated
10 def get(self):
11 self.render("nopaste/create.html")
12
13 @tornado.web.authenticated
14 @base.ratelimit(minutes=15, requests=5)
15 def post(self):
16 subject = self.get_argument("subject", None)
17 content = self.get_argument("content")
18
19 # Fetch expires time
20 expires = self.get_argument_int("expires", "0")
21
22 with self.db.transaction():
23 paste = self.backend.nopaste.create(content, subject=subject, expires=expires,
24 account=self.current_user, address=self.get_remote_ip())
25
26 # Redirect to the paste
27 return self.redirect("/view/%s" % paste.uuid)
28
29 # cURL Interface
30
31 def get_current_user(self):
32 if self.request.method == "PUT":
33 return self.perform_basic_authentication()
34
35 # Perform the usual authentication
36 return super().get_current_user()
37
38 def check_xsrf_cookie(self):
39 # Skip the check on PUT
40 if self.request.method == "PUT":
41 return
42
43 # Perform the check as usual
44 super().check_xsrf_cookie()
45
46 def write_error(self, *args, **kwargs):
47 if self.request.method == "PUT":
48 return
49
50 # Write errors as usual
51 return super().write_error(*args, **kwargs)
52
53 @tornado.web.authenticated
54 @base.ratelimit(minutes=15, requests=5)
55 def put(self):
56 with self.db.transaction():
57 paste = self.backend.nopaste.create(
58 self.request.body, account=self.current_user, address=self.get_remote_ip())
59
60 # Send a message to the client
61 self.write("https://%s/view/%s\n" % (self.request.host, paste.uuid))
62
63 # Tell the user when this expires
64 if paste.expires_at:
65 self.write(" This paste will expire at %s\n" % self.locale.format_date(paste.expires_at))
66
67 # All done
68 self.finish()
69
70
71class UploadHandler(base.AnalyticsMixin, base.BaseHandler):
72 @tornado.web.authenticated
73 def get(self):
74 self.render("nopaste/upload.html")
75
76 @tornado.web.authenticated
77 def post(self):
78 subject = self.get_argument("subject", None)
79
80 # Fetch expires time
81 expires = self.get_argument_int("expires", "0")
82
83 with self.db.transaction():
84 for f in self.request.files.get("file", []):
85 paste = self.backend.nopaste.create(f.body, subject=subject, expires=expires,
86 account=self.current_user, address=self.get_remote_ip())
87
88 # Only accept one file
89 break
90
91 # Complain if no file was selected
92 else:
93 raise tornado.web.HTTPError(400, "No file uploaded")
94
95 # Redirect to the paste
96 return self.redirect("/view/%s" % paste.uuid)
97
98
99class RawHandler(base.AnalyticsMixin, base.BaseHandler):
100 def get(self, uid):
101 with self.db.transaction():
102 paste = self.backend.nopaste.get(uid)
103 if not paste:
104 raise tornado.web.HTTPError(404)
105
106 # This has received a view
107 paste.viewed()
108
109 # Set the filename
110 self.set_header("Content-Disposition", "inline; filename=\"%s\"" % paste.subject)
111
112 # Set mimetype
113 self.set_header("Content-Type", paste.mimetype)
114
115 # Send content
116 self.finish(paste.blob)
117
118
119class ViewHandler(base.AnalyticsMixin, base.BaseHandler):
120 def get(self, uid):
121 with self.db.transaction():
122 paste = self.backend.nopaste.get(uid)
123 if not paste:
124 raise tornado.web.HTTPError(404)
125
126 # This has received a view
127 paste.viewed()
128
129 self.render("nopaste/view.html", paste=paste)
130
131
132class CodeModule(ui_modules.UIModule):
133 def render(self, content):
134 return self.render_string("nopaste/modules/code.html", content=content)
135
136 def javascript_files(self):
137 return "js/prettify.js"
138
139 def css_files(self):
140 return "css/prettify.css"
141
142 def embedded_javascript(self):
143 return """
144 // Run pretty print
145 prettyPrint();
146 """