]>
Commit | Line | Data |
---|---|---|
60024cc8 MT |
1 | #!/usr/bin/python |
2 | ||
3 | import tornado.web | |
4 | ||
9b8ff27d | 5 | from . import auth |
124a8404 | 6 | from . import base |
e1814f16 | 7 | from . import ui_modules |
60024cc8 | 8 | |
ed4f64dd | 9 | class CreateHandler(auth.CacheMixin, base.BaseHandler): |
b0b3e3e7 MT |
10 | MODES = ("paste", "upload") |
11 | ||
60024cc8 | 12 | def get(self): |
b0b3e3e7 MT |
13 | mode = self.get_argument("mode", "paste") |
14 | if not mode in self.MODES: | |
15 | raise tornado.web.HTTPError(400) | |
16 | ||
17 | self.render("nopaste/create.html", mode=mode, | |
18 | max_size=self._max_size) | |
60024cc8 | 19 | |
372ef119 | 20 | @base.ratelimit(minutes=15, requests=5) |
60024cc8 | 21 | def post(self): |
b0b3e3e7 MT |
22 | mode = self.get_argument("mode") |
23 | if not mode in self.MODES: | |
24 | raise tornado.web.HTTPError(400) | |
25 | ||
bed82f3e MT |
26 | mimetype = "text/plain" |
27 | ||
b0b3e3e7 MT |
28 | if mode == "paste": |
29 | subject = self.get_argument("subject", None) | |
5e0260ae | 30 | content = self.get_argument("content", "").encode("utf-8") |
b0b3e3e7 MT |
31 | |
32 | elif mode == "upload": | |
b0b3e3e7 | 33 | for f in self.request.files.get("file"): |
bed82f3e MT |
34 | subject = f.filename |
35 | content = f.body | |
36 | mimetype = f.content_type | |
b0b3e3e7 MT |
37 | break |
38 | ||
39 | # Check maximum size | |
40 | if len(content) > self._max_size: | |
41 | raise tornado.web.HTTPError(400, | |
42 | "You cannot upload more than %s bytes" % self._max_size) | |
66862195 MT |
43 | |
44 | expires = self.get_argument("expires", "0") | |
45 | try: | |
46 | expires = int(expires) | |
47 | except (TypeError, ValueError): | |
48 | expires = None | |
49 | ||
bed82f3e MT |
50 | uid = self.backend.nopaste.create(subject, content, mimetype=mimetype, |
51 | expires=expires, account=self.current_user, address=self.get_remote_ip()) | |
66862195 MT |
52 | |
53 | if uid: | |
54 | return self.redirect("/view/%s" % uid) | |
55 | ||
56 | raise tornado.web.HTTPError(500) | |
57 | ||
b0b3e3e7 MT |
58 | @property |
59 | def _max_size(self): | |
a41085fb | 60 | # Authenticated users are allowed to upload up to 25MB |
b0b3e3e7 | 61 | if self.current_user: |
e72c1f45 | 62 | return 25 * (1024 ** 2) |
b0b3e3e7 MT |
63 | |
64 | # The rest is only allowed to upload up to 2MB | |
65 | return 2 * (1024 ** 2) | |
66 | ||
66862195 | 67 | |
a41085fb | 68 | class RawHandler(base.BaseHandler): |
66862195 MT |
69 | def get(self, uid): |
70 | entry = self.backend.nopaste.get(uid) | |
71 | if not entry: | |
72 | raise tornado.web.HTTPError(404) | |
73 | ||
6f3eb7f9 MT |
74 | # Set the filename |
75 | self.set_header("Content-Disposition", "inline; filename=\"%s\"" % entry.subject) | |
76 | ||
66862195 MT |
77 | # Set mimetype |
78 | self.set_header("Content-Type", entry.mimetype) | |
79 | ||
e2708eb5 | 80 | # Set expiry headers |
a41085fb | 81 | self.set_expires(3600) |
e2708eb5 | 82 | |
66862195 | 83 | # Send content |
0de07bc3 MT |
84 | content = self.backend.nopaste.get_content(entry.uuid) |
85 | self.finish(content) | |
60024cc8 MT |
86 | |
87 | ||
9b8ff27d | 88 | class ViewHandler(auth.CacheMixin, base.BaseHandler): |
66862195 MT |
89 | def get(self, uid): |
90 | entry = self.backend.nopaste.get(uid) | |
91 | if not entry: | |
92 | raise tornado.web.HTTPError(404) | |
60024cc8 | 93 | |
0de07bc3 MT |
94 | # Fetch the content if the output should be displayed |
95 | if entry.mimetype.startswith("text/"): | |
96 | content = self.backend.nopaste.get_content(entry.uuid) | |
97 | else: | |
98 | content = None | |
99 | ||
9b8ff27d MT |
100 | # Set expiry headers |
101 | self.set_expires(3600) | |
102 | ||
0de07bc3 | 103 | self.render("nopaste/view.html", entry=entry, content=content) |
e1814f16 MT |
104 | |
105 | ||
106 | class CodeModule(ui_modules.UIModule): | |
107 | def render(self, content): | |
108 | return self.render_string("nopaste/modules/code.html", content=content) | |
109 | ||
110 | def javascript_files(self): | |
111 | return "js/prettify.js" | |
112 | ||
113 | def css_files(self): | |
114 | return "css/prettify.css" | |
115 | ||
116 | def embedded_javascript(self): | |
117 | return """ | |
118 | // Run pretty print | |
119 | prettyPrint(); | |
120 | """ |