Stream refreshed media for playback and view full
This commit is contained in:
@@ -117,6 +117,7 @@ export interface MediaItemResult {
|
||||
contentType: string | null;
|
||||
durationSecs: number | null;
|
||||
url: string;
|
||||
playUrl: string | null;
|
||||
localUrl: string | null;
|
||||
thumbUrl: string | null;
|
||||
provider: string | null;
|
||||
@@ -373,6 +374,89 @@ export class MediaService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A servable media source: the local file, or a remote URL refreshed from the source
|
||||
* message (signed CDN links expire). Null when nothing can be resolved.
|
||||
*/
|
||||
async resolveMediaSource(
|
||||
scrapeId: string,
|
||||
itemId: number
|
||||
): Promise<
|
||||
| { kind: "file"; path: string; contentType: string }
|
||||
| { kind: "remote"; url: string }
|
||||
| null
|
||||
> {
|
||||
const file = this.getMediaFile(scrapeId, itemId);
|
||||
if (file) return { kind: "file", ...file };
|
||||
|
||||
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) {
|
||||
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 };
|
||||
}
|
||||
|
||||
/** Re-fetch the source message and update expired URLs. Returns the fresh playable URL. */
|
||||
private async refreshUrls(
|
||||
scrapeId: string,
|
||||
channelId: string,
|
||||
item: MediaRow
|
||||
): Promise<string | null> {
|
||||
try {
|
||||
const store = new ScrapeStore(scrapeDir(this.rootDir, scrapeId));
|
||||
let messageId: string | null = null;
|
||||
try {
|
||||
messageId = store.sourceMessageId(item.id);
|
||||
} finally {
|
||||
store.close();
|
||||
}
|
||||
if (!messageId || !this.bot) return null;
|
||||
|
||||
const channel = await this.bot.channels.fetch(channelId);
|
||||
if (!channel?.isTextBased()) return null;
|
||||
const message = await channel.messages.fetch(messageId);
|
||||
|
||||
const path = (url: string) => url.split("?")[0];
|
||||
const attachment = [...message.attachments.values()].find(
|
||||
candidate => path(candidate.url) === path(item.url)
|
||||
);
|
||||
if (attachment) {
|
||||
const update = new ScrapeStore(scrapeDir(this.rootDir, scrapeId));
|
||||
try {
|
||||
update.updateUrls(item.id, { url: attachment.url });
|
||||
} finally {
|
||||
update.close();
|
||||
}
|
||||
return attachment.url;
|
||||
}
|
||||
|
||||
const embed = message.embeds.find(
|
||||
candidate =>
|
||||
(item.play_url && candidate.video?.url && path(candidate.video.url) === path(item.play_url)) ||
|
||||
(candidate.video?.url && path(candidate.url ?? "") === path(item.url))
|
||||
);
|
||||
if (embed?.video?.url) {
|
||||
const update = new ScrapeStore(scrapeDir(this.rootDir, scrapeId));
|
||||
try {
|
||||
update.updateUrls(item.id, { playUrl: embed.video.url, url: embed.url ?? item.url });
|
||||
} finally {
|
||||
update.close();
|
||||
}
|
||||
return embed.video.url;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (error) {
|
||||
this.deps.log?.(`refresh failed for ${scrapeId}:${item.id}: ${String(error)}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Absolute path and content type for a downloaded file, or null. */
|
||||
getMediaFile(scrapeId: string, itemId: number): { path: string; contentType: string } | null {
|
||||
// A custom destination is the user's filesystem, outside the archive: not served.
|
||||
@@ -609,6 +693,7 @@ export class MediaService {
|
||||
contentType: row.content_type,
|
||||
durationSecs: row.duration_secs,
|
||||
url: row.url,
|
||||
playUrl: row.play_url,
|
||||
localUrl:
|
||||
row.file_path && !meta.downloadDir
|
||||
? `/api/media/${meta.id}/${row.id}/file`
|
||||
|
||||
Reference in New Issue
Block a user