more tornado
This commit is contained in:
parent
b690aabe1b
commit
0dbc92d027
3 changed files with 24 additions and 45 deletions
|
@ -93,6 +93,7 @@ def get_cover(app, id, size, callback):
|
|||
item = Item.get(id)
|
||||
if not item:
|
||||
callback('')
|
||||
else:
|
||||
data = None
|
||||
if size:
|
||||
data = covers['%s:%s' % (id, size)]
|
||||
|
@ -111,6 +112,7 @@ def get_cover(app, id, size, callback):
|
|||
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):
|
||||
|
|
|
@ -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('/<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>/txt/')
|
||||
|
|
|
@ -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),
|
||||
|
|
Loading…
Reference in a new issue