Fix broken YouTube thumbnails (#3248)

This commit is contained in:
hfcRed
2026-07-25 06:52:43 -04:00
committed by GitHub
parent 3f6674a394
commit 519067f99f
15 changed files with 38 additions and 1 deletions

View File

@@ -33,6 +33,30 @@ describe("extractYoutubeIdFromVideoUrl", () => {
expect(result).toBe("dQw4w9WgXcQ");
});
it("should strip share tracking params from a shortened YouTube URL", () => {
const url = "https://youtu.be/fuj_pSAbU-A?si=mAzDxgrIJWLO1ykq";
const result = extractYoutubeIdFromVideoUrl(url);
expect(result).toBe("fuj_pSAbU-A");
});
it("should strip extra query params from a standard YouTube URL", () => {
const url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=120";
const result = extractYoutubeIdFromVideoUrl(url);
expect(result).toBe("dQw4w9WgXcQ");
});
it("should strip query params from a YouTube live URL", () => {
const url = "https://www.youtube.com/live/dQw4w9WgXcQ?feature=shared";
const result = extractYoutubeIdFromVideoUrl(url);
expect(result).toBe("dQw4w9WgXcQ");
});
it("should strip url fragments", () => {
const url = "https://youtu.be/dQw4w9WgXcQ#t=1m";
const result = extractYoutubeIdFromVideoUrl(url);
expect(result).toBe("dQw4w9WgXcQ");
});
it("should return null for an invalid YouTube URL", () => {
const url = "https://www.example.com/watch?v=dQw4w9WgXcQ";
const result = extractYoutubeIdFromVideoUrl(url);

View File

@@ -52,7 +52,7 @@ export function canEditVideo({
export function extractYoutubeIdFromVideoUrl(url: string): string | null {
const match = url.match(
/^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|live\/)|youtu\.be\/)([^&/?]+)/,
/^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/(?:watch\?v=|live\/)|youtu\.be\/)([^&/?#]+)/,
);
return match ? match[1] : null;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,13 @@
export function up(db) {
db.transaction(() => {
for (const delimiter of ["?", "&", "#"]) {
db.prepare(
/* sql */ `
update "UnvalidatedVideo"
set "youtubeId" = substr("youtubeId", 1, instr("youtubeId", ?) - 1)
where instr("youtubeId", ?) > 0
`,
).run(delimiter, delimiter);
}
})();
}