diff --git a/serve.py b/serve.py index 087bc1a..97aa379 100755 --- a/serve.py +++ b/serve.py @@ -8,6 +8,7 @@ import uuid import logging.config import argparse from concurrent.futures import ThreadPoolExecutor +import mimetypes # user installed libraries import tornado.ioloop @@ -92,10 +93,12 @@ class LoginHandler(BaseHandler): class LogoutHandler(BaseHandler): @tornado.web.authenticated def get(self): - self.learn.logout(self.current_user) self.clear_cookie('user') self.redirect(self.get_argument('next', '/')) + def on_finish(self): + self.learn.logout(self.current_user) + # ---------------------------------------------------------------------------- class ChangePasswordHandler(BaseHandler): @tornado.web.authenticated @@ -159,6 +162,7 @@ class FileHandler(BaseHandler): uid = self.current_user public_dir = self.learn.get_current_public_dir(uid) filepath = path.expanduser(path.join(public_dir, filename)) + try: f = open(filepath, 'rb') except FileNotFoundError: @@ -166,6 +170,11 @@ class FileHandler(BaseHandler): except PermissionError: logging.error(f'No permission: {filepath}') else: + content_type = mimetypes.guess_type(filename) + self.set_header("Content-Type", content_type[0]) + + # divide the file into chunks and write one chunk at a time, so + # that the write does not block the ioloop for very long. with f: chunk = f.read(self.chunk_size) while chunk: @@ -173,12 +182,11 @@ class FileHandler(BaseHandler): self.write(chunk) # write the cunk to response await self.flush() # flush the current chunk to socket except iostream.StreamClosedError: - # client closed the connection - break + break # client closed the connection finally: del chunk await gen.sleep(0.000000001) # 1 nanosecond (hack) - # in tornnado 5.0 use `await asyncio.sleep(0)` instead + # FIXME in the upcomming tornado 5.0 use `await asyncio.sleep(0)` instead chunk = f.read(self.chunk_size) -- libgit2 0.21.2