assetcache: add interface for non-Jinja consumers.
Rather than generating the URL in helpers/jinja2.py, we move the logic to helpers/assetcache.py because there are consumers of asset URLs in Python code (for better or for worse).
This commit is contained in:
parent
fcb51c09d3
commit
6f3b995455
2 changed files with 14 additions and 10 deletions
|
@ -3,6 +3,7 @@ import zlib
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
ASSET_DIR = 'files/assets'
|
ASSET_DIR = 'files/assets'
|
||||||
|
ASSET_URL = '/assets/'
|
||||||
ASSET_CACHE = defaultdict(lambda: None)
|
ASSET_CACHE = defaultdict(lambda: None)
|
||||||
|
|
||||||
def assetcache_build(asset_dir):
|
def assetcache_build(asset_dir):
|
||||||
|
@ -14,7 +15,16 @@ def assetcache_build(asset_dir):
|
||||||
fhash = zlib.crc32(f.read())
|
fhash = zlib.crc32(f.read())
|
||||||
ASSET_CACHE[relpath] = '%x' % fhash
|
ASSET_CACHE[relpath] = '%x' % fhash
|
||||||
|
|
||||||
def assetcache_get(path):
|
def assetcache_hash(asset_path):
|
||||||
return ASSET_CACHE[path]
|
return ASSET_CACHE[asset_path]
|
||||||
|
|
||||||
|
def assetcache_path(asset_path):
|
||||||
|
cachehash = assetcache_hash(asset_path)
|
||||||
|
|
||||||
|
url = ASSET_URL + asset_path
|
||||||
|
if cachehash:
|
||||||
|
url += '?v=' + cachehash
|
||||||
|
|
||||||
|
return url
|
||||||
|
|
||||||
assetcache_build(ASSET_DIR)
|
assetcache_build(ASSET_DIR)
|
||||||
|
|
|
@ -3,7 +3,7 @@ from .get import *
|
||||||
from os import listdir, environ
|
from os import listdir, environ
|
||||||
from .const import *
|
from .const import *
|
||||||
import time
|
import time
|
||||||
from files.helpers.assetcache import assetcache_get
|
from files.helpers.assetcache import assetcache_path
|
||||||
|
|
||||||
@app.template_filter("post_embed")
|
@app.template_filter("post_embed")
|
||||||
def post_embed(id, v):
|
def post_embed(id, v):
|
||||||
|
@ -49,13 +49,7 @@ def timestamp(timestamp):
|
||||||
|
|
||||||
@app.template_filter("asset")
|
@app.template_filter("asset")
|
||||||
def template_asset(asset_path):
|
def template_asset(asset_path):
|
||||||
outpath = '/assets/' + asset_path
|
return assetcache_path(asset_path)
|
||||||
|
|
||||||
cachehash = assetcache_get(asset_path)
|
|
||||||
if cachehash:
|
|
||||||
outpath += '?v=' + cachehash
|
|
||||||
|
|
||||||
return outpath
|
|
||||||
|
|
||||||
@app.context_processor
|
@app.context_processor
|
||||||
def inject_constants():
|
def inject_constants():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue