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
@@ -8,6 +8,7 @@ import uuid @@ -8,6 +8,7 @@ import uuid
8 import logging.config 8 import logging.config
9 import argparse 9 import argparse
10 from concurrent.futures import ThreadPoolExecutor 10 from concurrent.futures import ThreadPoolExecutor
  11 +import mimetypes
11 12
12 # user installed libraries 13 # user installed libraries
13 import tornado.ioloop 14 import tornado.ioloop
@@ -92,10 +93,12 @@ class LoginHandler(BaseHandler): @@ -92,10 +93,12 @@ class LoginHandler(BaseHandler):
92 class LogoutHandler(BaseHandler): 93 class LogoutHandler(BaseHandler):
93 @tornado.web.authenticated 94 @tornado.web.authenticated
94 def get(self): 95 def get(self):
95 - self.learn.logout(self.current_user)  
96 self.clear_cookie('user') 96 self.clear_cookie('user')
97 self.redirect(self.get_argument('next', '/')) 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 class ChangePasswordHandler(BaseHandler): 103 class ChangePasswordHandler(BaseHandler):
101 @tornado.web.authenticated 104 @tornado.web.authenticated
@@ -159,6 +162,7 @@ class FileHandler(BaseHandler): @@ -159,6 +162,7 @@ class FileHandler(BaseHandler):
159 uid = self.current_user 162 uid = self.current_user
160 public_dir = self.learn.get_current_public_dir(uid) 163 public_dir = self.learn.get_current_public_dir(uid)
161 filepath = path.expanduser(path.join(public_dir, filename)) 164 filepath = path.expanduser(path.join(public_dir, filename))
  165 +
162 try: 166 try:
163 f = open(filepath, 'rb') 167 f = open(filepath, 'rb')
164 except FileNotFoundError: 168 except FileNotFoundError:
@@ -166,6 +170,11 @@ class FileHandler(BaseHandler): @@ -166,6 +170,11 @@ class FileHandler(BaseHandler):
166 except PermissionError: 170 except PermissionError:
167 logging.error(f'No permission: {filepath}') 171 logging.error(f'No permission: {filepath}')
168 else: 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 with f: 178 with f:
170 chunk = f.read(self.chunk_size) 179 chunk = f.read(self.chunk_size)
171 while chunk: 180 while chunk:
@@ -173,12 +182,11 @@ class FileHandler(BaseHandler): @@ -173,12 +182,11 @@ class FileHandler(BaseHandler):
173 self.write(chunk) # write the cunk to response 182 self.write(chunk) # write the cunk to response
174 await self.flush() # flush the current chunk to socket 183 await self.flush() # flush the current chunk to socket
175 except iostream.StreamClosedError: 184 except iostream.StreamClosedError:
176 - # client closed the connection  
177 - break 185 + break # client closed the connection
178 finally: 186 finally:
179 del chunk 187 del chunk
180 await gen.sleep(0.000000001) # 1 nanosecond (hack) 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 chunk = f.read(self.chunk_size) 190 chunk = f.read(self.chunk_size)
183 191
184 192