-
Notifications
You must be signed in to change notification settings - Fork 0
/
tornado_server.py
175 lines (124 loc) · 4.77 KB
/
tornado_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "jiangjun"
import os
import shutil
import tornado.web
import tornado.gen
import tornado.autoreload
import tornado.httpserver
from tornado.options import define, options
define("port", default=8888, type=int)
BUFF_SIZE = 8*1024
class MainHanlder(tornado.web.RequestHandler):
def initialize(self):
self.upload_path = os.path.join(os.getcwd(), "upload")
def get(self):
self.render("index.html")
def post(self):
'''send the file_list to ajax request
'''
file_list = [a for a in os.listdir(self.upload_path)]
self.set_status(200)
self.write("||".join(file_list))
class UploadHandler(tornado.web.RequestHandler):
'''handle the upload request
'''
def initialize(self):
self.upload_path = os.path.join(os.getcwd(), "upload")
self.file_name = self.get_argument("resumableFilename", "data")
self.tmp_dir = os.path.join(
self.upload_path,
self.get_argument("resumableIdentifier")
)
self.chunk = os.path.join(
self.tmp_dir,
"part" + str(self.get_argument("resumableChunkNumber"))
)
self.chunk_num = int(self.get_argument("resumableChunkNumber"))
self.chunk_size = int(self.get_argument("resumableChunkSize"))
self.total_size = int(self.get_argument("resumableTotalSize"))
def prepare(self):
pass
def get(self):
'''first, if the file to be uploaded exists, do nothing.
when the chunk to be uploaded exists, we return status code 200,
otherwise return 404, so that this chunk could be posted again.
'''
if os.path.exists(os.path.join(self.upload_path, self.file_name)):
self.set_status(200)
elif os.path.exists(self.chunk):
self.set_status(200)
else:
self.set_status(404)
@tornado.web.asynchronous
def post(self):
'''save every chunk uploaded to a temporary directory,
and name the file as "part"+resumableChunkNumber
'''
if not os.path.exists(self.tmp_dir):
os.mkdir(self.tmp_dir)
tornado.ioloop.IOLoop.instance().add_callback(self.save_tmp_files)
self.finish()
def save_tmp_files(self):
with open(self.chunk, "wb") as f:
f.write(self.request.files["file"][0]["body"])
self.check_complete()
def check_complete(self):
'''check if all the chunks have been uploaded, if so,
concatenate all the chunks.
'''
uploaded_chunk_size = self.chunk_num * self.chunk_size
current_chunk_size = int(self.get_argument("resumableCurrentChunkSize"))
if uploaded_chunk_size + current_chunk_size >= self.total_size:
save_path = os.path.join(self.upload_path, self.file_name)
with open(save_path, "w+") as f:
for i in range(1, self.chunk_num + 1):
chunk_file = os.path.join(
self.tmp_dir,
"part" + str(i)
)
with open(chunk_file, "rb") as g:
f.write(g.read())
self.remove_tmpfiles()
def remove_tmpfiles(self):
if os.path.exists(self.tmp_dir):
shutil.rmtree(self.tmp_dir)
class DownloadHandler(tornado.web.RequestHandler):
def initialize(self):
self.upload_path = os.path.join(os.getcwd(), "upload")
@tornado.web.asynchronous
@tornado.gen.engine
def get(self, upload_file):
self.set_header('Content-Type', 'application/octet-stream')
self.set_header('Content-Disposition', 'attachment; filename=' + upload_file)
yield tornado.gen.Task(self.send_file, upload_file)
self.finish()
def send_file(self, upload_file, callback):
with open(os.path.join(self.upload_path, upload_file), 'rb') as f:
while True:
data = f.read(BUFF_SIZE)
if not data:
break
self.write(data)
self.flush()
callback()
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", MainHanlder),
(r"/upload", UploadHandler),
(r'/download/(.*)', DownloadHandler)
]
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static"),
"debug": True
}
tornado.web.Application.__init__(self, handlers, **settings)
def main():
tornado.options.parse_command_line()
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()