Respect deliver_album_cover on player page and scale cover to 160px everywhere

This commit is contained in:
Troll (Hermes Agent) 2026-08-11 02:57:38 +00:00
parent 530e9e53ff
commit 8175327ce3
5 changed files with 24 additions and 7 deletions

View file

@ -735,17 +735,29 @@ def format_musicgpt_cost(cost):
return f"${float(cost):.4f} USD"
def download_album_cover(rid, cover_url):
"""Download a MusicGPT album cover to a temp file and return its path."""
def download_album_cover(rid, cover_url, max_width=160):
"""
Download a MusicGPT album cover to a temp file, resize it so it is not
oversized in emails or pages, and return its path. The default max_width
of 160px matches the inline album cover display size on the player/admin pages.
"""
if not cover_url:
return None
try:
import tempfile
from PIL import Image
resp = requests.get(cover_url, timeout=30)
if resp.status_code == 200:
ext = Path(cover_url).suffix or '.jpg'
tmp = Path(tempfile.gettempdir()) / f"cover_{rid}{ext}"
tmp.write_bytes(resp.content)
# Resize if the image is wider than max_width to keep email/file size small.
with Image.open(tmp) as img:
if img.width > max_width:
ratio = max_width / img.width
new_height = int(img.height * ratio)
img = img.resize((max_width, new_height))
img.save(tmp)
return str(tmp)
except Exception:
pass