Open source CapCut alternative.
TypeScript
import {
cancelJob,
completeClientJob,
completeClientStems,
createClientJob,
createJob,
getJob,
listAllJobs,
progressClientJob,
releaseClientJob,
type ExportSpec,
} from "../jobs";
import { serveFileRange } from "../serveFile";
import { UserError, errorBody } from "../../lib/userError";
import { errorReply } from "../util";
const startFailed = () => new UserError("export_start_failed", "Export failed to start.");
const notRunning = () => new UserError("export_not_running", "This export is no longer running.");
/** Export rendering: start a job, poll it, cancel it, download the result. */
export const exportApi = {
/** Every export job across every project, in start order — the feed the
* app-wide exports dock polls so it shows the same queue in every tab. */
activeAll() {
return Response.json(listAllJobs());
},
async create(req: Request) {
try {
const form = await req.formData();
const job = await createJob(form);
if (job.status === "error") {
return Response.json(errorBody(job, startFailed()), { status: 400 });
}
return Response.json({ id: job.id });
} catch (e) {
return Response.json(
errorReply(e, startFailed()),
{ status: 500 }
);
}
},
/** A render the browser tab carries itself: open the job row and claim the
* file's name; the tab draws the frames and hands the file in below. */
async createClient(req: Request) {
try {
const body = (await req.json()) as {
projectId?: string;
container?: ExportSpec["container"];
name?: string;
stems?: boolean;
};
if (!body.projectId) return Response.json({ error: "Missing project." }, { status: 400 });
const job = await createClientJob(body.projectId, body.container, body.name?.trim() || undefined, body.stems === true ? "fileAndStems" : "file");
if (job.status === "error") {
return Response.json(errorBody(job, startFailed()), { status: 400 });
}
return Response.json({ id: job.id, outName: job.outName });
} catch (e) {
return Response.json(
errorReply(e, startFailed()),
{ status: 500 }
);
}
},
async clientProgress(req: Request, { jobId }: { jobId: string }) {
const body = (await req.json().catch(() => ({}))) as { ratio?: number };
progressClientJob(jobId, Number(body.ratio));
return Response.json({ ok: true });
},
/** The finished file, streamed straight into the project's exports folder. */
async clientComplete(req: Request, { jobId }: { jobId: string }) {
const job = await completeClientJob(jobId, req.body);
if (!job) {
return Response.json(errorBody(null, notRunning()), { status: 409 });
}
if (job.status === "error") {
return Response.json(errorBody(job, job.error ?? ""), { status: 500 });
}
return Response.json({ id: job.id, outName: job.outName });
},
/** The tab's stems zip, streamed beside the file before the file lands. */
async clientStems(req: Request, { jobId }: { jobId: string }) {
try {
const job = await completeClientStems(jobId, req.body);
if (!job) {
return Response.json(errorBody(null, notRunning()), { status: 409 });
}
return Response.json({ ok: true });
} catch (e) {
return Response.json(
errorReply(e, new UserError("export_stems_save_failed", "Could not save the stems.")),
{ status: 500 }
);
}
},
async clientRelease(_req: Request, { jobId }: { jobId: string }) {
releaseClientJob(jobId);
return Response.json({ ok: true });
},
async status(_req: Request, { jobId }: { jobId: string }) {
const job = getJob(jobId);
if (!job) {
return Response.json(errorBody(null, new UserError("export_unknown", "Unknown export.")), { status: 404 });
}
return Response.json({
status: job.status,
progress: job.progress,
error: job.error,
errorCode: job.errorCode,
errorParams: job.errorParams,
outName: job.outName,
});
},
async cancel(_req: Request, { jobId }: { jobId: string }) {
cancelJob(jobId);
return Response.json({ ok: true });
},
async file(req: Request, { jobId }: { jobId: string }) {
const job = getJob(jobId);
if (!job || job.status !== "done") {
return new Response("Export not ready.", { status: 404 });
}
return serveFileRange(job.outPath, req, { downloadName: job.outName });
},
};