async img serving

This commit is contained in:
j 2014-05-25 22:32:00 +02:00
parent af3c133d02
commit 833faa5a6c
2 changed files with 15 additions and 14 deletions

View file

@ -40,31 +40,35 @@ class EpubHandler(OMLHandler):
self.write(z.read(filename)) self.write(z.read(filename))
self.finish() self.finish()
def serve_static(handler, path, mimetype): def serve_static(handler, path, mimetype, include_body=True):
#fixme use static file handler / serve ranges #fixme use static file handler / serve ranges
handler.set_header('Content-Type', mimetype) handler.set_header('Content-Type', mimetype)
handler.set_header('Content-Length', str(os.stat(path).st_size)) handler.set_header('Content-Length', str(os.stat(path).st_size))
with open(path) as fd: if include_body:
handler.write(fd.read()) with open(path) as fd:
handler.write(fd.read())
handler.finish() handler.finish()
return return
class FileHandler(OMLHandler): class FileHandler(OMLHandler):
def get(self, id): def head(self, id):
self.get(id, include_body=False)
def get(self, id, include_body=True):
with self._app.app_context(): with self._app.app_context():
item = Item.get(id) item = Item.get(id)
if not item: path = item.get_path() if item else None
if not item or not path:
self.set_status(404) self.set_status(404)
self.finish() self.finish()
return return
path = item.get_path()
mimetype={ mimetype={
'epub': 'application/epub+zip', 'epub': 'application/epub+zip',
'pdf': 'application/pdf', 'pdf': 'application/pdf',
'txt': 'text/plain', 'txt': 'text/plain',
}.get(path.split('.')[-1], None) }.get(path.split('.')[-1], None)
return serve_static(self, path, mimetype) return serve_static(self, path, mimetype, include_body)
class ReaderHandler(OMLHandler): class ReaderHandler(OMLHandler):

View file

@ -127,24 +127,21 @@ class IconHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous @tornado.web.asynchronous
@tornado.gen.coroutine @tornado.gen.coroutine
def get(self, id, type_, size=None): def get(self, id, type_, size=None):
def fail():
self.set_status(404)
self.write('')
self.finish()
size = int(size) if size else None size = int(size) if size else None
if type_ not in ('cover', 'preview'): if type_ not in ('cover', 'preview'):
fail() self.set_status(404)
self.finish()
return return
self.set_header('Content-Type', 'image/jpeg') self.set_header('Content-Type', 'image/jpeg')
response = yield tornado.gen.Task(get_icon, self._app, id, type_, size) response = yield tornado.gen.Task(get_icon, self._app, id, type_, size)
if not response: if not response:
fail() self.set_status(404)
self.finish()
return return
if self._finished: if self._finished:
return return
self.write(response) self.write(response)
self.finish()