openmedialibrary/oml/item/views.py

46 lines
1.2 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2014-05-16 08:06:11 +00:00
from __future__ import division
2014-05-04 17:26:43 +00:00
from datetime import datetime
from flask import Blueprint
2014-05-19 22:49:04 +00:00
from flask import abort, send_file
2014-05-04 17:26:43 +00:00
import settings
2014-05-19 22:49:04 +00:00
from models import Item
2014-05-04 17:26:43 +00:00
app = Blueprint('item', __name__, static_folder=settings.static_path)
@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>/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)
2014-05-20 00:43:54 +00:00
item.sort_accessed = item.accessed = datetime.utcnow()
2014-05-04 17:26:43 +00:00
item.sort_timesaccessed = item.timesaccessed = (item.timesaccessed or 0) + 1
item.save()
return app.send_static_file(html)