-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
77 lines (71 loc) · 3.19 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import base64
import datetime
import hashlib
import os
from subprocess import Popen, PIPE
import tornado.ioloop
import tornado.web
from tornado.options import define, options
import xattr
class MainHandler(tornado.web.RequestHandler):
def get(self, arg, head=False):
if not arg:
self.redirect("/post.html")
path = os.path.join('files',arg)
try:
with open(path,"rb") as f:
attrs = xattr.xattr(f)
self.set_header("Expires", datetime.datetime.utcnow() + datetime.timedelta(1000000))
if 'user.Content-Type' in attrs:
mimetype = attrs['user.Content-Type'].decode('utf-8')
else:
mimetype = Popen(["file","-b","--mime-type", path], stdout=PIPE).communicate()[0].decode('utf8').strip()
self.set_header("Content-Type", mimetype)
try:
orig_filename = attrs.get('user.filename').decode('utf-8')
self.set_header('Content-Disposition',' inline; filename="{}"'.format(orig_filename))
except IOError:
pass
self.set_header('content-length',os.stat(f.fileno()).st_size)
if head:
self.finish()
return
self.write(f.read())
self.finish()
except IOError:
raise tornado.web.HTTPError(404)
def head(self, arg):
self.get(arg, head=True)
def post(self, arg):
file_body = self.request.arguments.get('data')[0]
if not file_body:
self.finish()
return
filename = base64.urlsafe_b64encode(hashlib.sha256(file_body).digest()).decode('utf-8')
with open(os.path.join('files', filename), "wb") as f:
f.write(file_body)
f.flush()
attrs = xattr.xattr(f)
mimetype = Popen(["file", "-b","--mime-type", f.name], stdout=PIPE).communicate()[0].decode('utf8').strip()
attrs['user.Content-Type'] = mimetype.encode('utf-8')
self.write('<html><body><a href="http://h45h.com/{}"></body></html>{}'.format(filename,filename))
def put(self, arg):
filename = base64.urlsafe_b64encode(hashlib.sha256(self.request.body).digest()).decode('utf-8')
with open(os.path.join('files',filename),"wb") as f:
f.write(self.request.body)
attrs = xattr.xattr(f)
mimetype = Popen(["file", "-b","--mime-type", f.name], stdout=PIPE).communicate()[0].decode('utf8').strip()
attrs['user.Content-Type'] = mimetype.encode('utf-8')
attrs['user.filename'] = arg.encode('utf-8')
self.write('http://h45h.com/{}\n'.format(filename))
application = tornado.web.Application([
(r"/(.*\.html)", tornado.web.StaticFileHandler, dict(path=os.path.join(os.path.dirname(__file__)))),
(r"/(.*)", MainHandler),
], debug=True)
if __name__ == "__main__":
tornado.options.parse_command_line()
path = os.path.join(os.path.join(os.path.dirname(__file__)), 'files')
if not os.path.exists(path):
os.makedirs(path)
application.listen(8888, xheaders=True)
tornado.ioloop.IOLoop.instance().start()