Commit aa7b797ee7f19b64b3cb60b2baeeff19124dab2a

Authored by Miguel Barão
1 parent c36cfb56
Exists in master and in 1 other branch dev

- fixed bug where mimetype was not defined when serving files

Showing 1 changed file with 12 additions and 4 deletions   Show diff stats
serve.py
... ... @@ -8,6 +8,7 @@ import uuid
8 8 import logging.config
9 9 import argparse
10 10 from concurrent.futures import ThreadPoolExecutor
  11 +import mimetypes
11 12  
12 13 # user installed libraries
13 14 import tornado.ioloop
... ... @@ -92,10 +93,12 @@ class LoginHandler(BaseHandler):
92 93 class LogoutHandler(BaseHandler):
93 94 @tornado.web.authenticated
94 95 def get(self):
95   - self.learn.logout(self.current_user)
96 96 self.clear_cookie('user')
97 97 self.redirect(self.get_argument('next', '/'))
98 98  
  99 + def on_finish(self):
  100 + self.learn.logout(self.current_user)
  101 +
99 102 # ----------------------------------------------------------------------------
100 103 class ChangePasswordHandler(BaseHandler):
101 104 @tornado.web.authenticated
... ... @@ -159,6 +162,7 @@ class FileHandler(BaseHandler):
159 162 uid = self.current_user
160 163 public_dir = self.learn.get_current_public_dir(uid)
161 164 filepath = path.expanduser(path.join(public_dir, filename))
  165 +
162 166 try:
163 167 f = open(filepath, 'rb')
164 168 except FileNotFoundError:
... ... @@ -166,6 +170,11 @@ class FileHandler(BaseHandler):
166 170 except PermissionError:
167 171 logging.error(f'No permission: {filepath}')
168 172 else:
  173 + content_type = mimetypes.guess_type(filename)
  174 + self.set_header("Content-Type", content_type[0])
  175 +
  176 + # divide the file into chunks and write one chunk at a time, so
  177 + # that the write does not block the ioloop for very long.
169 178 with f:
170 179 chunk = f.read(self.chunk_size)
171 180 while chunk:
... ... @@ -173,12 +182,11 @@ class FileHandler(BaseHandler):
173 182 self.write(chunk) # write the cunk to response
174 183 await self.flush() # flush the current chunk to socket
175 184 except iostream.StreamClosedError:
176   - # client closed the connection
177   - break
  185 + break # client closed the connection
178 186 finally:
179 187 del chunk
180 188 await gen.sleep(0.000000001) # 1 nanosecond (hack)
181   - # in tornnado 5.0 use `await asyncio.sleep(0)` instead
  189 + # FIXME in the upcomming tornado 5.0 use `await asyncio.sleep(0)` instead
182 190 chunk = f.read(self.chunk_size)
183 191  
184 192  
... ...