Web: cleaner player, iOS-style mixer, NO STEMS tags, cache-busting
Mixer rows mirror iOS (stem colours, percent, red M, yellow S) and update in place so taps answer at once. Karaoke toggle dropped from the mixer top (the preset covers it). Queue button moves bottom left. Badges mark songs without stems instead of with. Playlist art goes through the photo transcoder with the raw composite as fallback. No backdrop blur, lazy card rendering. stemserve stamps web assets with ?v=<build> (immutable) and sends ETags, so a plain reload picks up new builds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c8157c489
commit
a708d28788
5 files changed
+141
-48
No files matched your search
+43
-5
@@ -30,6 +30,7 @@ import glob
|
||||
import hashlib
|
||||
import json
|
||||
import operator
|
||||
import re
|
||||
import os
|
||||
import shutil
|
||||
import signal
|
||||
@@ -63,6 +64,35 @@ PEAK_RATE = 2000 # Hz the envelope is computed at; plenty for a few hundred bar
|
||||
CHUNK = 1 << 20
|
||||
# The web player. Served without the token; it asks for one in its settings when TOKEN is set.
|
||||
WEB_DIR = os.path.realpath(os.environ.get("WEB_DIR", os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "web")))
|
||||
|
||||
|
||||
# Web player build stamp: changes whenever any file in web/ does.
|
||||
_web_cache = {}
|
||||
|
||||
|
||||
def web_version():
|
||||
try:
|
||||
sig = sorted((e.name, e.stat().st_mtime_ns, e.stat().st_size) for e in os.scandir(WEB_DIR) if e.is_file())
|
||||
except OSError:
|
||||
return "0"
|
||||
return hashlib.sha1(repr(sig).encode()).hexdigest()[:10]
|
||||
|
||||
|
||||
def web_file(full, ver):
|
||||
"""File body with local asset URLs stamped with ?v=ver (html and js), and its ETag."""
|
||||
hit = _web_cache.get(full)
|
||||
if hit and hit[0] == ver:
|
||||
return hit[1], hit[2]
|
||||
with open(full, "rb") as f:
|
||||
body = f.read()
|
||||
ext = os.path.splitext(full)[1]
|
||||
if ext == ".html":
|
||||
body = re.sub(rb'((?:src|href)="(?:\./)?[\w-]+\.(?:js|css))"', rb'\1?v=' + ver.encode() + rb'"', body)
|
||||
elif ext == ".js":
|
||||
body = re.sub(rb'((?:from |import\()"\./[\w-]+\.js)"', rb'\1?v=' + ver.encode() + rb'"', body)
|
||||
etag = '"%s-%s"' % (ver, hashlib.sha1(body).hexdigest()[:8])
|
||||
_web_cache[full] = (ver, body, etag)
|
||||
return body, etag
|
||||
WEB_TYPES = {".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8",
|
||||
".svg": "image/svg+xml", ".png": "image/png", ".ico": "image/x-icon", ".json": "application/json"}
|
||||
|
||||
@@ -315,7 +345,9 @@ class Handler(BaseHTTPRequestHandler):
|
||||
def _error(self, status, msg):
|
||||
self._send(status, json.dumps({"error": msg}).encode())
|
||||
|
||||
def _web(self, path):
|
||||
def _web(self, path, query=""):
|
||||
"""The web player. index.html and the modules point at ?v=<build> URLs, so a new
|
||||
build is picked up on a plain reload and the versioned files cache for good."""
|
||||
if not SERVE_WEB:
|
||||
return self._error(404, "not found")
|
||||
if path == "/":
|
||||
@@ -323,10 +355,16 @@ class Handler(BaseHTTPRequestHandler):
|
||||
full = os.path.realpath(os.path.join(WEB_DIR, path.lstrip("/")))
|
||||
if not full.startswith(WEB_DIR + os.sep) or not os.path.isfile(full):
|
||||
return self._error(404, "not found")
|
||||
with open(full, "rb") as f:
|
||||
body = f.read()
|
||||
ver = web_version()
|
||||
body, etag = web_file(full, ver)
|
||||
ctype = WEB_TYPES.get(os.path.splitext(full)[1], "application/octet-stream")
|
||||
self._send(200, body, ctype, {"Cache-Control": "no-cache"})
|
||||
if "v=" + ver in query.split("&"):
|
||||
cache = "public, max-age=31536000, immutable"
|
||||
else:
|
||||
cache = "no-cache"
|
||||
if self.headers.get("If-None-Match") == etag:
|
||||
return self._send(304, b"", ctype, {"Cache-Control": cache, "ETag": etag})
|
||||
self._send(200, body, ctype, {"Cache-Control": cache, "ETag": etag})
|
||||
|
||||
def do_HEAD(self):
|
||||
self.do_GET()
|
||||
@@ -341,7 +379,7 @@ class Handler(BaseHTTPRequestHandler):
|
||||
if path.startswith("/plex/") and PLEX_URL:
|
||||
return self._plex_proxy(path[len("/plex"):], url.query, query)
|
||||
if not path.startswith(("/api/", "/file/")):
|
||||
return self._web(path)
|
||||
return self._web(path, url.query)
|
||||
if not self._authorized(query):
|
||||
return self._error(HTTPStatus.UNAUTHORIZED, "bad token")
|
||||
try:
|
||||
|
||||
Reference in new issue
Block a user