first commit
This commit is contained in:
18
app/services/assets.py
Normal file
18
app/services/assets.py
Normal file
@@ -0,0 +1,18 @@
|
||||
import hashlib
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
from flask import url_for
|
||||
|
||||
|
||||
@lru_cache(maxsize=128)
|
||||
def asset_version(file_path: str) -> str:
|
||||
path = Path(file_path)
|
||||
if not path.exists():
|
||||
return '0'
|
||||
return hashlib.md5(path.read_bytes()).hexdigest()[:10]
|
||||
|
||||
|
||||
def asset_url(relative_path: str) -> str:
|
||||
base_path = Path(__file__).resolve().parent.parent / 'static' / relative_path
|
||||
return url_for('static', filename=relative_path, v=asset_version(str(base_path)))
|
||||
Reference in New Issue
Block a user