Skip to content

Open source CapCut alternative.

TypeScript
12.4 KB
Raw Blame
"use client";

// Imports that are on screen before they are stored. A dropped file is probed
// from its own bytes and placed straight away; a stock tile or library clip is
// registered against its source URL. The copy into project storage runs here,
// behind the editor, and the asset swaps from the URL it arrived on to the
// stored one when the bytes land.
//
// A pending upload lives in one of two worlds, and its lifecycle follows:
// - Tab-scoped (`upload.stored` unset): the bytes exist only in this tab, so
//   nothing about the asset reaches the saved document (see storedAssets /
//   docClips) and leaving the project cancels the upload — a copy that lands
//   after the cancel is deleted so it never counts against the account.
// - Store-backed (`upload.stored`): the browser store holds the bytes and the
//   document already references the asset, so the upload is background sync.
//   Deleting the asset cancels it (removeAsset cleans both sides), but leaving
//   the project only pauses it — the ledger pin resumes it on the next open,
//   and bytes that land late stay landed, because the document points at them.
// - Either way a failed upload leaves the asset in place with an error, so the
//   user can retry it rather than discover later that it was never saved.
import { getBackend, type CutBackend } from "./backend";
import { resolveRegisteredBlob } from "./backend/browser/registry";
import type { PendingImport } from "./media";
import { storedMediaUrl } from "./mediaSync";
import { CUT_UPLOADS } from "./chatRuntime";
import { connectionLost, isOffline, onReconnect } from "./network";
import { useEditor } from "./store";
import { errorFields, UserError, type ErrorFields } from "./userError";

/** Blob URLs owned by the browser store's registry stay alive for the whole
 * session; only tab-scoped object URLs are this queue's to revoke. */
function revokeTabUrl(url: string) {
  if (!resolveRegisteredBlob(url)) URL.revokeObjectURL(url);
}

// Browsers cap connections per host anyway; a small window keeps one big file
// from starving the rest of a multi-file drop.
const MAX_IN_FLIGHT = 3;

let active = 0;
const waiting: (() => void)[] = [];

function acquire(): Promise<void> {
  if (active < MAX_IN_FLIGHT) {
    active++;
    return Promise.resolve();
  }
  return new Promise((resolve) => waiting.push(resolve));
}

function release() {
  const next = waiting.shift();
  if (next) next();
  else active--;
}

type Job = {
  pending: PendingImport;
  projectId: string;
  /** Pinned at start: the copy can outlast a move to a project of the other
   * residency, and its cleanup has to reach the backend it landed on. */
  backend: CutBackend;
  controller: AbortController;
};

const jobs = new Map<string, Job>();

// An upload the connection dropped, the way a phone hotspot does, picks back
// up by itself: after each pause CUT_UPLOADS lists, and the moment
// the browser reports the connection back, which also restarts the pauses. A
// refusal from the server waits for the user's retry.
const resumes = new Map<string, { attempt: number; timer: ReturnType<typeof setTimeout> | null }>();
let resumeArmed = false;

/** The connection is back: every failed upload retries with fresh pauses. */
function resumeAll() {
  for (const r of resumes.values()) {
    if (r.timer) {
      clearTimeout(r.timer);
    }
  }
  resumes.clear();
  retryFailedUploads();
}

/** Forget an asset's pending resume, when it lands or leaves. */
function dropResume(assetId: string) {
  const r = resumes.get(assetId);
  if (r?.timer) {
    clearTimeout(r.timer);
  }
  resumes.delete(assetId);
}

/** Retry every upload that failed, e.g. once the connection is back. */
export function retryFailedUploads() {
  for (const a of useEditor.getState().assets) {
    if (a.upload?.error) {
      retryUpload(a.id);
    }
  }
}

/** Book the next timed resume of a dropped upload, while any are left. One
 * waits per asset: a later failure replaces the booked one. */
