diff --git a/oml/app.py b/oml/app.py index 074f663..2f3c5f7 100644 --- a/oml/app.py +++ b/oml/app.py @@ -19,7 +19,6 @@ import item.person import api -import item.views import commands #FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s' @@ -30,7 +29,6 @@ logging.basicConfig(level=logging.DEBUG) app = Flask('openmedialibrary', static_folder=settings.static_path) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////%s' % settings.db_path -app.register_blueprint(item.views.app) db.init_app(app) migrate = Migrate(app, db) diff --git a/oml/item/handlers.py b/oml/item/handlers.py index 80087fd..37618dc 100644 --- a/oml/item/handlers.py +++ b/oml/item/handlers.py @@ -41,8 +41,9 @@ class EpubHandler(OMLHandler): self.finish() def serve_static(handler, path, mimetype): - #fixme use static file handler + #fixme use static file handler / serve ranges handler.set_header('Content-Type', mimetype) + handler.set_header('Content-Length', str(os.stat(path).st_size)) with open(path) as fd: handler.write(fd.read()) handler.finish() @@ -61,6 +62,7 @@ class FileHandler(OMLHandler): mimetype={ 'epub': 'application/epub+zip', 'pdf': 'application/pdf', + 'txt': 'text/plain', }.get(path.split('.')[-1], None) return serve_static(self, path, mimetype) diff --git a/oml/item/views.py b/oml/item/views.py deleted file mode 100644 index 2860656..0000000 --- a/oml/item/views.py +++ /dev/null @@ -1,46 +0,0 @@ -# -*- coding: utf-8 -*- -# vi:si:et:sw=4:sts=4:ts=4 -from __future__ import division - -from datetime import datetime - -from flask import Blueprint -from flask import abort, send_file - -import settings - -from models import Item - -app = Blueprint('item', __name__, static_folder=settings.static_path) - - -@app.route('//get') -@app.route('//txt/') -@app.route('//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('//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.accessed = datetime.utcnow() - item.timesaccessed = (item.timesaccessed or 0) + 1 - item.update_sort() - item.save() - return app.send_static_file(html) diff --git a/oml/server.py b/oml/server.py index c78e4b1..21ed1b6 100644 --- a/oml/server.py +++ b/oml/server.py @@ -3,8 +3,8 @@ import os import sys -from tornado.web import StaticFileHandler, Application, FallbackHandler -from tornado.wsgi import WSGIContainer + +from tornado.web import StaticFileHandler, Application from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop @@ -17,7 +17,14 @@ import node.server import oxtornado from item.icons import IconHandler -from item.handlers import EpubHandler +from item.handlers import EpubHandler, ReaderHandler, FileHandler +from item.handlers import OMLHandler, serve_static + +class MainHandler(OMLHandler): + + def get(self, path): + path = os.path.join(settings.static_path, 'html/oml.html') + serve_static(self, path, 'text/html') def run(): root_dir = os.path.normpath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')) @@ -29,16 +36,17 @@ def run(): 'debug': not PID } - tr = WSGIContainer(app) - handlers = [ (r'/(favicon.ico)', StaticFileHandler, {'path': static_path}), (r'/static/(.*)', StaticFileHandler, {'path': static_path}), (r'/(.*)/epub/(.*)', EpubHandler, dict(app=app)), + (r'/(.*?)/reader/', ReaderHandler, dict(app=app)), + (r'/(.*?)/pdf/', FileHandler, dict(app=app)), + (r'/(.*?)/txt/', FileHandler, dict(app=app)), (r'/(.*)/(cover|preview)(\d*).jpg', IconHandler, dict(app=app)), (r'/api/', oxtornado.ApiHandler, dict(app=app)), (r'/ws', websocket.Handler), - (r".*", FallbackHandler, dict(fallback=tr)), + (r"(.*)", MainHandler, dict(app=app)), ] http_server = HTTPServer(Application(handlers, **options)) diff --git a/static/pdf.js/viewer.js b/static/pdf.js/viewer.js index 13cda7b..10c2783 100644 --- a/static/pdf.js/viewer.js +++ b/static/pdf.js/viewer.js @@ -4749,7 +4749,7 @@ document.addEventListener('DOMContentLoaded', function webViewerLoad(evt) { PDFView.initialize(); var params = PDFView.parseQueryString(document.location.search.substring(1)); - var file = params.file || document.location.pathname.replace(/\/reader\//, '/pdf'); + var file = params.file || document.location.pathname.replace(/\/reader\//, '/pdf/'); var fileInput = document.createElement('input');