serve all files without flask

This commit is contained in:
j 2014-05-22 01:06:08 +02:00
parent 08fac3e131
commit f2221188ea
5 changed files with 18 additions and 56 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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('/<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)
item.accessed = datetime.utcnow()
item.timesaccessed = (item.timesaccessed or 0) + 1
item.update_sort()
item.save()
return app.send_static_file(html)

View file

@ -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))

View file

@ -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');