function scheduleResume(job: Job) {
  const id = job.pending.asset.id;
  const delays = CUT_UPLOADS.resumeDelaysMs;
  const prior = resumes.get(id);
  if (prior?.timer) {
    clearTimeout(prior.timer);
  }
  const attempt = prior?.attempt ?? 0;
  if (attempt >= delays.length) {
    resumes.set(id, { attempt, timer: null });
    return;
  }
  const timer = setTimeout(() => {
    const r = resumes.get(id);
    if (r) {
      r.timer = null;
    }
    // Offline, the reconnect does it; a retry already under way holds it.
    const failed = useEditor.getState().assets.find((a) => a.id === id)?.upload?.error;
    if (isOffline() || jobs.get(id) !== job || !failed) {
      return;
    }
    retryUpload(id);
  }, delays[attempt]);
  resumes.set(id, { attempt: attempt + 1, timer });
}

/** Start the background upload for a prepared import. The asset must already
 * be in the store. */
export function startUpload(projectId: string, pending: PendingImport) {
  if (!resumeArmed) {
    resumeArmed = true;
    onReconnect(resumeAll);
  }
  const job: Job = {
    pending,
    projectId,
    backend: getBackend(),
    controller: new AbortController(),
  };
  jobs.set(pending.asset.id, job);
  void run(job);
}

/** Whether a job's bytes are held by the browser store — see the header. */
const storeBacked = (job: Job) => job.pending.asset.upload?.stored === true;

/** The browser store took the bytes mid-drain, under the name the cloud
 * claimed: from here the job is background sync (see the header), and the
 * asset carries its stored name into the saved document. */
export function markUploadStored(assetId: string, fileName: string) {
  const upload = { progress: 0, stored: true as const };
  const job = jobs.get(assetId);
  if (job) job.pending.asset = { ...job.pending.asset, fileName, upload };
  useEditor.getState().updateAsset(assetId, { fileName, upload });
}

/** The `upload` patch for a job, keeping `stored` so a progress tick can't
 * demote a store-backed asset to tab-scoped (which would strip it from the
 * saved document), and `server` so the indicator keeps calling a copy a copy.
 * `updateAsset` merges shallowly; `upload` is replaced whole. */
function uploadPatch(job: Job, progress: number, failure?: ErrorFields) {
  return {
    upload: {
      progress,
      ...failure,
      ...(storeBacked(job) ? { stored: true as const } : {}),
      ...(job.pending.asset.upload?.server ? { server: true as const } : {}),
    },
  };
}

/** Retry an upload that failed. */
export function retryUpload(assetId: string) {
  const job = jobs.get(assetId);
  if (!job) return;
  job.controller = new AbortController();
  useEditor.getState().updateAsset(assetId, uploadPatch(job, 0));
  void run(job);
}

/** Drop bytes that landed for an asset that is no longer here. A copy that
 * completes counts against the account's storage, so a cancel that arrives
 * once it has finished has to take the file back out. */
function dropLanded(job: Job, fileName: string) {
  void job.backend
    .fetch(`/api/cut/projects/${job.projectId}/media/${encodeURIComponent(fileName)}`, {
      method: "DELETE",
    })
    .catch(() => {});
}

/** Stop an upload whose asset is gone. A claim the copy never completed is
 * left incomplete on purpose — it was never counted against the account. */
export function cancelUpload(assetId: string) {
  const job = jobs.get(assetId);
  if (!job) return;
  jobs.delete(assetId);
  dropResume(assetId);
  job.controller.abort();
  revokeTabUrl(job.pending.localUrl);
}

/** True while this asset's upload job is alive (in flight or failed-retryable). */
export function uploadInFlight(assetId: string): boolean {
  return jobs.has(assetId);
}

/** An upload is only worth finishing while its asset is still open in front of
 * the user: deleting it, or leaving for another project, ends it. A
 * store-backed upload outlives navigation on the ledger, so this gate only
 * applies to tab-scoped ones. */
function wanted(job: Job): boolean {
  const s = useEditor.getState();
  return s.projectId === job.projectId && s.assets.some((a) => a.id === job.pending.asset.id);
}

/** Set a store-backed upload down without unpinning it: the ledger entry and
 * the stored bytes stay, and the next open of the project resumes the drain. */
function pauseUpload(job: Job) {
  jobs.delete(job.pending.asset.id);
  job.controller.abort();
}

