diff --git a/app.py b/app.py index 0cfe64c..78f2fd5 100755 --- a/app.py +++ b/app.py @@ -2,9 +2,11 @@ # -*- coding: utf-8 -*- from functools import wraps +from hashlib import md5 from json import dumps from logging import DEBUG, INFO, getLogger from logging.handlers import TimedRotatingFileHandler +from os import path from time import asctime from traceback import format_exc @@ -27,6 +29,7 @@ hand = TimedRotatingFileHandler("logs/app.log", when="midnight", backupCount=7) hand.setLevel(DEBUG) app.logger.addHandler(hand) app.logger.info(u"Flask server started " + asctime()) +app._hash_cache = {} def catch_errors(func): @wraps(func) @@ -77,6 +80,22 @@ def close_databases(error): if g._db: g._db.close() +def external_url_handler(error, endpoint, **values): + if endpoint == "static" and "file" in values: + fpath = path.join(app.static_folder, values["file"]) + mtime = path.getmtime(fpath) + cache = app._hash_cache.get(fpath) + if cache and cache[0] == mtime: + hashstr = cache[1] + else: + with open(fpath, "rb") as f: + hashstr = md5(f.read()).hexdigest() + app._hash_cache[fpath] = (mtime, hashstr) + return "/static/{0}?v={1}".format(values["file"], hashstr) + raise error + +app.build_error_handler = external_url_handler + @app.route("/") @catch_errors def index(): diff --git a/templates/api.mako b/templates/api.mako index e25b0fc..ca9a080 100644 --- a/templates/api.mako +++ b/templates/api.mako @@ -1,4 +1,7 @@ -<%! from json import dumps %>\ +<%! + from json import dumps + from flask import url_for +%>\ <%def name="do_indent(size)">
% for i in xrange(size): @@ -31,7 +34,7 @@ API - Earwig's Copyvio Detector - + % if help: diff --git a/templates/support/header.mako b/templates/support/header.mako index 6e1718c..3bed3aa 100644 --- a/templates/support/header.mako +++ b/templates/support/header.mako @@ -1,6 +1,6 @@ <%page args="title"/>\ <%! - from flask import g, request + from flask import g, request, url_for from copyvios.background import set_background %>\ @@ -8,9 +8,9 @@ ${title} - + - + <% selected = g.cookies["CopyviosBackground"].value if "CopyviosBackground" in g.cookies else "list" %>\ % if selected == "plain":