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:
TLSM 2022-08-30 16:23:09 -04:00
parent fcb51c09d3
commit 6f3b995455
No known key found for this signature in database
GPG key ID: E745A82778055C7E
2 changed files with 14 additions and 10 deletions

View file

@ -3,6 +3,7 @@ import zlib
from collections import defaultdict
ASSET_DIR = 'files/assets'
ASSET_URL = '/assets/'
ASSET_CACHE = defaultdict(lambda: None)
def assetcache_build(asset_dir):
@ -14,7 +15,16 @@ def assetcache_build(asset_dir):
fhash = zlib.crc32(f.read())
ASSET_CACHE[relpath] = '%x' % fhash
def assetcache_get(path):
return ASSET_CACHE[path]
def assetcache_hash(asset_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)

View file

@ -3,7 +3,7 @@ from .get import *
from os import listdir, environ
from .const import *
import time
from files.helpers.assetcache import assetcache_get
from files.helpers.assetcache import assetcache_path
@app.template_filter("post_embed")
def post_embed(id, v):
@ -49,13 +49,7 @@ def timestamp(timestamp):
@app.template_filter("asset")
def template_asset(asset_path):
outpath = '/assets/' + asset_path
cachehash = assetcache_get(asset_path)
if cachehash:
outpath += '?v=' + cachehash
return outpath
return assetcache_path(asset_path)
@app.context_processor
def inject_constants():