Normalize YouTube Shorts links to watch URLs
This commit is contained in:
@@ -36,6 +36,15 @@ export interface ExtractedMedia {
|
||||
title: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorts links break the embed player: it needs `watch?v=`. Rewrite them for storage so the
|
||||
* viewer, "View full", and refreshes all use the canonical form. Other URLs pass through.
|
||||
*/
|
||||
export function normalizeYouTubeUrl(url: string): string {
|
||||
const match = /^https?:\/\/(?:www\.|m\.)?youtube\.com\/shorts\/([A-Za-z0-9_-]{6,})/.exec(url);
|
||||
return match ? `https://www.youtube.com/watch?v=${match[1]}` : url;
|
||||
}
|
||||
|
||||
function extension(filename: string | null | undefined): string {
|
||||
const match = /\.([a-z0-9]+)$/i.exec(filename ?? "");
|
||||
return match ? match[1].toLowerCase() : "";
|
||||
@@ -75,9 +84,11 @@ function hostname(url: string | null): string | null {
|
||||
|
||||
function extractEmbed(embed: CoreEmbed): ExtractedMedia | null {
|
||||
const provider = embed.provider ?? hostname(embed.url);
|
||||
const embedUrl = embed.url ? normalizeYouTubeUrl(embed.url) : null;
|
||||
const videoUrl = embed.videoUrl ? normalizeYouTubeUrl(embed.videoUrl) : null;
|
||||
const common = {
|
||||
source: "embed" as const,
|
||||
playUrl: embed.videoUrl ?? null,
|
||||
playUrl: videoUrl,
|
||||
provider,
|
||||
downloaded: false,
|
||||
size: null,
|
||||
@@ -91,7 +102,7 @@ function extractEmbed(embed: CoreEmbed): ExtractedMedia | null {
|
||||
return {
|
||||
...common,
|
||||
kind: "video",
|
||||
url: embed.url ?? embed.videoUrl,
|
||||
url: embedUrl ?? videoUrl ?? "",
|
||||
contentType: null,
|
||||
previewUrl: embed.thumbnailUrl ?? embed.imageUrl
|
||||
};
|
||||
@@ -102,7 +113,7 @@ function extractEmbed(embed: CoreEmbed): ExtractedMedia | null {
|
||||
return {
|
||||
...common,
|
||||
kind: "gif",
|
||||
url: embed.url ?? embed.imageUrl ?? "",
|
||||
url: embedUrl ?? embed.imageUrl ?? "",
|
||||
contentType: null,
|
||||
previewUrl: embed.thumbnailUrl ?? embed.imageUrl
|
||||
};
|
||||
@@ -112,7 +123,7 @@ function extractEmbed(embed: CoreEmbed): ExtractedMedia | null {
|
||||
return {
|
||||
...common,
|
||||
kind: "image",
|
||||
url: embed.url ?? embed.imageUrl,
|
||||
url: embedUrl ?? embed.imageUrl,
|
||||
contentType: null,
|
||||
previewUrl: embed.thumbnailUrl ?? embed.imageUrl
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user