Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"check:no-linked-sdk": "node scripts/check-no-linked-sdk.mjs"
},
"dependencies": {
"@rendobar/sdk": "^3.0.0",
"@clack/prompts": "^1.2.0",
"@rendobar/sdk": "^3.0.1",
"citty": "^0.2.0",
"picocolors": "^1.1.1",
"@clack/prompts": "^1.2.0"
"picocolors": "^1.1.1"
},
"devDependencies": {
"@commitlint/cli": "^21.0.0",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions src/__tests__/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,43 @@ describe("uploadLocalFiles", () => {

await expect(uploadLocalFiles(args, inputs, mockClient)).rejects.toThrow("File not found");
});

it("passes the file's sha256 checksum so the server can dedup", async () => {
const localPath = createTempFile("video.mp4", "fake");
const mockUpload = mock((_data: Uint8Array, _options: { checksum?: string }) =>
Promise.resolve({ url: "https://cdn.rendobar.com/uploads/abc.mp4" }),
);
const mockClient = { uploads: { create: mockUpload } } as unknown as Parameters<typeof uploadLocalFiles>[2];

await uploadLocalFiles(["-i", localPath, "out.mp4"], [{ index: 1, value: localPath, isLocal: true }], mockClient);

// Hash of the same content, computed independently of the code under test.
const expected = new Bun.CryptoHasher("sha256").update("fake").digest("hex");
expect(mockUpload.mock.calls[0]![1].checksum).toBe(expected);
});

it("forwards SDK progress events to onFileProgress with file context", async () => {
const localPath = createTempFile("video.mp4", "fake-bytes");
const mockUpload = mock(
async (_data: Uint8Array, options: { onProgress?: (p: { loaded: number; total: number }) => void }) => {
options.onProgress?.({ loaded: 5, total: 10 });
options.onProgress?.({ loaded: 10, total: 10 });
return { url: "https://cdn.rendobar.com/uploads/abc.mp4" };
},
);
const mockClient = { uploads: { create: mockUpload } } as unknown as Parameters<typeof uploadLocalFiles>[2];

const events: unknown[][] = [];
await uploadLocalFiles(
["-i", localPath, "out.mp4"],
[{ index: 1, value: localPath, isLocal: true }],
mockClient,
{ onFileProgress: (...args) => events.push(args) },
);

expect(events).toEqual([
["video.mp4", 5, 10, 0, 1],
["video.mp4", 10, 10, 0, 1],
]);
});
});
12 changes: 10 additions & 2 deletions src/commands/ffmpeg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
downloadUrlToFile,
downloadFilesToDir,
outputUrl,
fmtBytes,
type MachineContext,
} from "../lib/progress.js";

Expand Down Expand Up @@ -199,8 +200,15 @@ export default defineCommand({
const localInputs = parsed.inputs.filter((i) => i.isLocal);

if (localInputs.length > 0) {
rewrittenArgs = await steps.step("Uploading", async () => {
return uploadLocalFiles(ffmpegArgs, parsed.inputs, client);
rewrittenArgs = await steps.step("Uploading", async (update) => {
const filePrefix = (index: number, count: number) =>
count > 1 ? `${index + 1}/${count} ` : "";
return uploadLocalFiles(ffmpegArgs, parsed.inputs, client, {
onFileStart: (filename, size, index, count) =>
update(`${filePrefix(index, count)}${filename} · ${fmtBytes(size)}`),
onFileProgress: (filename, loaded, size, index, count) =>
update(`${filePrefix(index, count)}${filename} · ${fmtBytes(loaded)} / ${fmtBytes(size)}`),
});
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ function fmtMs(ms: number): string {
return `${Math.round(ms / 1000)}s`;
}

export function fmtBytes(n: number): string {
if (n < 1024) return `${n} B`;
if (n < 1024 * 1024) return `${(n / 1024).toFixed(0)} KB`;
if (n < 1024 * 1024 * 1024) return `${(n / (1024 * 1024)).toFixed(1)} MB`;
return `${(n / (1024 * 1024 * 1024)).toFixed(2)} GB`;
}

// ── Step renderer ──────────────────────────────────────────────

export class StepRenderer {
Expand Down
12 changes: 11 additions & 1 deletion src/lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ParsedInput } from "./parse-ffmpeg-args.js";

export interface UploadCallbacks {
onFileStart?: (filename: string, size: number, index: number, total: number) => void;
onFileProgress?: (filename: string, loaded: number, size: number, index: number, total: number) => void;
onFileDone?: (filename: string, index: number, total: number) => void;
}

Expand Down Expand Up @@ -34,8 +35,17 @@ export async function uploadLocalFiles(
const buffer = await file.arrayBuffer();
const filename = path.basename(input.value);

// sha256 enables server-side dedup: re-uploading the same bytes skips the
// transfer entirely (the API returns the existing ready asset).
const checksum = new Bun.CryptoHasher("sha256").update(buffer).digest("hex");

callbacks?.onFileStart?.(filename, file.size, i, total);
const asset = await client.uploads.create(new Uint8Array(buffer), { filename });
const asset = await client.uploads.create(new Uint8Array(buffer), {
filename,
checksum,
onProgress: ({ loaded, total: size }) =>
callbacks?.onFileProgress?.(filename, loaded, size, i, total),
});
callbacks?.onFileDone?.(filename, i, total);

result[input.index] = asset.url;
Expand Down