openmedialibrary/oml/item/views.py
2014-05-16 10:06:11 +02:00

102 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
import os
from datetime import datetime
import zipfile
import mimetypes
from StringIO import StringIO
import Image
from flask import Blueprint
from flask import json, request, make_response, abort, send_file
from covers import covers
import settings
from models import Item, db
from utils import resize_image
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/')
@app.route('/<string:id>/pdf')
def get(id):
item = Item.get(id)
if not item:
abort(404)
path = item.get_path()
mimetype={
'epub': 'application/epub+zip',
'pdf': 'application/pdf',
}.get(path.split('.')[-1], None)
return send_file(path, mimetype=mimetype)
@app.route('/<string:id>/cover.jpg')
@app.route('/<string:id>/cover<int:size>.jpg')
def cover(id, size=None):
item = Item.get(id)
if not item:
abort(404)
data = None
if size:
data = covers['%s:%s' % (id, size)]
if data:
size = None
if not data:
data = covers[id]
if not data:
print 'check for cover', id
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.meta:
#img = Image.open(StringIO(str(covers[id])))
img = Image.open(StringIO(data))
item.meta['coverRatio'] = img.size[0]/img.size[1]
db.session.add(item)
db.session.commit()
resp = make_response(data)
resp.content_type = "image/jpeg"
return resp
@app.route('/<string:id>/reader/')
def reader(id, filename=''):
item = Item.get(id)
if item.info['extension'] == 'epub':
html = 'html/epub.html'
elif item.info['extension'] == 'pdf':
html = 'html/pdf.html'
elif item.info['extension'] == 'txt':
html = 'html/txt.html'
else:
abort(404)
item.sort_accessed = item.accessed = datetime.now()
item.sort_timesaccessed = item.timesaccessed = (item.timesaccessed or 0) + 1
item.save()
return app.send_static_file(html)