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.finish()
def serve_static(handler, path, mimetype):
def serve_static(handler, path, mimetype, include_body=True):
#fixme use static file handler / serve ranges
handler.set_header('Content-Type', mimetype)
handler.set_header('Content-Length', str(os.stat(path).st_size))
with open(path) as fd:
handler.write(fd.read())
if include_body:
with open(path) as fd:
handler.write(fd.read())
handler.finish()
return
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():
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.finish()
return
path = item.get_path()
mimetype={
'epub': 'application/epub+zip',
'pdf': 'application/pdf',
'txt': 'text/plain',
}.get(path.split('.')[-1], None)
return serve_static(self, path, mimetype)
return serve_static(self, path, mimetype, include_body)
class ReaderHandler(OMLHandler):

View File

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