Prefer cached stills and stream first frames in the grid

This commit is contained in:
Fisto
2026-09-16 13:44:34 +02:00
parent 0081224954
commit 1e064b69d0
3 changed files with 71 additions and 47 deletions
+10 -6
View File
@@ -391,16 +391,20 @@ export class MediaService {
const file = this.getMediaFile(scrapeId, itemId);
if (file) return { kind: "file", ...file };
const item = this.getMediaItem(scrapeId, itemId);
if (!item) return null;
return { kind: "remote", url: item.play_url ?? item.url };
}
/** Re-fetch the source message for a fresh signed URL. Null when not possible. */
async refreshMediaUrl(scrapeId: string, itemId: number): Promise<string | null> {
const meta = this.engine.get(scrapeId);
const item = this.getMediaItem(scrapeId, itemId);
if (!meta || !item) return null;
if (meta.backend !== "slopbot" || !meta.guildId || !this.bot) return null;
if (meta.backend === "slopbot" && meta.guildId && this.bot) {
const refreshed = await this.refreshUrls(meta.id, meta.channelId, item);
if (refreshed) return { kind: "remote", url: refreshed };
}
return { kind: "remote", url: item.play_url ?? item.url };
return this.refreshUrls(meta.id, meta.channelId, item);
}
/** Re-fetch the source message and update expired URLs. Returns the fresh playable URL. */
+14 -3
View File
@@ -789,9 +789,20 @@ export function startWebUi(deps: WebUiDeps): WebUiHandle {
return;
}
const upstream = await fetch(source.url, {
headers: req.headers.range ? { range: String(req.headers.range) } : {}
});
const rangeHeaders: Record<string, string> = req.headers.range
? { range: String(req.headers.range) }
: {};
let upstream = await fetch(source.url, { headers: rangeHeaders });
// The signed CDN URL may have expired; re-fetch the message once and retry.
if (!upstream.ok && [403, 404, 410].includes(upstream.status)) {
const refreshed = await deps.media.refreshMediaUrl(
String(req.params.scrapeId),
Number(req.params.itemId)
);
if (refreshed) upstream = await fetch(refreshed, { headers: rangeHeaders });
}
if (!upstream.ok && upstream.status !== 206) {
throw new HttpError(502, `upstream ${upstream.status}`);
}