From 0dbc92d027f577c82eaca788579054813e09dcc9 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 20 May 2014 00:49:04 +0200 Subject: [PATCH] more tornado --- oml/item/covers.py | 38 ++++++++++++++++++++------------------ oml/item/views.py | 29 ++--------------------------- oml/server.py | 2 ++ 3 files changed, 24 insertions(+), 45 deletions(-) diff --git a/oml/item/covers.py b/oml/item/covers.py index 7c950ab..a2a509f 100644 --- a/oml/item/covers.py +++ b/oml/item/covers.py @@ -93,25 +93,27 @@ def get_cover(app, id, size, callback): item = Item.get(id) if not item: callback('') - data = None - if size: - data = covers['%s:%s' % (id, size)] - if data: - size = None - if not data: - data = covers[id] - if not data: - data = item.update_cover() + else: + data = None + if size: + data = covers['%s:%s' % (id, size)] + if data: + size = None if not data: - data = covers.black() - if size: - data = covers['%s:%s' % (id, size)] = resize_image(data, size=size) - data = str(data) - if not 'coverRatio' in item.info: - img = Image.open(StringIO(data)) - item.info['coverRatio'] = img.size[0]/img.size[1] - item.save() - callback(data) + data = covers[id] + if not data: + data = item.update_cover() + if not data: + data = covers.black() + if size: + data = covers['%s:%s' % (id, size)] = resize_image(data, size=size) + data = str(data) + if not 'coverRatio' in item.info: + img = Image.open(StringIO(data)) + item.info['coverRatio'] = img.size[0]/img.size[1] + item.save() + data = data or '' + callback(data) class CoverHandler(tornado.web.RequestHandler): diff --git a/oml/item/views.py b/oml/item/views.py index 9340fd6..bd30df1 100644 --- a/oml/item/views.py +++ b/oml/item/views.py @@ -3,41 +3,16 @@ from __future__ import division from datetime import datetime -import zipfile -import mimetypes -from StringIO import StringIO -import Image from flask import Blueprint -from flask import make_response, abort, send_file -from covers import covers +from flask import abort, send_file import settings -from models import Item, db - -from utils import resize_image +from models import Item app = Blueprint('item', __name__, static_folder=settings.static_path) -@app.route('//epub/') -@app.route('//epub/') -def epub(id, filename=''): - item = Item.get(id) - if not item or item.info['extension'] != 'epub': - abort(404) - - path = item.get_path() - z = zipfile.ZipFile(path) - if filename == '': - return '
\n'.join([f.filename for f in z.filelist]) - if filename not in [f.filename for f in z.filelist]: - abort(404) - resp = make_response(z.read(filename)) - resp.content_type = { - 'xpgt': 'application/vnd.adobe-page-template+xml' - }.get(filename.split('.')[0], mimetypes.guess_type(filename)[0]) or 'text/plain' - return resp @app.route('//get') @app.route('//txt/') diff --git a/oml/server.py b/oml/server.py index 384ee8d..750de92 100644 --- a/oml/server.py +++ b/oml/server.py @@ -16,6 +16,7 @@ import state import node.server import oxtornado from item.covers import CoverHandler +from item.handlers import EpubHandler def run(): root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')) @@ -32,6 +33,7 @@ def run(): handlers = [ (r'/(favicon.ico)', StaticFileHandler, {'path': static_path}), (r'/static/(.*)', StaticFileHandler, {'path': static_path}), + (r'/(.*)/epub/(.*)', EpubHandler, dict(app=app)), (r'/(.*)/cover(\d*).jpg', CoverHandler, dict(app=app)), (r'/api/', oxtornado.ApiHandler, dict(app=app)), (r'/ws', websocket.Handler),