Skip to content

Open source CapCut alternative.

TypeScript

feat(site): a browser project imports a URL through the cloud worker [rebuild]

David Le8eb7d2d96a29
diff --git a/site/src/cut/lib/api.ts b/site/src/cut/lib/api.ts
index 7acc0c0c..06004da4 100644
--- a/site/src/cut/lib/api.ts
+++ b/site/src/cut/lib/api.ts
@@ -175,22 +175,42 @@ export async function apiFetch(path: string, init?: RequestInit) {
return fetch(`${base}${scopedPath(path)}`, init);
}
-/** JSON body of an engine reply. A reply that never reached a handler is plain
- * text — a 404/405 from an engine build older than the route, a 403 refusal —
- * so a non-JSON body folds into an `error` message instead of throwing a
- * SyntaxError at the caller's parse. The 404/405 case is the hosted page
- * driving an out-of-date engine: the page updates on deploy, the engine only
- * when the user updates the Donkey app. */
+/** Whether a reply came from the engine on this Mac. The hosted API and the
+ * in-page browser router answer the same `/api/cut/*` paths through their own
+ * drivers, so the origin — or, when the engine serves the page itself, the
+ * prefix the cloud driver rewrites away — is what separates them. */
+function fromEngine(res: Response): boolean {
+ if (!res.url) return false; // a Response the browser router built in-page
+ const origin = engineOrigin();
+ if (origin) return res.url.startsWith(`${origin}/`);
+ if (!servedFromEngine()) return false;
+ try {
+ return new URL(res.url).pathname.startsWith("/api/cut/");
+ } catch {
+ return false;
+ }
+}
+
+/** JSON body of a backend reply. A reply that never reached a handler is plain
+ * text — a 404/405 from a build older than the route, a 403 refusal — so a
+ * non-JSON body folds into an `error` message instead of throwing a
+ * SyntaxError at the caller's parse. Only the engine can be behind the page it
+ * is driving: it updates when the user updates the Donkey app, while the
+ * hosted API ships with the page, so only an engine reply asks for an update. */
export async function apiJson<T>(res: Response): Promise<T & { error?: string }> {
const text = await res.text();
try {
return JSON.parse(text) as T & { error?: string };
} catch {
+ const engine = fromEngine(res);
const stale = res.status === 404 || res.status === 405;
return {
- error: stale
- ? "The Donkey app on this Mac doesn't support this yet — update Donkey and try again."
- : text.trim() || `The engine replied ${res.status}.`,
+ error:
+ stale && engine
+ ? "The Donkey app on this Mac doesn't support this yet — update Donkey and try again."
+ : stale
+ ? "Donkey doesn't support this yet."
+ : text.trim() || `The ${engine ? "engine" : "server"} replied ${res.status}.`,
} as T & { error?: string };
}
}
diff --git a/site/src/cut/lib/backend/browser.ts b/site/src/cut/lib/backend/browser.ts
index 63795883..65693acb 100644
--- a/site/src/cut/lib/backend/browser.ts
+++ b/site/src/cut/lib/backend/browser.ts
@@ -18,7 +18,7 @@ async function browserFetch(path: string, init?: RequestInit): Promise<Response>
export const browserBackend: CutBackend = {
kind: "browser",
caps: {
- importUrl: false, // needs a machine that can run the fetch
+ importUrl: true, // the cloud worker fetches; the page adopts the bytes
liveMic: true, // hosted LLM STT (lib/cloudTranscribe.ts)
transcribe: true, // hosted LLM STT (lib/cloudTranscribe.ts)
captionAi: true, // hosted Gemini twin (server/cloud/captions.ts)
diff --git a/site/src/cut/lib/backend/browser/router.ts b/site/src/cut/lib/backend/browser/router.ts
index 0184df68..943178e1 100644
--- a/site/src/cut/lib/backend/browser/router.ts
+++ b/site/src/cut/lib/backend/browser/router.ts
@@ -314,10 +314,10 @@ async function exportJobStatus(jobId: string): Promise<Response> {
async function exportJobFile(jobId: string, download: boolean): Promise<Response> {
const job = getBrowserExportJob(jobId);
if (!job || job.status !== "done" || !job.outName) {
- return new Response("Export not ready.", { status: 404 });
+ return err("Export not ready.", 404);
}
const file = await store.readFileAt(await store.exportsDir(job.projectId), job.outName);
- if (!file) return new Response("Export not ready.", { status: 404 });
+ if (!file) return err("Export not ready.", 404);
return serveFile(file, job.outName, download);
}
diff --git a/site/src/cut/lib/cloudJob.ts b/site/src/cut/lib/cloudJob.ts
new file mode 100644
index 00000000..f2a78692
--- /dev/null
+++ b/site/src/cut/lib/cloudJob.ts
@@ -0,0 +1,41 @@
+"use client";
+
+import { apiJson, type CutBackend } from "./backend";
+
+/**
+ * Wait on a cloud render job (URL import, conversion) and hand back its
+ * result. The cloud answers those routes with a job id and a worker does the
+ * 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`.
+ */
+export async function pollCloudJob<T>(
+ jobId: string,
+ backend: CutBackend,
+ fallback: string,
+ timedOut = fallback
+): Promise<T> {
+ const deadline = Date.now() + 10 * 60 * 1000;
+ const MAX_STRIKES = 6;
+ let strikes = 0;
+ for (;;) {
+ if (Date.now() > deadline) throw new Error(timedOut);
+ await new Promise((r) => setTimeout(r, 2000));
+ let res: Response | null = null;
+ try {
+ res = await backend.fetch(`/api/cut/jobs/${jobId}`);
+ } catch {
+ // Network blip — a strike, counted below.
+ }
+ // The create call returned this job's id, so a 404 means it's gone.
+ if (res?.status === 404) throw new Error(fallback);
+ if (!res?.ok) {
+ if (++strikes >= MAX_STRIKES) throw new Error(fallback);
+ continue;
+ }
+ strikes = 0;
+ const job = await apiJson<{ state?: string; result?: T }>(res);
+ 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.ts
index 73bb1631..c3f722f2 100644
--- a/site/src/cut/lib/library.ts
+++ b/site/src/cut/lib/library.ts
@@ -2,6 +2,7 @@
import { apiJson, getBackend } from "./backend";
import { readSnapshot, writeSnapshot } from "./cache";
+import { pollCloudJob } from "./cloudJob";
import { downloadFromUrl } from "./download";
import {
enrichAsset,
@@ -129,11 +130,27 @@ export async function importUrlToLibrary(
url: string,
residency: Residency = activeResidency()
): Promise<LibraryAsset[]> {
- const res = await backendFor(residency).fetch("/api/cut/library/import-url", {
+ const backend = backendFor(residency);
+ const res = await backend.fetch("/api/cut/library/import-url", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url }),
});
+ // The engine downloads inside the request; the cloud answers {jobId} and a
+ // worker does the fetch, so that side waits on the job.
+ if (backend.kind === "cloud") {
+ const started = await apiJson<{ jobId?: string }>(res);
+ if (!res.ok || !started.jobId) throw new Error(started.error ?? "Could not import that URL.");
+ const done = await pollCloudJob<{ assets?: LibraryAsset[] }>(
+ started.jobId,
+ backend,
+ "Could not import that URL."
+ );
+ // 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.");
+ 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 }));
diff --git a/site/src/cut/lib/media.ts b/site/src/cut/lib/media.ts
index 5566fbae..717e1465 100644
--- a/site/src/cut/lib/media.ts
+++ b/site/src/cut/lib/media.ts
@@ -2,6 +2,8 @@
import { scanSilence, type PcmChunk } from "./audioScan";
import { apiFetch, apiJson, getBackend, type CutBackend } from "./backend";
+import { cloudBackend } from "./backend/cloud";
+import { pollCloudJob } from "./cloudJob";
import { quotaErrorMessage } from "./backend/cloud";
import { encodeWav } from "./cloudTranscribe";
import { downloadFromUrl } from "./download";
@@ -830,6 +832,12 @@ export async function importUrlMedia(
// 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();
+ // A browser project has no server of its own to run the fetch, so its import
+ // rides the cloud worker and the bytes come back down.
+ if (backend.kind === "browser") {
+ const staged = await importUrlThroughCloud(projectId, backend, url);
+ return adoptImportedFiles(projectId, backend, staged);
+ }
const res = await backend.fetch(`/api/cut/projects/${projectId}/import-url`, {
method: "POST",
headers: { "Content-Type": "application/json" },
@@ -840,7 +848,7 @@ export async function importUrlMedia(
// The cloud route is async: it answers {jobId} and a worker does the fetch.
const started = await apiJson<{ jobId?: string }>(res);
if (!res.ok || !started.jobId) throw new Error(started.error ?? "Could not import that URL.");
- body = await pollImportUrlJob(started.jobId, backend);
+ body = await pollCloudJob(started.jobId, backend, "Could not import that URL.");
} else {
body = await apiJson<{ files?: { fileName: string; title: string }[]; text?: string }>(res);
}
@@ -849,8 +857,18 @@ export async function importUrlMedia(
if (!res.ok || (!body.files?.length && !body.text)) {
throw new Error(body.error ?? "Could not import that URL.");
}
+ return adoptImportedFiles(projectId, backend, body);
+}
+
+/** Register the files an import landed in the project, in order, and hand back
+ * what the caller places. */
+async function adoptImportedFiles(
+ projectId: string,
+ backend: CutBackend,
+ imported: { files?: { fileName: string; title: string }[]; text?: string }
+): Promise<{ assets: MediaAsset[]; text?: string }> {
const assets: MediaAsset[] = [];
- for (const f of body.files ?? []) {
+ for (const f of imported.files ?? []) {
const asset = await assetFromProjectFile(
projectId,
f.fileName,
@@ -861,43 +879,63 @@ export async function importUrlMedia(
void enrichAsset(asset);
assets.push(asset);
}
- return { assets, text: body.text };
+ return { assets, text: imported.text };
}
-/** Poll a cloud import-url job to completion (2s cadence, ~10 min cap) and
- * return the engine-shaped {files, text} result. Fails only when the job
- * itself says so — state "error", or the job gone (404) — or after several
- * consecutive failed polls; a single dropped request keeps polling. */
-async function pollImportUrlJob(
- jobId: string,
- backend: CutBackend
-): Promise<{ files?: { fileName: string; title: string }[]; text?: string }> {
- const deadline = Date.now() + 10 * 60 * 1000;
- const MAX_STRIKES = 6;
- let strikes = 0;
- for (;;) {
- if (Date.now() > deadline) throw new Error("Could not import that URL.");
- await new Promise((r) => setTimeout(r, 2000));
- let res: Response | null = null;
- try {
- res = await backend.fetch(`/api/cut/jobs/${jobId}`);
- } catch {
- // Network blip — a strike, counted below.
+/**
+ * A browser project's URL import. Nothing in the page can fetch a TikTok or a
+ * YouTube video, so the cloud worker does it — into the account's library,
+ * where the page can read it — and the bytes come down into this browser's own
+ * storage. The staged library copy is dropped once the project holds it, so
+ * the import leaves nothing behind on the cloud shelf.
+ */
+async function importUrlThroughCloud(
+ projectId: string,
+ backend: CutBackend,
+ url: string
+): Promise<{ files: { fileName: string; title: string }[]; text?: string }> {
+ const res = await cloudBackend.fetch("/api/cut/library/import-url", {
+ method: "POST",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ url }),
+ });
+ const started = await apiJson<{ jobId?: string }>(res);
+ if (!res.ok || !started.jobId) throw new Error(started.error ?? "Could not import that URL.");
+ const done = await pollCloudJob<{
+ assets?: { id: string; fileName: string; name: string }[];
+ text?: string;
+ }>(started.jobId, cloudBackend, "Could not import that URL.");
+ // Words with no media are a successful import; nothing at all is a failure.
+ if (!done.assets?.length && !done.text) throw new Error("Could not import that URL.");
+ const files: { fileName: string; title: string }[] = [];
+ // Every staged copy is dropped from the cloud shelf, however this ends: a
+ // download or a local write that throws part way through would otherwise
+ // leave the user holding — and paying quota for — library media they never
+ // asked for and cannot see from a browser project.
+ const dropStaged = async () => {
+ for (const staged of done.assets ?? []) {
+ await cloudBackend
+ .fetch(`/api/cut/library/${staged.id}`, { method: "DELETE" })
+ .catch(() => {});
}
- // The create call returned this job's id, so a 404 means it's gone.
- if (res?.status === 404) throw new Error("Could not import that URL.");
- if (!res?.ok) {
- if (++strikes >= MAX_STRIKES) throw new Error("Could not import that URL.");
- continue;
+ };
+ try {
+ for (const staged of done.assets ?? []) {
+ const path = `/api/cut/library/media/${encodeURIComponent(staged.fileName)}`;
+ const bytes = await cloudBackend.fetch(path);
+ if (!bytes.ok) throw new Error("Could not import that URL.");
+ const fileName = await uploadProjectMediaTo(
+ backend,
+ projectId,
+ await bytes.blob(),
+ staged.fileName
+ );
+ files.push({ fileName, title: staged.name });
}
- strikes = 0;
- const job = await apiJson<{
- state?: string;
- result?: { files?: { fileName: string; title: string }[]; text?: string };
- }>(res);
- if (job.state === "error") throw new Error(job.error ?? "Could not import that URL.");
- if (job.state === "done") return job.result ?? {};
+ } finally {
+ void dropStaged();
}
+ return { files, ...(done.text ? { text: done.text } : {}) };
}
/** Build a runtime asset for a media file the engine already wrote into the
diff --git a/site/src/cut/lib/mediaConvert.ts b/site/src/cut/lib/mediaConvert.ts
index 6d7fb7bf..d3f986d1 100644
--- a/site/src/cut/lib/mediaConvert.ts
+++ b/site/src/cut/lib/mediaConvert.ts
@@ -43,6 +43,7 @@ import {
import { apiFetch as engineFetch } from "./api";
import { getBackend, hasLocalCompute, type CutBackend } from "./backend";
import { resolveRegisteredBlob } from "./backend/browser/registry";
+import { pollCloudJob } from "./cloudJob";
import { enrichAsset, uploadProjectMediaTo } from "./media";
import {
audioTrackOf,
@@ -241,7 +242,12 @@ async function convertOnBackend(
if (backend.kind === "cloud") {
const started = (await res.json().catch(() => ({}))) as { jobId?: string; error?: string };
if (!res.ok || !started.jobId) throw new Error(started.error ?? "Could not convert that file.");
- body = await pollConvertJob(started.jobId, backend);
+ body = await pollCloudJob(
+ started.jobId,
+ backend,
+ "Could not convert that file.",
+ "The conversion took too long."
+ );
} else {
body = (await res.json().catch(() => ({}))) as ServerConvert;
if (!res.ok) throw new Error(body.error ?? "Could not convert that file.");
@@ -250,37 +256,6 @@ async function convertOnBackend(
return fromServer(body);
}
-/** Poll a cloud convert job to completion — the import-url cadence: 2s, a
- * ten-minute ceiling, and a run of failed polls before giving up on it. */
-async function pollConvertJob(jobId: string, backend: CutBackend): Promise<ServerConvert> {
- const deadline = Date.now() + 10 * 60 * 1000;
- const MAX_STRIKES = 6;
- let strikes = 0;
- for (;;) {
- if (Date.now() > deadline) throw new Error("The conversion took too long.");
- await new Promise((r) => setTimeout(r, 2000));
- let res: Response | null = null;
- try {
- res = await backend.fetch(`/api/cut/jobs/${jobId}`);
- } catch {
- // Network blip — a strike, counted below.
- }
- if (res?.status === 404) throw new Error("Could not convert that file.");
- if (!res?.ok) {
- if (++strikes >= MAX_STRIKES) throw new Error("Could not convert that file.");
- continue;
- }
- strikes = 0;
- const job = (await res.json().catch(() => ({}))) as {
- state?: string;
- result?: ServerConvert;
- error?: string;
- };
- if (job.state === "error") throw new Error(job.error ?? "Could not convert that file.");
- if (job.state === "done") return job.result ?? {};
- }
-}
-
/** No machine stores this project, so the work happens beside the bytes: in
* this process, or on the Mac when this process has no decoder for them. */
async function convertNearThePage(
diff --git a/site/src/cut/server/cloud/jobs.ts b/site/src/cut/server/cloud/jobs.ts
index 9303cfda..edc4d5a8 100644
--- a/site/src/cut/server/cloud/jobs.ts
+++ b/site/src/cut/server/cloud/jobs.ts
@@ -453,6 +453,29 @@ export const jobsCloud = {
}
},
+ /** Queue a URL import into the shared library. No project owns it: the
+ * worker lands the files under the account's library prefix and registers
+ * the assets, and the client polls this job for them. */
+ async importUrlToLibrary(userId: string, req: Request) {
+ try {
+ const { url } = (await req.json()) as { url?: string };
+ if (!url) return err("No URL provided.", 400);
+ const capped = await renderJobCheck(userId);
+ if (capped) return capped;
+ const row = await prisma.cutRenderJob.create({
+ data: {
+ userId,
+ kind: "import_url",
+ spec: { url, target: "library" } as unknown as Prisma.InputJsonValue,
+ },
+ });
+ wakeRenderWorker();
+ return Response.json({ jobId: row.id });
+ } catch (e) {
+ return caught(e, "Could not import that URL.");
+ }
+ },
+
/** Queue a media conversion: the worker rewrites one of the project's media
* files as MP4 and registers it beside the original. */
async convert(userId: string, projectId: string, req: Request) {
diff --git a/site/src/cut/server/cloud/projects.ts b/site/src/cut/server/cloud/projects.ts
index 9c4e5fc4..47c0c7bb 100644
--- a/site/src/cut/server/cloud/projects.ts
+++ b/site/src/cut/server/cloud/projects.ts
@@ -199,6 +199,7 @@ export const projectsCloud = {
aspect: normalizeAspect(body.aspect) ?? existing.aspect,
fadeIn: typeof body.fadeIn === "number" ? body.fadeIn : existing.fadeIn,
fadeOut: typeof body.fadeOut === "number" ? body.fadeOut : existing.fadeOut,
+ background: typeof body.background === "string" ? body.background : existing.background,
subtitles:
body.subtitles && typeof body.subtitles === "object" ? body.subtitles : existing.subtitles,
ui: body.ui && typeof body.ui === "object" ? { ...existing.ui, ...body.ui } : existing.ui,
diff --git a/site/src/cut/server/cloud/routes.ts b/site/src/cut/server/cloud/routes.ts
index 355e8033..7d5cb3e6 100644
--- a/site/src/cut/server/cloud/routes.ts
+++ b/site/src/cut/server/cloud/routes.ts
@@ -73,6 +73,7 @@ const CUT_CLOUD_ROUTES: CloudRoute[] = [
{ method: "POST", path: "/api/cut-cloud/library/templates/:id/add", handler: (r, u, p) => libraryCloud.addToTemplate(u, p.id, r) },
{ method: "PUT", path: "/api/cut-cloud/library/templates/:id", handler: (r, u, p) => libraryCloud.renameTemplate(u, p.id, r) },
{ method: "DELETE", path: "/api/cut-cloud/library/templates/:id", handler: (_r, u, p) => libraryCloud.removeTemplate(u, p.id) },
+ { method: "POST", path: "/api/cut-cloud/library/import-url", handler: (r, u) => jobsCloud.importUrlToLibrary(u, r) },
{ method: "POST", path: "/api/cut-cloud/library/folders", handler: (r, u) => libraryCloud.createFolder(u, r) },
{ method: "PUT", path: "/api/cut-cloud/library/folders/:id", handler: (r, u, p) => libraryCloud.renameFolder(u, p.id, r) },
{ method: "DELETE", path: "/api/cut-cloud/library/folders/:id", handler: (_r, u, p) => libraryCloud.deleteFolder(u, p.id) },
diff --git a/site/src/cut/server/http/projects.ts b/site/src/cut/server/http/projects.ts
index 3ba7ab9a..0e7d05ab 100644
--- a/site/src/cut/server/http/projects.ts
+++ b/site/src/cut/server/http/projects.ts
@@ -127,6 +127,7 @@ export const projectsApi = {
aspect: normalizeAspect(body.aspect) ?? existing.aspect,
fadeIn: typeof body.fadeIn === "number" ? body.fadeIn : existing.fadeIn,
fadeOut: typeof body.fadeOut === "number" ? body.fadeOut : existing.fadeOut,
+ background: typeof body.background === "string" ? body.background : existing.background,
subtitles:
body.subtitles && typeof body.subtitles === "object"
? body.subtitles
diff --git a/site/src/cut/worker/convertJob.ts b/site/src/cut/worker/convertJob.ts
index e5e5aecd..6a3e11b9 100644
--- a/site/src/cut/worker/convertJob.ts
+++ b/site/src/cut/worker/convertJob.ts
@@ -4,8 +4,8 @@ import path from "node:path";
import type { RenderHandle } from "../server/exportPipeline";
import { convertToMp4, mp4NameFor } from "../server/convert";
import { probeDuration } from "../server/frames";
+import { dedupeName } from "../server/cloud/util";
import { prisma, registerObject, unregisterObjects, type ClaimedJob } from "./db";
-import { dedupeName } from "./importUrlJob";
import { deleteObjects, downloadToFile, mediaKey, uploadFile } from "./r2";
/** What a convert job records in CutRenderJob.result — the same shape the
diff --git a/site/src/cut/worker/db.ts b/site/src/cut/worker/db.ts
index 07f1067b..48856fb2 100644
--- a/site/src/cut/worker/db.ts
+++ b/site/src/cut/worker/db.ts
@@ -13,26 +13,28 @@ export interface ClaimedJob {
}
/**
- * Record a finished R2 object and keep the user's storage total in step. The
- * upsert makes re-registering the same key (a preview re-render, a retried
- * job) charge only the size delta instead of double-counting.
+ * Record a finished R2 object and keep the user's storage total in step, and
+ * hand back the row's id. The upsert makes re-registering the same key (a
+ * preview re-render, a retried job) charge only the size delta instead of
+ * double-counting.
*/
export async function registerObject(opts: {
userId: string;
- projectId: string;
+ /** Null for objects no project owns — the account's shared library. */
+ projectId: string | null;
r2Key: string;
fileName: string;
mime: string;
bytes: number;
kind: string;
-}): Promise<void> {
+}): Promise<string> {
const prior = await prisma.cutMediaObject.findUnique({
where: { r2Key: opts.r2Key },
select: { bytes: true, uploadState: true },
});
const priorBytes = prior?.uploadState === "complete" ? prior.bytes : BigInt(0);
const delta = BigInt(opts.bytes) - priorBytes;
- await prisma.$transaction([
+ const [row] = await prisma.$transaction([
prisma.cutMediaObject.upsert({
where: { r2Key: opts.r2Key },
create: {
@@ -53,6 +55,7 @@ export async function registerObject(opts: {
update: { bytes: { increment: delta } },
}),
]);
+ return row.id;
}
/**
diff --git a/site/src/cut/worker/importUrlJob.ts b/site/src/cut/worker/importUrlJob.ts
index 577b6d22..d6ecd407 100644
--- a/site/src/cut/worker/importUrlJob.ts
+++ b/site/src/cut/worker/importUrlJob.ts
@@ -1,40 +1,45 @@
import { mkdtemp, rm } from "node:fs/promises";
import os from "node:os";
import path from "node:path";
+import type { Prisma } from "@/generated/prisma/client";
+import { dedupeName, safeFileName, typeOf } from "../server/cloud/util";
+import type { LibraryAsset } from "../server/library";
+import { probeDuration } from "../server/frames";
import { download } from "../server/urlDownload";
+import { videoDimensions } from "../server/util";
import { prisma, registerObject, unregisterObjects, type ClaimedJob } from "./db";
-import { deleteObjects, mediaKey, mimeFor, uploadFile } from "./r2";
+import { deleteObjects, libraryKey, mediaKey, mimeFor, uploadFile } from "./r2";
-/** What an import_url job records in CutRenderJob.result — the same shape the
- * engine's synchronous import route returns to the client. */
+/** What a project import_url job records in CutRenderJob.result — the same
+ * shape the engine's synchronous import route returns to the client. */
export interface ImportUrlResult {
files: { fileName: string; title: string }[];
text?: string;
}
-/** First variant of `base` (stem, stem-1, stem-2, …) not already `taken` —
- * the engine's on-disk dedupe scheme, run against the project's media rows. */
-export function dedupeName(base: string, taken: Set<string>): string {
- const ext = path.extname(base);
- const stem = base.slice(0, base.length - ext.length) || "media";
- let name = base;
- for (let n = 1; taken.has(name); n++) name = `${stem}-${n}${ext}`;
- return name;
+/** What a library import_url job records: the assets, exactly as the engine's
+ * library import answers with (minus the residency the client stamps on), plus
+ * the source's own words — a browser project imports through this job, and its
+ * chat quotes that text the way a project import does. */
+export interface LibraryImportResult {
+ assets: LibraryAsset[];
+ text?: string;
}
/**
* Run one import_url job: fetch the URL into a temp dir with the shared
- * yt-dlp/tweet-photo logic, then land each file in the project's R2 media
- * prefix with a name deduped against its existing media objects.
+ * yt-dlp/tweet-photo logic, then land what came back — in the project's R2
+ * media prefix, or in the account's library when the job carries no project.
*/
export async function runImportUrlJob(
job: ClaimedJob,
isCanceled: () => boolean
-): Promise<ImportUrlResult> {
- const { url } = (job.spec ?? {}) as { url?: string };
+): Promise<ImportUrlResult | LibraryImportResult> {
+ const { url, target } = (job.spec ?? {}) as { url?: string; target?: string };
if (!url || !/^https?:\/\//i.test(url.trim())) throw new Error("Enter a valid http(s) URL.");
+ const toLibrary = target === "library";
const projectId = job.projectId;
- if (!projectId) throw new Error("Import job has no project.");
+ if (!toLibrary && !projectId) throw new Error("Import job has no project.");
const tmp = await mkdtemp(path.join(os.tmpdir(), "cut-dl-"));
try {
@@ -42,36 +47,61 @@ export async function runImportUrlJob(
if (isCanceled()) throw new Error("Import canceled.");
const rows = await prisma.cutMediaObject.findMany({
- where: { projectId, kind: "media" },
+ where: toLibrary ? { userId: job.userId, kind: "library" } : { projectId, kind: "media" },
select: { fileName: true },
});
const taken = new Set(rows.map((r) => r.fileName));
const files: ImportUrlResult["files"] = [];
+ const assets: LibraryAsset[] = [];
// A failed or canceled run keeps no bytes: whatever it staged in R2 is
- // unregistered and deleted before the error propagates, so the project's
- // storage only ever holds media the client can still adopt.
+ // unregistered and deleted before the error propagates, so the account's
+ // storage only ever holds media the client can still adopt. Library rows
+ // written for the files that already landed go with them — a card whose
+ // media was just deleted is a card nothing can open.
const staged: string[] = [];
+ const libraryRows: string[] = [];
try {
for (const f of dl.files) {
if (isCanceled()) throw new Error("Import canceled.");
- const fileName = dedupeName(path.basename(f.file), taken);
+ const base = toLibrary ? safeFileName(f.file) : path.basename(f.file);
+ const fileName = dedupeName(base, taken);
taken.add(fileName);
- const key = mediaKey(job.userId, projectId, fileName);
+ const key = toLibrary
+ ? libraryKey(job.userId, fileName)
+ : mediaKey(job.userId, projectId!, fileName);
staged.push(key);
const bytes = await uploadFile(key, f.file, mimeFor(fileName));
- await registerObject({
+ const objectId = await registerObject({
userId: job.userId,
- projectId,
+ projectId: toLibrary ? null : projectId,
r2Key: key,
fileName,
mime: mimeFor(fileName),
bytes,
- kind: "media",
+ kind: toLibrary ? "library" : "media",
});
- files.push({ fileName, title: f.title });
+ if (!toLibrary) {
+ files.push({ fileName, title: f.title });
+ continue;
+ }
+ const asset = await addLibraryRow(
+ job.userId,
+ objectId,
+ fileName,
+ f.file,
+ f.title,
+ dl.source
+ );
+ libraryRows.push(asset.id);
+ assets.push(asset);
}
+ if (toLibrary) return { assets, ...(dl.text ? { text: dl.text } : {}) };
return { files, ...(dl.text ? { text: dl.text } : {}) };
} catch (err) {
+ if (libraryRows.length > 0)
+ await prisma.cutLibraryAsset
+ .deleteMany({ where: { userId: job.userId, id: { in: libraryRows } } })
+ .catch(() => {});
await unregisterObjects(job.userId, staged).catch(() => {});
await deleteObjects(staged);
throw err;
@@ -80,3 +110,44 @@ export async function runImportUrlJob(
void rm(tmp, { recursive: true, force: true });
}
}
+
+/** Register one downloaded file as a library asset, probing the local copy for
+ * the shape the library card reads (duration, pixel size). */
+async function addLibraryRow(
+ userId: string,
+ mediaObjectId: string,
+ fileName: string,
+ localFile: string,
+ title: string,
+ source: LibraryAsset["source"]
+): Promise<LibraryAsset> {
+ const type = typeOf(fileName);
+ if (!type) throw new Error("Unsupported file type.");
+ const duration = type === "image" ? 0 : await probeDuration(localFile);
+ const dims = type === "audio" ? null : await videoDimensions(localFile);
+ const meta = {
+ name: title || fileName,
+ type,
+ duration,
+ ...(dims ? { width: dims.width, height: dims.height } : {}),
+ ...(source ? { source } : {}),
+ };
+ const row = await prisma.cutLibraryAsset.create({
+ data: {
+ userId,
+ mediaObjectId,
+ meta: meta as unknown as Prisma.InputJsonValue,
+ },
+ });
+ return {
+ id: row.id,
+ fileName,
+ name: meta.name,
+ type,
+ duration,
+ ...(dims ? { width: dims.width, height: dims.height } : {}),
+ addedAt: row.createdAt.getTime(),
+ folderId: null,
+ ...(source ? { source } : {}),
+ };
+}
diff --git a/site/src/cut/worker/r2.ts b/site/src/cut/worker/r2.ts
index 0d72d09e..d48225d3 100644
--- a/site/src/cut/worker/r2.ts
+++ b/site/src/cut/worker/r2.ts
@@ -19,6 +19,7 @@ export {
projectExportKey as exportKey,
projectHlsPrefix as hlsPrefix,
projectHlsRoot as hlsRoot,
+ libraryKey,
projectMediaKey as mediaKey,
projectPreviewKey as previewKey,
} from "../server/cloud/r2";