Show album art on the song page, dashboard and history
CI / validate-monitoring (push) Successful in 9s
CI / lint (push) Successful in 14s
CI / federation-interop (push) Successful in 1m7s
CI / test (push) Successful in 3m24s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 6s
CI / validate-monitoring (push) Successful in 9s
CI / lint (push) Successful in 14s
CI / federation-interop (push) Successful in 1m7s
CI / test (push) Successful in 3m24s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 6s
New frontend/js/art.js builds a thumbnail that starts as a placeholder and swaps itself for the real cover once (if) it resolves, fetched via the new /api/v1/art/album endpoint with the existing bearer-auth transport and handed to the browser as a blob: object URL (no query-string tokens, no hotlinking). Wired into all four places a track/album appears: the song page's glowing display, the dashboard's "Most played > Albums" list, the dashboard's "Recently played" list, and the full History page - Artists/Tracks rankings are left alone. Also fixes a real reliability gap found during manual testing: Cover Art Archive's existence check (a HEAD request, redirected through archive.org) had no retry at all, unlike the MusicBrainz search next to it, and an occasional slow/flaky response there was enough to fail a lookup outright. It now retries the same way. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5bdeeddb0b
commit
dfd2ef868b
8 files changed
+160
-17
No files matched your search
@@ -40,6 +40,29 @@ export function redirectToLogin() {
|
||||
location.href = `login.html?next=${encodeURIComponent(safeNext(here + location.search))}`;
|
||||
}
|
||||
|
||||
// Fetches a binary resource (e.g. album art) with the same bearer-auth transport as api(),
|
||||
// returning a blob: object URL, or null on any failure (including a 404 "no art" response) -
|
||||
// the caller treats "missing" and "couldn't fetch" the same way: show a placeholder.
|
||||
export async function fetchImage(path) {
|
||||
const headers = {};
|
||||
const token = getToken();
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = await fetch(`${apiBase}/api/v1${path}`, { headers });
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
if (response.status === 401) {
|
||||
setToken(null);
|
||||
redirectToLogin();
|
||||
return null;
|
||||
}
|
||||
if (!response.ok) return null;
|
||||
return URL.createObjectURL(await response.blob());
|
||||
}
|
||||
|
||||
export async function api(path, { method = "GET", body, auth = true } = {}) {
|
||||
const headers = {};
|
||||
if (body !== undefined) headers["Content-Type"] = "application/json";
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// A small thumbnail that starts as a placeholder and swaps itself for the real album art
|
||||
// once (if) it resolves. Shared by every list/page that shows art.
|
||||
|
||||
import { fetchImage } from "./api.js";
|
||||
import { h } from "./dom.js";
|
||||
|
||||
const cache = new Map(); // dedupes repeat/concurrent lookups for the same album within a page
|
||||
|
||||
export function artThumb(artist, album, extraClass) {
|
||||
const classes = extraClass ? `art-thumb ${extraClass}` : "art-thumb";
|
||||
const el = h("div", { class: `${classes} art-thumb-empty` });
|
||||
if (!album) return el; // nothing to look up
|
||||
|
||||
const key = `${artist.toLowerCase()}␟${album.toLowerCase()}`;
|
||||
if (!cache.has(key)) {
|
||||
cache.set(
|
||||
key,
|
||||
fetchImage(`/art/album?artist=${encodeURIComponent(artist)}&album=${encodeURIComponent(album)}`),
|
||||
);
|
||||
}
|
||||
cache.get(key).then((url) => {
|
||||
if (!url) return; // miss or failed fetch: leave the placeholder
|
||||
const img = h("img", { class: classes, alt: "", loading: "lazy" });
|
||||
img.addEventListener("load", () => URL.revokeObjectURL(url), { once: true });
|
||||
img.src = url;
|
||||
el.replaceWith(img);
|
||||
});
|
||||
return el;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { api } from "./api.js";
|
||||
import { artThumb } from "./art.js";
|
||||
import { errorBox, formatDuration, h, num, replace, timeAgo } from "./dom.js";
|
||||
import { signedInPage } from "./layout.js";
|
||||
import { spectrum } from "./spectrum.js";
|
||||
@@ -157,16 +158,17 @@ async function loadTop(period) {
|
||||
api(`/me/top/tracks?period=${period}&limit=10`),
|
||||
]);
|
||||
renderRanking(document.getElementById("top-artists"), artists.items, false);
|
||||
renderRanking(document.getElementById("top-albums"), albums.items, true);
|
||||
renderRanking(document.getElementById("top-albums"), albums.items, true, false, true);
|
||||
renderRanking(document.getElementById("top-tracks"), tracks.items, true, true);
|
||||
}
|
||||
|
||||
function renderRanking(list, items, withArtist, linkToSong = false) {
|
||||
function renderRanking(list, items, withArtist, linkToSong = false, withArt = false) {
|
||||
if (!items.length) {
|
||||
replace(list, h("li", { class: "empty" }, "No plays in this period."));
|
||||
return;
|
||||
}
|
||||
const max = items[0].playcount;
|
||||
const meterOffset = withArt ? "5.75rem" : "2.75rem";
|
||||
replace(
|
||||
list,
|
||||
items.map((item) => {
|
||||
@@ -177,13 +179,14 @@ function renderRanking(list, items, withArtist, linkToSong = false) {
|
||||
return h(
|
||||
"li",
|
||||
{},
|
||||
withArt ? artThumb(item.artist, item.name) : null,
|
||||
h("span", { class: "rank" }, item.rank),
|
||||
name,
|
||||
h("span", { class: "plays" }, num(item.playcount)),
|
||||
h("span", {
|
||||
class: "meter",
|
||||
"aria-hidden": "true",
|
||||
style: `width: calc((100% - 2.75rem) * ${item.playcount / max})`,
|
||||
style: `width: calc((100% - ${meterOffset}) * ${item.playcount / max})`,
|
||||
}),
|
||||
);
|
||||
}),
|
||||
@@ -261,6 +264,7 @@ async function loadRecent() {
|
||||
h(
|
||||
"li",
|
||||
{},
|
||||
artThumb(s.album_artist || s.artist, s.album),
|
||||
h("time", { datetime: s.played_at, title: new Date(s.played_at).toLocaleString() }, timeAgo(s.played_at)),
|
||||
h(
|
||||
"a",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { api } from "./api.js";
|
||||
import { artThumb } from "./art.js";
|
||||
import { errorBox, formatDay, formatTime, h, num, replace } from "./dom.js";
|
||||
import { signedInPage } from "./layout.js";
|
||||
|
||||
@@ -38,11 +39,12 @@ function render(result) {
|
||||
h("h2", { class: "log-day" }, formatDay(scrobbles[0].played_at)),
|
||||
h(
|
||||
"ol",
|
||||
{ class: "log log-clock" },
|
||||
{ class: "log log-clock log-art" },
|
||||
scrobbles.map((s) =>
|
||||
h(
|
||||
"li",
|
||||
{},
|
||||
artThumb(s.album_artist || s.artist, s.album),
|
||||
h("time", { datetime: s.played_at }, formatTime(s.played_at)),
|
||||
h(
|
||||
"span",
|
||||
|
||||
+6
-2
@@ -1,4 +1,5 @@
|
||||
import { api } from "./api.js";
|
||||
import { artThumb } from "./art.js";
|
||||
import { errorBox, formatDate, formatDay, formatDuration, formatTime, h, num, replace } from "./dom.js";
|
||||
import { signedInPage } from "./layout.js";
|
||||
import { spectrum } from "./spectrum.js";
|
||||
@@ -38,9 +39,9 @@ function render(result) {
|
||||
document.title = `${result.track} · Scrobbler`;
|
||||
const m = result.metadata;
|
||||
|
||||
const display = h(
|
||||
const body = h(
|
||||
"div",
|
||||
{ class: "display" },
|
||||
{ class: "display-body" },
|
||||
h("p", { class: "display-status" }, "Now viewing"),
|
||||
h("p", { class: "display-track" }, result.track),
|
||||
h(
|
||||
@@ -50,6 +51,9 @@ function render(result) {
|
||||
m.album ? [" from ", h("strong", {}, m.album)] : null,
|
||||
),
|
||||
);
|
||||
const display = m.album
|
||||
? h("div", { class: "display display-with-art" }, artThumb(m.album_artist || result.artist, m.album, "display-art"), body)
|
||||
: h("div", { class: "display" }, body);
|
||||
replace(panel, display, readouts(m), spectrum(result.counts, `Plays of ${result.track}`, "90 days"));
|
||||
|
||||
const row = (label, value) =>
|
||||
|
||||
Reference in new issue
Block a user