Open source CapCut alternative.
TypeScript
feat(site): an imported link brings back its title, cover, and source [rebuild]
David Le876f792fb173
diff --git a/site/src/cut/lib/backend/browser/library.ts b/site/src/cut/lib/backend/browser/library.tsindex ae4f5baa..6a5cc20b 100644--- a/site/src/cut/lib/backend/browser/library.ts+++ b/site/src/cut/lib/backend/browser/library.ts@@ -44,6 +44,9 @@ type StoredAsset = {addedAt: number;folderId?: string | null;source?: LibrarySource;+ /** The source's cover image, stored beside the media and served by the same+ * route — an import from a site that publishes one carries it. */+ posterFile?: string;};type StoredFolder = { id: string; name: string; createdAt: number };@@ -116,6 +119,7 @@ async function list(): Promise<Response> {const idx = await readIndex();for (const name of new Set([...idx.assets.map((a) => a.fileName),+ ...idx.assets.flatMap((a) => (a.posterFile ? [a.posterFile] : [])),...idx.templates.flatMap((t) => t.media.map((m) => m.fileName)),])) {await register(name);@@ -132,7 +136,8 @@ async function addAsset(fileName: string,name: string,meta: { type?: AssetType; duration?: number; width?: number; height?: number } | null,- source?: LibrarySource | null+ source?: LibrarySource | null,+ posterFile?: string): Promise<StoredAsset> {const type = meta?.type ?? typeOf(fileName);if (!type) throw new Error("Unsupported file type.");@@ -146,6 +151,7 @@ async function addAsset(addedAt: Date.now(),folderId: null,...(source ? { source } : {}),+ ...(posterFile ? { posterFile } : {}),};await mutateIndex((idx) => {idx.assets.push(asset);@@ -175,7 +181,16 @@ async function upload(req: Request): Promise<Response> {if (!(meta?.type ?? typeOf(file.name))) return err("Unsupported file type.", 400);const fileName = await store.saveLibraryMedia(file, file.name);await register(fileName, file);- return json(await addAsset(fileName, name, meta, source));+ // An import carries the source's cover alongside the media; it is stored+ // like any other library file and named after the media it belongs to.+ const posterField = form.get("poster");+ let posterFile: string | undefined;+ if (posterField instanceof File) {+ const ext = posterField.name.match(/\.[a-z0-9]+$/i)?.[0] ?? ".jpg";+ posterFile = await store.saveLibraryMedia(posterField, `${fileName}.poster${ext}`);+ await register(posterFile, posterField);+ }+ return json(await addAsset(fileName, name, meta, source, posterFile));} catch (e) {return caught(e, "Upload failed.");}@@ -190,14 +205,18 @@ async function serveMedia(fileName: string, download: boolean): Promise<Responseasync function removeAsset(id: string): Promise<Response> {try {- const fileName = await mutateIndex((idx) => {+ const { fileName, posterFile } = await mutateIndex((idx) => {const asset = idx.assets.find((a) => a.id === id);if (!asset) throw new Error("Library asset not found.");idx.assets = idx.assets.filter((a) => a.id !== id);- return asset.fileName;+ return { fileName: asset.fileName, posterFile: asset.posterFile };});await store.deleteLibraryMedia(fileName);revokeRegistered(libraryMediaApiPath(fileName));+ if (posterFile) {+ await store.deleteLibraryMedia(posterFile).catch(() => {});+ revokeRegistered(libraryMediaApiPath(posterFile));+ }return json({ ok: true });} catch (e) {return caught(e, "Could not delete.");diff --git a/site/src/cut/lib/cloudJob.ts b/site/src/cut/lib/cloudJob.tsindex f2a78692..fc3dbfb1 100644--- a/site/src/cut/lib/cloudJob.ts+++ b/site/src/cut/lib/cloudJob.ts@@ -8,12 +8,15 @@ import { apiJson, type CutBackend } from "./backend";* work, so the client polls: 2s cadence, a ten-minute ceiling, and a run of* failed polls before giving up — a single dropped request keeps polling.* Failure reports what the job itself said, or `fallback`.+ *+ * `onState` sees every state the job reports ("queued", "running", …), so a+ * surface waiting on the job can say which one it is in.*/export async function pollCloudJob<T>(jobId: string,backend: CutBackend,fallback: string,- timedOut = fallback+ { timedOut = fallback, onState }: { timedOut?: string; onState?: (state: string) => void } = {}): Promise<T> {const deadline = Date.now() + 10 * 60 * 1000;const MAX_STRIKES = 6;@@ -35,6 +38,7 @@ export async function pollCloudJob<T>(}strikes = 0;const job = await apiJson<{ state?: string; result?: T }>(res);+ if (job.state) onState?.(job.state);if (job.state === "error") throw new Error(job.error ?? fallback);if (job.state === "done") return (job.result ?? {}) as T;}diff --git a/site/src/cut/lib/library.ts b/site/src/cut/lib/library.tsindex c8a5331b..64fbbc9b 100644--- a/site/src/cut/lib/library.ts+++ b/site/src/cut/lib/library.ts@@ -1,11 +1,11 @@"use client";import { apiJson, getBackend } from "./backend";-import { browserBackend } from "./backend/browser";import { cloudBackend } from "./backend/cloud";import { readSnapshot, writeSnapshot } from "./cache";import { pollCloudJob } from "./cloudJob";import { downloadFromUrl } from "./download";+import { normalizeLink } from "./link";import {enrichAsset,importRemote,@@ -44,6 +44,10 @@ export interface LibraryAsset {addedAt: number;folderId?: string | null;source?: LibrarySource;+ /** The source's own cover image, stored beside the media on the same shelf.+ * An import from a site that publishes one carries it; it is what a card and+ * the viewer show while the video itself loads. */+ posterFile?: string;/** Which shelf this came off. Stamped on arrival — the servers each answer* for themselves and don't know the other exists. */residency: Residency;@@ -69,6 +73,10 @@ export interface LibraryData {export const libraryMediaUrl = (fileName: string, residency: Residency) =>backendFor(residency).url(`/api/cut/library/media/${encodeURIComponent(fileName)}`);+/** The asset's cover image, when the import brought one back. */+export const libraryPosterUrl = (a: Pick<LibraryAsset, "posterFile" | "residency">) =>+ a.posterFile ? libraryMediaUrl(a.posterFile, a.residency) : undefined;+/** Save a library file to the user's Downloads folder, off the shelf it sits* on. */export function downloadLibraryAsset(a: Pick<LibraryAsset, "fileName" | "residency">) {@@ -127,11 +135,29 @@ export async function fetchLibrary(opts?: { remembered?: boolean }): Promise<Lib};}+/** Where an import is in its trip: waiting for a worker, pulling the media+ * down, or writing it onto the shelf. The library shows it on the tile the+ * import already took. */+export type ImportStage = "queued" | "downloading" | "saving";++/** The shape a link is likely to land in, before anything is known about it:+ * the short-form feeds are vertical, everything else is filmed wide. The tile+ * takes the real shape the moment the media lands. */+export function estimatedShape(url: string): { width: number; height: number } {+ return SHORT_FORM_URL.test(url) ? { width: 9, height: 16 } : { width: 16, height: 9 };+}++const SHORT_FORM_URL =+ /^https?:\/\/[^/]*(?:youtube\.com\/shorts\/|tiktok\.com\/|instagram\.com\/(?:reels?|stories)\/)/i;+export async function importUrlToLibrary(url: string,- residency: Residency = activeResidency()+ residency: Residency = activeResidency(),+ onStage?: (stage: ImportStage) => void): Promise<LibraryAsset[]> {- if (residency === "browser") return importUrlToBrowserShelf(url);+ url = normalizeLink(url);+ if (residency === "browser") return importUrlThroughCloud(url, "browser", onStage);+ onStage?.("downloading");const backend = backendFor(residency);const res = await backend.fetch("/api/cut/library/import-url", {method: "POST",@@ -146,7 +172,8 @@ export async function importUrlToLibrary(const done = await pollCloudJob<{ assets?: LibraryAsset[] }>(started.jobId,backend,- "Could not import that URL."+ "Could not import that URL.",+ { onState: (state) => onStage?.(state === "queued" ? "queued" : "downloading") });// The library holds media, so a source that turned out to be only words// fails here — the same line the engine's library import draws.@@ -154,20 +181,38 @@ export async function importUrlToLibrary(return done.assets.map((a) => ({ ...a, residency }));}const body = await apiJson<LibraryAsset[]>(res);- if (!res.ok) throw new Error(body.error ?? "Could not import that URL.");- return (Array.isArray(body) ? body : []).map((a) => ({ ...a, residency }));+ if (res.ok) return (Array.isArray(body) ? body : []).map((a) => ({ ...a, residency }));+ // The engine fetches with the tools it ships, and a site can refuse the+ // build it happens to carry — TikTok reads the requests it impersonates with+ // and answers with nothing usable. The cloud worker fetches with a different+ // one, so the link gets a second chance there and the bytes come back down+ // to this Mac. The engine's own reason stands if that side can't do it+ // either: it is the shelf the user was importing to.+ const reason = body.error ?? "Could not import that URL.";+ try {+ return await importUrlThroughCloud(url, residency, onStage);+ } catch {+ throw new Error(reason);+ }}/**- * A link imported onto the browser shelf.+ * A link fetched by the cloud worker and landed on a shelf of this machine.*- * Nothing in a page can fetch a video off a YouTube or TikTok link, so the- * cloud worker does it — into the account's cloud shelf, where this browser- * can read it — and the bytes come straight back down into this browser's own- * storage. The staged cloud copy is dropped once they land, however this ends,- * so an import leaves nothing behind on a shelf the user wasn't importing to.+ * Nothing in a page can pull a video off a YouTube or TikTok link, so the+ * browser shelf always imports this way; the engine falls back to it when the+ * fetch it runs itself comes back empty-handed. The worker stages the media in+ * the account's cloud shelf, where this browser can read it, and the bytes come+ * straight back down into the target shelf's own storage. The staged cloud copy+ * is dropped once they land, however this ends, so an import leaves nothing+ * behind on a shelf the user wasn't importing to.*/-async function importUrlToBrowserShelf(url: string): Promise<LibraryAsset[]> {+async function importUrlThroughCloud(+ url: string,+ target: Residency,+ onStage?: (stage: ImportStage) => void+): Promise<LibraryAsset[]> {+ onStage?.("downloading");const res = await cloudBackend.fetch("/api/cut/library/import-url", {method: "POST",headers: { "Content-Type": "application/json" },@@ -178,12 +223,14 @@ async function importUrlToBrowserShelf(url: string): Promise<LibraryAsset[]> {const done = await pollCloudJob<{ assets?: LibraryAsset[] }>(started.jobId,cloudBackend,- "Could not import that URL."+ "Could not import that URL.",+ { onState: (state) => onStage?.(state === "queued" ? "queued" : "downloading") });// The library holds media, so a source that turned out to be only words// fails here — the same line the engine's library import draws.if (!done.assets?.length) throw new Error("That link has no media to import.");const landed: LibraryAsset[] = [];+ onStage?.("saving");try {for (const staged of done.assets) {const bytes = await cloudBackend.fetch(@@ -193,6 +240,12 @@ async function importUrlToBrowserShelf(url: string): Promise<LibraryAsset[]> {const form = new FormData();form.append("file", await bytes.blob(), staged.fileName);form.append("name", staged.name);+ if (staged.posterFile) {+ const poster = await cloudBackend.fetch(+ `/api/cut/library/media/${encodeURIComponent(staged.posterFile)}`+ );+ if (poster.ok) form.append("poster", await poster.blob(), staged.posterFile);+ }form.append("meta",JSON.stringify({@@ -203,15 +256,18 @@ async function importUrlToBrowserShelf(url: string): Promise<LibraryAsset[]> {}));if (staged.source) form.append("source", JSON.stringify(staged.source));- const saved = await browserBackend.fetch("/api/cut/library", { method: "POST", body: form });+ const saved = await backendFor(target).fetch("/api/cut/library", {+ method: "POST",+ body: form,+ });const body = await apiJson<LibraryAsset>(saved);if (!saved.ok) throw new Error(body.error ?? "Could not import that URL.");- landed.push({ ...body, residency: "browser" });+ landed.push({ ...body, residency: target });}} catch (e) {// A part-written import is worse than none: the user reads an error and is// left holding half a link.- for (const a of landed) await deleteFromLibrary("browser", a.id).catch(() => {});+ for (const a of landed) await deleteFromLibrary(target, a.id).catch(() => {});throw e;} finally {for (const staged of done.assets) {diff --git a/site/src/cut/lib/link.ts b/site/src/cut/lib/link.tsnew file mode 100644index 00000000..0fc9f8d8--- /dev/null+++ b/site/src/cut/lib/link.ts@@ -0,0 +1,12 @@+/** A pasted address, made absolute. People copy links without the scheme —+ * "youtube.com/watch?v=…" — and every import path takes http(s) only: the+ * engine, the cloud worker, and the client's own label and shape guess all+ * parse the value as a URL. A value that already carries a scheme passes+ * through untouched, so a link that is genuinely unsupported still fails. */+export function normalizeLink(value: string): string {+ const v = value.trim();+ if (!v) return v;+ if (v.startsWith("//")) return `https:${v}`;+ if (/^[a-z][a-z0-9+.-]*:/i.test(v)) return v;+ return `https://${v}`;+}diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.tsindex 717e1465..ef40aed4 100644--- a/site/src/cut/lib/media.ts+++ b/site/src/cut/lib/media.ts@@ -6,6 +6,7 @@ import { cloudBackend } from "./backend/cloud";import { pollCloudJob } from "./cloudJob";import { quotaErrorMessage } from "./backend/cloud";import { encodeWav } from "./cloudTranscribe";+import { normalizeLink } from "./link";import { downloadFromUrl } from "./download";import { startUpload, uploadInFlight } from "./importQueue";import { convertProjectFile, dropProjectFile, mp4DisplayName } from "./mediaConvert";@@ -829,6 +830,7 @@ export async function importUrlMedia(projectId: string,url: string): Promise<{ assets: MediaAsset[]; text?: string }> {+ url = normalizeLink(url);// Pinned at start: the download can outlast navigation into a project of// the other residency, and every poll must hit the backend the job started on.const backend = getBackend();diff --git a/site/src/cut/lib/mediaConvert.ts b/site/src/cut/lib/mediaConvert.tsindex d3f986d1..c7139d94 100644--- a/site/src/cut/lib/mediaConvert.ts+++ b/site/src/cut/lib/mediaConvert.ts@@ -246,7 +246,7 @@ async function convertOnBackend(started.jobId,backend,"Could not convert that file.",- "The conversion took too long."+ { timedOut: "The conversion took too long." });} else {body = (await res.json().catch(() => ({}))) as ServerConvert;diff --git a/site/src/cut/server/cloud/library.ts b/site/src/cut/server/cloud/library.tsindex fcd0aee6..575e8547 100644--- a/site/src/cut/server/cloud/library.ts+++ b/site/src/cut/server/cloud/library.ts@@ -30,6 +30,9 @@ interface AssetMeta {width?: number;height?: number;source?: LibrarySource;+ /** File name of the source's cover image, an object of its own under this+ * account's library prefix. */+ posterFile?: string;}interface TemplateDoc {@@ -74,6 +77,7 @@ function assetView(addedAt: row.createdAt.getTime(),folderId: row.folderId ?? null,...(meta.source ? { source: meta.source } : {}),+ ...(meta.posterFile ? { posterFile: meta.posterFile } : {}),};}@@ -153,15 +157,24 @@ export async function deleteLibraryAssetCascade(const obj = await prisma.cutMediaObject.findFirst({where: { id: asset.mediaObjectId, userId },});- const freed = obj && obj.uploadState === "complete" ? Number(obj.bytes) : 0;+ // The cover image is a second object under the library prefix, referenced+ // only by this row, so it leaves with it.+ const posterName = ((asset.meta ?? {}) as AssetMeta).posterFile;+ const poster = posterName+ ? await prisma.cutMediaObject.findFirst({+ where: { userId, kind: "library", fileName: posterName },+ })+ : null;+ const objects = [obj, poster].filter((o): o is NonNullable<typeof o> => !!o);+ const freed = objects+ .filter((o) => o.uploadState === "complete")+ .reduce((n, o) => n + Number(o.bytes), 0);await prisma.$transaction(async (tx) => {await tx.cutLibraryAsset.delete({ where: { id } });- if (obj) {- await tx.cutMediaObject.delete({ where: { id: obj.id } });- if (freed > 0) await addUsage(tx, userId, -freed);- }+ for (const o of objects) await tx.cutMediaObject.delete({ where: { id: o.id } });+ if (freed > 0) await addUsage(tx, userId, -freed);});- if (obj) await del([obj.r2Key]);+ if (objects.length > 0) await del(objects.map((o) => o.r2Key));return freed;}diff --git a/site/src/cut/server/http/library.ts b/site/src/cut/server/http/library.tsindex cd6c368d..52c7fb8d 100644--- a/site/src/cut/server/http/library.ts+++ b/site/src/cut/server/http/library.ts@@ -16,6 +16,7 @@ import {saveTemplate,useInProject,useTemplate,+ type LibrarySource,type TemplateInput,} from "../library";import { serveFileRange, wantsDownload } from "../serveFile";@@ -138,12 +139,32 @@ export const libraryApi = {}},+ /** A file put on the shelf: an upload from the picker, or media the client+ * fetched elsewhere and is landing here — those carry the title, the source+ * they came from, and the source's cover alongside the bytes. */async upload(req: Request) {try {const form = await req.formData();const file = form.get("file");if (!(file instanceof File)) return err("No file in upload.", 400);- return Response.json(await addUpload(file));+ const name = form.get("name");+ const sourceField = form.get("source");+ let source: LibrarySource | undefined;+ if (typeof sourceField === "string" && sourceField)+ try {+ source = JSON.parse(sourceField) as LibrarySource;+ } catch {+ // Malformed notes are dropped; the media still lands.+ }+ const poster = form.get("poster");+ return Response.json(+ await addUpload(+ file,+ typeof name === "string" ? name : undefined,+ source,+ poster instanceof File ? poster : undefined+ )+ );} catch (e) {return caught(e, "Upload failed.");}diff --git a/site/src/cut/server/library.ts b/site/src/cut/server/library.tsindex 50fc70c2..8669a4af 100644--- a/site/src/cut/server/library.ts+++ b/site/src/cut/server/library.ts@@ -31,6 +31,10 @@ export interface LibraryAsset {addedAt: number;folderId?: string | null;source?: LibrarySource;+ /** The source's own cover image, stored beside the media and served by the+ * same media route — what a card and the viewer show while the video loads.+ * Only an import from a site that publishes one has it. */+ posterFile?: string;}export interface LibraryFolder {@@ -216,7 +220,8 @@ function typeOf(fileName: string): "video" | "audio" | "image" | null {export async function register(fileName: string,name: string,- source?: LibrarySource+ source?: LibrarySource,+ posterFile?: string): Promise<LibraryAsset> {const type = typeOf(fileName);if (!type) throw new Error("Unsupported file type.");@@ -231,6 +236,7 @@ export async function register(addedAt: Date.now(),folderId: null,...(source ? { source } : {}),+ ...(posterFile ? { posterFile } : {}),};await mutateIndex((idx) => {idx.assets.push(asset);@@ -239,12 +245,25 @@ export async function register(}/** Upload a file straight into the library. */-export async function addUpload(file: File): Promise<LibraryAsset> {+export async function addUpload(+ file: File,+ name?: string,+ source?: LibrarySource,+ poster?: File+): Promise<LibraryAsset> {if (!typeOf(file.name)) throw new Error("Unsupported file type.");await mkdir(libMedia(), { recursive: true });const fileName = await freeName(file.name);await writeFile(libMediaPath(fileName), Buffer.from(await file.arrayBuffer()));- return register(fileName, file.name);+ // An import that came down from elsewhere brings the source's cover with it;+ // it is stored like any other library file, named after the media it belongs+ // to, and taken down with it.+ let posterFile: string | undefined;+ if (poster) {+ posterFile = `${fileName}.poster${path.extname(poster.name) || ".jpg"}`;+ await writeFile(libMediaPath(posterFile), Buffer.from(await poster.arrayBuffer()));+ }+ return register(fileName, name?.trim() || file.name, source, posterFile);}/** Copy a project's media file into the library for reuse. */@@ -261,17 +280,26 @@ export async function addFromProject(return register(dest, name || fileName);}-/** Move a freshly downloaded file into the library and register it. */+/** Move a freshly downloaded file into the library and register it, with the+ * source's cover image beside it when the download brought one back. */export async function addDownloaded(srcPath: string,name: string,- source?: LibrarySource+ source?: LibrarySource,+ posterPath?: string): Promise<LibraryAsset> {if (!typeOf(srcPath)) throw new Error("Unsupported file type.");await mkdir(libMedia(), { recursive: true });const dest = await freeName(path.basename(srcPath));await copyFile(srcPath, libMediaPath(dest));- return register(dest, name || path.basename(srcPath), source);+ let posterFile: string | undefined;+ if (posterPath) {+ posterFile = await freeName(`${dest}.poster${path.extname(posterPath)}`);+ await copyFile(posterPath, libMediaPath(posterFile)).catch(() => {+ posterFile = undefined;+ });+ }+ return register(dest, name || path.basename(srcPath), source, posterFile);}/** Copy a library asset into a project's media folder. Returns the file name@@ -289,13 +317,14 @@ export async function useInProject(assetId: string, projectId: string): Promise<}export async function removeAsset(id: string) {- const fileName = await mutateIndex((idx) => {+ const { fileName, posterFile } = await mutateIndex((idx) => {const asset = idx.assets.find((a) => a.id === id);if (!asset) throw new Error("Library asset not found.");idx.assets = idx.assets.filter((a) => a.id !== id);- return asset.fileName;+ return { fileName: asset.fileName, posterFile: asset.posterFile };});await rm(libMediaPath(fileName), { force: true });+ if (posterFile) await rm(libMediaPath(posterFile), { force: true });}export function getAsset(id: string) {diff --git a/site/src/cut/server/urlDownload.ts b/site/src/cut/server/urlDownload.tsindex 975e08ed..f058eab4 100644--- a/site/src/cut/server/urlDownload.ts+++ b/site/src/cut/server/urlDownload.ts@@ -1,5 +1,6 @@import { spawn } from "node:child_process";-import { readdir, stat, writeFile } from "node:fs/promises";+import { accessSync, constants as fsConstants, realpathSync } from "node:fs";+import { mkdir, readdir, stat, writeFile } from "node:fs/promises";import path from "node:path";import { Defuddle } from "defuddle/node";import { parseHTML } from "linkedom";@@ -16,6 +17,7 @@ const MEDIA_EXT = /\.(mp4|mov|m4v|webm|mkv|mp3|m4a|aac|wav|ogg|flac)$/i;interface YtMeta {title?: string;+ thumbnail?: string;description?: string;uploader?: string;upload_date?: string;@@ -26,11 +28,12 @@ interface YtMeta {/** What a finished download hands its consumer: the media files (inside a* temp dir the wrapper deletes afterwards — one for yt-dlp's merged output,* one per photo for a photo post) plus what the extractor knew about them.- * `text` is the source's own words — a post's body, an Article, a video's+ * Each file may carry a `poster`: the source's own cover image, saved beside+ * it. `text` is the source's own words — a post's body, an Article, a video's* title and description — shown in the chat beside the media it came with. A* source that is only words lands with no files and that text alone. */export interface Downloaded {- files: { file: string; title: string }[];+ files: { file: string; title: string; poster?: string }[];source: LibrarySource;text?: string;}@@ -71,6 +74,11 @@ export async function download(url: string, tmp: string): Promise<Downloaded> {if (IMAGE_URL_RE.test(url)) return downloadDirectImage(url, tmp);const rungs: (() => Promise<Downloaded | null>)[] = [];const postId = X_STATUS_RE.exec(url)?.[1];+ // An X post's lower rungs are the post itself — its stills, its words — so a+ // video stream that won't come down still imports as the post. Everywhere+ // else the page under a video host is its thumbnail, and filing that as the+ // import is worse than saying why the video didn't arrive.+ const xLadder = !!postId || X_ARTICLE_RE.test(url);if (postId) {// One syndication fetch feeds every X rung.const post = fetchPost(postId);@@ -91,6 +99,7 @@ export async function download(url: string, tmp: string): Promise<Downloaded> {const got = await rung();if (got) return got;} catch (e) {+ if (e instanceof MediaHostError && !xLadder) throw e;failures.push(e);}}@@ -98,16 +107,57 @@ export async function download(url: string, tmp: string): Promise<Downloaded> {throw first instanceof Error ? first : new Error("Nothing could be imported from that URL.");}+/** yt-dlp knows this site as a video host and still could not bring the video+ * back. The import fails on it rather than falling through to the page. */+class MediaHostError extends Error {}++/** The bundled tool is missing — every retry lands here, so the first attempt+ * is the last. */+class MissingToolError extends Error {}+async function downloadMedia(url: string, tmp: string): Promise<Downloaded> {- const meta = await runYtDlp(url, tmp);+ // Read what sits at the URL before pulling any bytes. The extractor name+ // says whether this is a site that serves video, and that decides what a+ // failed download means: on a video host the import stops with the real+ // reason, on an ordinary page the page rung takes over. A URL yt-dlp can't+ // read at all throws here, so a page import spawns it once.+ let probe: YtMeta;+ let bin: string;+ try {+ ({ meta: probe, bin } = await probeUrl(url));+ } catch (e) {+ // The failure names the extractor that produced it. A media host that+ // could not be read is the end of the import — falling through to the page+ // rung here would land a video site's cover image and its markup in place+ // of the video, and bury the reason the download failed.+ if (e instanceof YtDlpError && e.extractor && !PAGE_EXTRACTORS.has(e.extractor))+ throw new MediaHostError(e.message);+ throw e;+ }+ const videoHost = !PAGE_EXTRACTORS.has((probe.extractor ?? "").toLowerCase());+ let meta: YtMeta;+ let out: string;+ try {+ ({ meta, dir: out } = await runYtDlp(url, tmp, bin));+ } catch (e) {+ if (!videoHost) throw e;+ throw new MediaHostError(e instanceof Error ? e.message : "Download failed.");+ }// Pick the largest media file left behind (the merged output).- const names = (await readdir(tmp)).filter((f) => MEDIA_EXT.test(f));+ const names = (await readdir(out)).filter((f) => MEDIA_EXT.test(f));if (names.length === 0) throw new Error("Nothing downloadable was found at that URL.");const sized = await Promise.all(- names.map(async (n) => ({ n, size: (await stat(path.join(tmp, n))).size }))+ names.map(async (n) => ({ n, size: (await stat(path.join(out, n))).size })));const file = sized.sort((a, b) => b.size - a.size)[0].n;- const clip = { file: path.join(tmp, file), title: (meta.title || "Imported clip").slice(0, 120) };+ // The site's own cover for this video, so a card and the viewer have a first+ // frame to show before a byte of the video is decoded.+ const poster = await savePoster(meta.thumbnail, out, path.parse(file).name);+ const clip = {+ file: path.join(out, file),+ title: (meta.title || "Imported clip").slice(0, 120),+ ...(poster ? { poster } : {}),+ };const source = {url: meta.webpage_url || url,title: meta.title,@@ -130,6 +180,30 @@ const PAGE_EXTRACTORS = new Set(["generic", "html5"]);const IMAGE_URL_RE = /\.(png|jpe?g|webp|gif|avif|bmp)(?:$|\?)/i;+/** Save the source's cover image beside the media it belongs to. A cover that+ * won't download is no reason to fail an import, so this answers nothing and+ * the media lands without one. */+async function savePoster(+ url: string | undefined,+ dir: string,+ base: string+): Promise<string | undefined> {+ if (!url) return undefined;+ try {+ const res = await fetch(url, { signal: AbortSignal.timeout(20_000) });+ if (!res.ok) return undefined;+ const ext = IMAGE_TYPES[(res.headers.get("content-type") ?? "").split(";")[0].toLowerCase()];+ if (!ext) return undefined;+ const bytes = Buffer.from(await res.arrayBuffer());+ if (bytes.byteLength === 0 || bytes.byteLength > MAX_IMAGE_BYTES) return undefined;+ const file = path.join(dir, `${base}.poster.${ext}`);+ await writeFile(file, bytes);+ return file;+ } catch {+ return undefined;+ }+}+async function downloadDirectImage(url: string, dir: string): Promise<Downloaded> {const res = await fetch(url);if (!res.ok) throw new Error("Could not download that image.");@@ -541,21 +615,179 @@ function clamp(text: string, max: number): string {return t.length > max ? `${t.slice(0, max).trimEnd()}…` : t;}-function runYtDlp(url: string, dir: string): Promise<YtMeta> {+// YouTube hands each of its player clients a different set of formats, and the+// one yt-dlp reaches for first can serve URLs that 403 the moment the download+// starts — the site breaks them one at a time. Each entry is a full attempt+// with a different client, so a broken one costs a retry instead of the+// import. Only YouTube reads the argument, so only YouTube pays for the ladder.+const YT_PLAYER_CLIENTS = [null, "mweb", "android"];+const YT_HOST = /(^|\.)(youtube\.com|youtu\.be)$/i;++function playerClients(url: string): (string | null)[] {+ try {+ return YT_HOST.test(new URL(url).hostname) ? YT_PLAYER_CLIENTS : [null];+ } catch {+ return [null];+ }+}++/** Every yt-dlp this machine can run, in PATH order and deduped by the file+ * they point at — the copy we ship comes first, since the engine puts its+ * tools ahead of the rest of PATH.+ *+ * They are not interchangeable. The build we bundle carries curl_cffi, so it+ * impersonates a browser on every request; a copy installed with pip or+ * Homebrew usually can't, and takes yt-dlp's plain path instead. Sites tell+ * the two apart: TikTok currently answers the impersonated request with a page+ * the extractor can't read, while the plain path solves its challenge and gets+ * the video. So a run that fails on one build is worth retrying on the next. */+function ytDlpBinaries(): string[] {+ const seen = new Set<string>();+ const bins: string[] = [];+ for (const dir of (process.env.PATH ?? "").split(path.delimiter)) {+ if (!dir) continue;+ const file = path.join(dir, "yt-dlp");+ try {+ accessSync(file, fsConstants.X_OK);+ const real = realpathSync(file);+ if (seen.has(real)) continue;+ seen.add(real);+ bins.push(file);+ } catch {+ // Not on this entry of PATH.+ }+ }+ return bins;+}++/** Read what sits at a URL, through whichever yt-dlp can read it. Hands back+ * the build that answered so the download starts with the one that works. */+async function probeUrl(url: string): Promise<{ meta: YtMeta; bin: string }> {+ const bins = ytDlpBinaries();+ if (bins.length === 0) throw new MissingToolError("yt-dlp is not available in this build.");+ let first: unknown;+ for (const bin of bins) {+ try {+ return { meta: await ytDlpJson(bin, ["--skip-download", "--dump-json", url]), bin };+ } catch (e) {+ first ??= e;+ }+ }+ throw first instanceof Error ? first : new Error("Nothing could be read at that URL.");+}++// What the whole ladder may spend. Someone is watching a tile the entire time,+// and the cloud rung gives up on a job at ten minutes, so every retry together+// has to finish inside that.+const DOWNLOAD_BUDGET_MS = 8 * 60_000;++/** Pull the media at `url` into a fresh directory under `dir`, working through+ * the builds and player clients until one comes back with the file. Each+ * attempt writes into its own directory: a failed one can leave a finished+ * video-only format behind, and the largest file in the directory is the one+ * that lands — a leftover would beat a smaller merged file that has sound. */+async function runYtDlp(+ url: string,+ dir: string,+ preferred: string+): Promise<{ meta: YtMeta; dir: string }> {+ const bins = [preferred, ...ytDlpBinaries().filter((b) => b !== preferred)];+ const deadline = Date.now() + DOWNLOAD_BUDGET_MS;+ const attempts = bins.flatMap((bin) => playerClients(url).map((client) => ({ bin, client })));+ let first: unknown;+ for (const [i, { bin, client }] of attempts.entries()) {+ const left = deadline - Date.now();+ if (left <= 0) break;+ const out = path.join(dir, `try${i}`);+ await mkdir(out, { recursive: true });+ try {+ return { meta: await ytDlp(bin, url, out, client, left), dir: out };+ } catch (e) {+ if (e instanceof MissingToolError) throw e;+ first ??= e;+ }+ }+ throw first instanceof Error ? first : new Error("Download failed.");+}++function ytDlp(+ bin: string,+ url: string,+ dir: string,+ client: string | null,+ timeoutMs: number+): Promise<YtMeta> {+ return ytDlpJson(bin, [+ ...(client ? ["--extractor-args", `youtube:player_client=${client}`] : []),+ // Take the best video and the best audio and put them together. Asking for+ // a ready-made mp4 first would land whatever single file the site keeps+ // around for compatibility — on YouTube that is 360p next to a 4K original.+ // Codec preference is a sort, never a filter, so an mp4-friendly stream+ // wins ties without a resolution ever being traded for it.+ "-f", "bestvideo*+bestaudio/best",+ "-S", "res,fps,hdr:sdr,vcodec:h264,acodec:aac",+ "--merge-output-format", "mp4",+ "-o", path.join(dir, "%(id)s.%(ext)s"),+ "--print-json",+ url,+ ], timeoutMs);+}++// yt-dlp reports failures as a stderr line aimed at people who file issues on+// its tracker. The library tile shows whatever comes back, so the shapes worth+// naming get a sentence someone can act on and the rest is trimmed down to the+// part that says what happened.+const YT_DLP_REASONS: [RegExp, string][] = [+ [/IP address is blocked|blocked from accessing|rate.?limit|too many requests|HTTP Error 429/i,+ "That site is blocking downloads from this network right now. Try again later."],+ [/private video|login required|sign in|requires authentication|members-only|age.?restrict/i,+ "That post is private or needs a sign-in."],+ [/not available in your country|geo.?restrict|geo.?block/i,+ "That post isn't available in this region."],+ [/video unavailable|does not exist|has been removed|HTTP Error 404|account.*(private|banned)/i,+ "That post isn't available any more."],+ [/unsupported url|no video formats found|no media found/i,+ "That link has nothing this can download."],+ [/unable to extract|unexpected response|failed to parse|rehydration/i,+ "That site changed how it serves this post, so the download tool can't read it."],+];++/** A yt-dlp run that ended in an error, holding the extractor named in its+ * last line — `ERROR: [TikTok] 76720…: …` — so the caller can tell a media+ * host's refusal from an ordinary page yt-dlp found no video in. */+class YtDlpError extends Error {+ constructor(+ message: string,+ readonly extractor: string | null+ ) {+ super(message);+ }+}++function ytDlpExtractor(stderr: string): string | null {+ const line = stderr.split("\n").filter(Boolean).slice(-1)[0] ?? "";+ return line.match(/^ERROR:\s*\[([^\]]+)\]/i)?.[1].toLowerCase() ?? null;+}++function ytDlpReason(stderr: string): string {+ const line = stderr.split("\n").filter(Boolean).slice(-1)[0] ?? "";+ for (const [pattern, reason] of YT_DLP_REASONS) if (pattern.test(line)) return reason;+ const clean = line+ .replace(/^ERROR:\s*/i, "")+ .replace(/^\[[^\]]+\]\s*[^:]*:\s*/, "")+ .split(/;\s*please report/i)[0]+ .trim();+ return clean ? clean.slice(0, 200) : "Download failed.";+}++/** Run yt-dlp and read the info dict it prints. */+function ytDlpJson(bin: string, args: string[], timeoutMs = 300_000): Promise<YtMeta> {return new Promise((resolve, reject) => {- const p = spawn("yt-dlp", [- "--no-playlist",- "--no-progress",- "-f", "mp4/bestvideo*+bestaudio/best",- "--merge-output-format", "mp4",- "-o", path.join(dir, "%(id)s.%(ext)s"),- "--print-json",- url,- ]);+ const p = spawn(bin, ["--no-playlist", "--no-progress", ...args]);const timer = setTimeout(() => {p.kill("SIGKILL");reject(new Error("Download timed out."));- }, 300_000);+ }, timeoutMs);timer.unref();let out = "";let err = "";@@ -563,19 +795,20 @@ function runYtDlp(url: string, dir: string): Promise<YtMeta> {p.stderr.on("data", (d) => (err = (err + d.toString()).slice(-2000)));p.on("error", (e) => {clearTimeout(timer);+ // Bun and Node word a missing binary differently; the code is the same.reject(- e.message.includes("ENOENT")- ? new Error("yt-dlp is not available in this build.")+ (e as NodeJS.ErrnoException).code === "ENOENT"+ ? new MissingToolError("yt-dlp is not available in this build."): e);});p.on("close", (code) => {clearTimeout(timer);if (code !== 0) {- reject(new Error(err.split("\n").filter(Boolean).slice(-1)[0] || "Download failed."));+ reject(new YtDlpError(ytDlpReason(err), ytDlpExtractor(err)));return;}- // --print-json emits the info dict as JSON; take the last JSON line.+ // The info dict comes back as JSON; take the last JSON line.const line = out.trim().split("\n").reverse().find((l) => l.trimStart().startsWith("{"));try {resolve(line ? (JSON.parse(line) as YtMeta) : {});diff --git a/site/src/cut/server/urlImport.ts b/site/src/cut/server/urlImport.tsindex 9a0b4ed2..b344a0d2 100644--- a/site/src/cut/server/urlImport.ts+++ b/site/src/cut/server/urlImport.ts@@ -48,7 +48,9 @@ export async function importFromUrl(url: string): Promise<LibraryAsset[]> {return withDownload(url, async (dl) => {if (dl.files.length === 0) throw new Error("That link has no media to import.");const assets: LibraryAsset[] = [];- for (const f of dl.files) assets.push(await addDownloaded(f.file, f.title, dl.source));+ for (const f of dl.files) {+ assets.push(await addDownloaded(f.file, f.title, dl.source, f.poster));+ }return assets;});}diff --git a/site/src/cut/worker/importUrlJob.ts b/site/src/cut/worker/importUrlJob.tsindex d6ecd407..e5767c0e 100644--- a/site/src/cut/worker/importUrlJob.ts+++ b/site/src/cut/worker/importUrlJob.ts@@ -84,13 +84,34 @@ export async function runImportUrlJob(files.push({ fileName, title: f.title });continue;}+ // The source's cover rides in as its own object under the same+ // library prefix: the media route serves it by name like any other+ // file, and the asset's delete takes it down with the media.+ let posterFile: string | undefined;+ if (f.poster) {+ posterFile = dedupeName(`${fileName}.poster${path.extname(f.poster)}`, taken);+ taken.add(posterFile);+ const posterKey = libraryKey(job.userId, posterFile);+ staged.push(posterKey);+ const posterBytes = await uploadFile(posterKey, f.poster, mimeFor(posterFile));+ await registerObject({+ userId: job.userId,+ projectId: null,+ r2Key: posterKey,+ fileName: posterFile,+ mime: mimeFor(posterFile),+ bytes: posterBytes,+ kind: "library",+ });+ }const asset = await addLibraryRow(job.userId,objectId,fileName,f.file,f.title,- dl.source+ dl.source,+ posterFile);libraryRows.push(asset.id);assets.push(asset);@@ -119,7 +140,8 @@ async function addLibraryRow(fileName: string,localFile: string,title: string,- source: LibraryAsset["source"]+ source: LibraryAsset["source"],+ posterFile?: string): Promise<LibraryAsset> {const type = typeOf(fileName);if (!type) throw new Error("Unsupported file type.");@@ -131,6 +153,7 @@ async function addLibraryRow(duration,...(dims ? { width: dims.width, height: dims.height } : {}),...(source ? { source } : {}),+ ...(posterFile ? { posterFile } : {}),};const row = await prisma.cutLibraryAsset.create({data: {@@ -149,5 +172,6 @@ async function addLibraryRow(addedAt: row.createdAt.getTime(),folderId: null,...(source ? { source } : {}),+ ...(posterFile ? { posterFile } : {}),};}