surge refill
This commit is contained in:
@@ -188,3 +188,36 @@ def validate_offline_assets() -> None:
|
||||
"Run: ./scripts/download_frontend_libs.py or ./install.sh\n"
|
||||
f"Missing files:\n{preview}{extra}"
|
||||
)
|
||||
|
||||
|
||||
_STATIC_HASH_CACHE: dict[tuple[str, int], str] = {}
|
||||
|
||||
def static_hash(static_root: Path | None = None) -> str:
|
||||
"""Return one short hash for all app static files.
|
||||
|
||||
Note: This value is used as the shared browser-cache version, so any static file
|
||||
change invalidates app.js imports, CSS and local frontend assets together.
|
||||
"""
|
||||
import hashlib
|
||||
|
||||
root = static_root or (BASE_DIR / "pytorrent" / "static")
|
||||
files = [path for path in root.rglob("*") if path.is_file() and "tracker_favicons" not in path.parts]
|
||||
fingerprint = f"{root}:{sum(path.stat().st_mtime_ns for path in files)}:{sum(path.stat().st_size for path in files)}"
|
||||
cached = _STATIC_HASH_CACHE.get((fingerprint, len(files)))
|
||||
if cached:
|
||||
return cached
|
||||
digest = hashlib.sha256()
|
||||
for path in sorted(files):
|
||||
rel = path.relative_to(root).as_posix()
|
||||
stat = path.stat()
|
||||
digest.update(rel.encode("utf-8"))
|
||||
digest.update(str(stat.st_size).encode("ascii"))
|
||||
digest.update(str(stat.st_mtime_ns).encode("ascii"))
|
||||
try:
|
||||
digest.update(path.read_bytes())
|
||||
except OSError:
|
||||
continue
|
||||
value = digest.hexdigest()[:16]
|
||||
_STATIC_HASH_CACHE.clear()
|
||||
_STATIC_HASH_CACHE[(fingerprint, len(files))] = value
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user