@@ -2,9 +2,11 @@ | |||||
# -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||
from functools import wraps | from functools import wraps | ||||
from hashlib import md5 | |||||
from json import dumps | from json import dumps | ||||
from logging import DEBUG, INFO, getLogger | from logging import DEBUG, INFO, getLogger | ||||
from logging.handlers import TimedRotatingFileHandler | from logging.handlers import TimedRotatingFileHandler | ||||
from os import path | |||||
from time import asctime | from time import asctime | ||||
from traceback import format_exc | from traceback import format_exc | ||||
@@ -27,6 +29,7 @@ hand = TimedRotatingFileHandler("logs/app.log", when="midnight", backupCount=7) | |||||
hand.setLevel(DEBUG) | hand.setLevel(DEBUG) | ||||
app.logger.addHandler(hand) | app.logger.addHandler(hand) | ||||
app.logger.info(u"Flask server started " + asctime()) | app.logger.info(u"Flask server started " + asctime()) | ||||
app._hash_cache = {} | |||||
def catch_errors(func): | def catch_errors(func): | ||||
@wraps(func) | @wraps(func) | ||||
@@ -77,6 +80,22 @@ def close_databases(error): | |||||
if g._db: | if g._db: | ||||
g._db.close() | 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("/") | @app.route("/") | ||||
@catch_errors | @catch_errors | ||||
def index(): | def index(): | ||||
@@ -1,4 +1,7 @@ | |||||
<%! from json import dumps %>\ | |||||
<%! | |||||
from json import dumps | |||||
from flask import url_for | |||||
%>\ | |||||
<%def name="do_indent(size)"> | <%def name="do_indent(size)"> | ||||
<br /> | <br /> | ||||
% for i in xrange(size): | % for i in xrange(size): | ||||
@@ -31,7 +34,7 @@ | |||||
<head> | <head> | ||||
<meta charset="utf-8"> | <meta charset="utf-8"> | ||||
<title>API - Earwig's Copyvio Detector</title> | <title>API - Earwig's Copyvio Detector</title> | ||||
<link rel="stylesheet" href="${request.script_root}/static/api.min.css" type="text/css" /> | |||||
<link rel="stylesheet" href="${request.script_root}$(url_for('static', file='api.min.css')" type="text/css" /> | |||||
</head> | </head> | ||||
<body> | <body> | ||||
% if help: | % if help: | ||||
@@ -1,6 +1,6 @@ | |||||
<%page args="title"/>\ | <%page args="title"/>\ | ||||
<%! | <%! | ||||
from flask import g, request | |||||
from flask import g, request, url_for | |||||
from copyvios.background import set_background | from copyvios.background import set_background | ||||
%>\ | %>\ | ||||
<!DOCTYPE html> | <!DOCTYPE html> | ||||
@@ -8,9 +8,9 @@ | |||||
<head> | <head> | ||||
<meta charset="utf-8"> | <meta charset="utf-8"> | ||||
<title>${title}</title> | <title>${title}</title> | ||||
<link rel="stylesheet" href="${request.script_root}/static/style.min.css" type="text/css" /> | |||||
<link rel="stylesheet" href="${request.script_root}$(url_for('static', file='style.min.css')" type="text/css" /> | |||||
<script src="//code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script> | <script src="//code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script> | ||||
<script src="${request.script_root}/static/script.min.js" type="text/javascript"></script> | |||||
<script src="${request.script_root}/$(url_for('static', file='script.min.js')" type="text/javascript"></script> | |||||
</head> | </head> | ||||
<% selected = g.cookies["CopyviosBackground"].value if "CopyviosBackground" in g.cookies else "list" %>\ | <% selected = g.cookies["CopyviosBackground"].value if "CopyviosBackground" in g.cookies else "list" %>\ | ||||
% if selected == "plain": | % if selected == "plain": | ||||