more tornado

This commit is contained in:
j 2014-05-20 00:49:04 +02:00
parent b690aabe1b
commit 0dbc92d027
3 changed files with 24 additions and 45 deletions

View file

@ -93,25 +93,27 @@ def get_cover(app, id, size, callback):
item = Item.get(id) item = Item.get(id)
if not item: if not item:
callback('') callback('')
data = None else:
if size: data = None
data = covers['%s:%s' % (id, size)] if size:
if data: data = covers['%s:%s' % (id, size)]
size = None if data:
if not data: size = None
data = covers[id]
if not data:
data = item.update_cover()
if not data: if not data:
data = covers.black() data = covers[id]
if size: if not data:
data = covers['%s:%s' % (id, size)] = resize_image(data, size=size) data = item.update_cover()
data = str(data) if not data:
if not 'coverRatio' in item.info: data = covers.black()
img = Image.open(StringIO(data)) if size:
item.info['coverRatio'] = img.size[0]/img.size[1] data = covers['%s:%s' % (id, size)] = resize_image(data, size=size)
item.save() data = str(data)
callback(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): class CoverHandler(tornado.web.RequestHandler):

View file

@ -3,41 +3,16 @@
from __future__ import division from __future__ import division
from datetime import datetime from datetime import datetime
import zipfile
import mimetypes
from StringIO import StringIO
import Image
from flask import Blueprint from flask import Blueprint
from flask import make_response, abort, send_file from flask import abort, send_file
from covers import covers
import settings import settings
from models import Item, db from models import Item
from utils import resize_image
app = Blueprint('item', __name__, static_folder=settings.static_path) app = Blueprint('item', __name__, static_folder=settings.static_path)
@app.route('/<string:id>/epub/')
@app.route('/<string:id>/epub/<path:filename>')
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 '<br>\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('/<string:id>/get') @app.route('/<string:id>/get')
@app.route('/<string:id>/txt/') @app.route('/<string:id>/txt/')

View file

@ -16,6 +16,7 @@ import state
import node.server import node.server
import oxtornado import oxtornado
from item.covers import CoverHandler from item.covers import CoverHandler
from item.handlers import EpubHandler
def run(): def run():
root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')) root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..'))
@ -32,6 +33,7 @@ def run():
handlers = [ handlers = [
(r'/(favicon.ico)', StaticFileHandler, {'path': static_path}), (r'/(favicon.ico)', StaticFileHandler, {'path': static_path}),
(r'/static/(.*)', 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'/(.*)/cover(\d*).jpg', CoverHandler, dict(app=app)),
(r'/api/', oxtornado.ApiHandler, dict(app=app)), (r'/api/', oxtornado.ApiHandler, dict(app=app)),
(r'/ws', websocket.Handler), (r'/ws', websocket.Handler),