/** A store-backed job that lost its asset: leaving the project pauses it;
 * deleting the asset ends it (removeAsset already cleaned the store side). */
function stopStored(job: Job) {
  if (useEditor.getState().projectId === job.projectId) cancelUpload(job.pending.asset.id);
  else pauseUpload(job);
}

async function run(job: Job) {
  const { asset, localUrl, send, hold } = job.pending;
  let slot = false;
  try {
    if (!wanted(job)) return storeBacked(job) ? stopStored(job) : cancelUpload(asset.id);
    let shown = -1;
    const opts = {
      signal: job.controller.signal,
      onProgress: (fraction: number) => {
        // The progress stream is the cheapest place to notice the asset is
        // gone, and the only one that fires during a long upload.
        if (!wanted(job)) return storeBacked(job) ? stopStored(job) : cancelUpload(asset.id);
        const pct = Math.floor(fraction * 100);
        if (pct === shown) return;
        shown = pct;
        useEditor.getState().updateAsset(asset.id, uploadPatch(job, fraction));
      },
    };
    // The hold runs at once, outside the window: every file of a long drop
    // reaches storage a reload can find while the uploads queue behind it.
    if (hold) {
      await hold(opts);
    }
    await acquire();
    slot = true;
    if (!wanted(job)) return storeBacked(job) ? stopStored(job) : cancelUpload(asset.id);
    const fileName = await send(opts);
    if (!wanted(job)) {
      if (storeBacked(job) && useEditor.getState().projectId !== job.projectId) {
        // The bytes landed exactly where the saved document points; the job
        // is simply finished, in whatever project the user is in now.
        jobs.delete(asset.id);
        return;
      }
      // The bytes beat the delete: they are stored and counted, and nothing
      // in the document points at them any more.
      cancelUpload(asset.id);
      return dropLanded(job, fileName);
    }
    jobs.delete(asset.id);
    dropResume(asset.id);
    // The stored file takes over from the source bytes, under the name the
    // copy resolved to (a dropped file reserved it up front; a remote import
    // learns it here), at the address every stored file plays from. Each move
    // repoints every reader on the asset. Filmstrips are self-contained frames
    // and survive the swap; a still's single "frame" is the source itself, so
    // it repoints with the asset.
    const url = await storedMediaUrl(job.projectId, fileName, job.backend);
    // The stored copy is the same bytes at a new key: what this session
    // already read under the source's key — the play so far, the waveform and
    // filmstrip decodes — carries over before anything opens the new address,
    // so the swap lands on a warm cache.
    const playingFrom = useEditor.getState().assets.find((a) => a.id === asset.id)?.url ?? localUrl;
    await import("./chunkCache")
      .then((m) => m.adoptChunks(playingFrom, url))
      .catch(() => {});
    useEditor.getState().updateAsset(asset.id, {
      fileName,
      url,
      upload: undefined,
      ...(asset.type === "image" ? { thumbs: [url] } : {}),
    });
    // A ProRes master's preview proxy, a heavy cloud video's, or the compat
    // proxy of a stream this browser cannot decode is built once the bytes
    // are at rest in the project's own storage, which is now.
    const landed = useEditor.getState().assets.find((a) => a.id === asset.id);
    if (landed && landed.type !== "image") {
      void import("./mediaProxy")
        .then((m) => m.ensureProxy(job.projectId, landed, job.backend))
        .catch(() => {});
    }
    // Decoders and voices carry across the URL change on what they already
    // hold; the source bytes stay readable while their new stacks open.
    setTimeout(() => revokeTabUrl(localUrl), 10_000);
  } catch (err) {
    if (job.controller.signal.aborted) return;
    if ((err as { paused?: boolean }).paused) return pauseUpload(job);
    if (!wanted(job)) return storeBacked(job) ? stopStored(job) : cancelUpload(asset.id);
    useEditor.getState().updateAsset(
      asset.id,
      uploadPatch(job, 0, errorFields(err, new UserError("upload_failed", "Upload failed.")))
    );
    if (connectionLost(err)) {
      scheduleResume(job);
    }
  } finally {
    if (slot) {
      release();
    }
  }
}