import { defineSchema, lazySchema } from "veryfront/schemas"; import type { InferSchema } from "veryfront/extensions/schema"; type SafeParseResult = { success: true; data: T } | { success: false; error: Error & { issues: unknown[] }; }; import { createFileSystem, cwd, lookupMimeType } from "veryfront/platform"; import { dirname, join, normalize, resolve } from "veryfront/platform/path"; import { withSpan } from "veryfront/observability/otlp-setup"; import { INVALID_ARGUMENT } from "veryfront/errors"; import { cliLogger } from "#cli/utils"; import { parseArgsOrThrow } from "#cli/shared/args"; import { type ApiClient, createApiClient, resolveLinkedProjectConfigWithAuth, } from "#cli/shared/config"; import type { ParsedArgs } from "#cli/shared/types"; import { printJson } from "../../shared/json-output.ts"; import { getBooleanArg, getStringArg } from "../../shared/parsed-args.ts"; const privateDownloadFileSystem = createFileSystem(); const writePrivateDownload = privateDownloadFileSystem.writeFileStreamAtomic; export interface UploadItem { type: "file" | "folder"; path: string; file_name?: string; /** File size in bytes. */ size_bytes?: number; /** Earlier spelling of `size_bytes`. */ size?: number; content_type?: string | null; status?: string; visibility?: string; created_at?: string; updated_at?: string; } /** A visible folder in the `folders` array of an uploads listing. */ interface UploadFolder { path: string; visibility?: string; permission?: string; created_at?: string; } interface UploadsListResponse { /** Uploads. An API that predates `folders` also lists folders here as `type: "folder"` items. */ data: UploadItem[]; /** Visible folders, unpaginated: every page carries the same array. */ folders?: UploadFolder[]; page_info?: { next?: string | null; prev?: string | null; }; } interface SignedUrlResponse { signed_url: string; expires_at: string; } interface CreateUploadResponse { upload_url: string; path: string; upload_id: string; required_headers: Record; status?: "pending"; } const getUploadListArgsSchema = defineSchema((v) => v.object({ projectSlug: v.string().optional(), projectDir: v.string().optional(), path: v.string().optional(), limit: v.coerce.number().min(1).max(100).optional(), recursive: v.boolean().default(true), json: v.boolean().default(false), quiet: v.boolean().default(false), }) ); const UploadListArgsSchema = lazySchema(getUploadListArgsSchema); export type UploadListOptions = InferSchema>; const getUploadPullArgsSchema = defineSchema((v) => v.object({ projectSlug: v.string().optional(), projectDir: v.string().optional(), uploads: v.array(v.string()).default([]), path: v.string().optional(), all: v.boolean().default(false), outputDir: v.string().default(join(cwd(), "uploads")), json: v.boolean().default(false), quiet: v.boolean().default(false), }) ); const UploadPullArgsSchema = lazySchema(getUploadPullArgsSchema); export type UploadPullOptions = InferSchema>; const getUploadPutArgsSchema = defineSchema((v) => v.object({ projectSlug: v.string().optional(), projectDir: v.string().optional(), uploadPath: v.string().min(1), from: v.string().min(1), contentType: v.string().optional(), json: v.boolean().default(false), quiet: v.boolean().default(false), }) ); const UploadPutArgsSchema = lazySchema(getUploadPutArgsSchema); export type UploadPutOptions = InferSchema>; const getUploadDeleteArgsSchema = defineSchema((v) => v.object({ projectSlug: v.string().optional(), projectDir: v.string().optional(), uploadPath: v.string().min(1), json: v.boolean().default(false), quiet: v.boolean().default(false), }) ); const UploadDeleteArgsSchema = lazySchema(getUploadDeleteArgsSchema); export type UploadDeleteOptions = InferSchema>; function formatUploadItem(item: UploadItem): string { if (item.type === "folder") return `[folder] ${item.path}`; const sizeBytes = item.size_bytes ?? item.size; const size = typeof sizeBytes === "number" ? ` (${sizeBytes} bytes)` : ""; return `[file] ${item.path}${size}`; } function showUploadsUsage(): void { console.log(` Veryfront Uploads Usage: veryfront uploads list [options] veryfront uploads pull [options] veryfront uploads pull --path --all [options] veryfront uploads put --from [options] veryfront uploads delete [options] Subcommands: list List uploads in the project uploads store pull Download one or many uploads into a local directory put Upload or replace a file in the project uploads store delete Delete an upload by path `); } function normalizeUploadPath(uploadPath: string): string { const normalizedPath = normalize(uploadPath).replace(/^\/+/, ""); if (!normalizedPath || normalizedPath.startsWith("..") || normalizedPath.startsWith("/")) { throw INVALID_ARGUMENT.create({ detail: `Invalid upload path: ${uploadPath}` }); } return normalizedPath; } export function parseUploadsListArgs( args: ParsedArgs, ): SafeParseResult { return UploadListArgsSchema.safeParse({ projectSlug: getStringArg(args, "project", "p", "project-slug"), projectDir: getStringArg(args, "project-dir", "dir", "d"), path: getStringArg(args, "path"), limit: args.limit, recursive: args.recursive === undefined ? true : Boolean(args.recursive), json: getBooleanArg(args, "json", "j"), quiet: getBooleanArg(args, "quiet", "q"), }) as SafeParseResult; } export function parseUploadsPullArgs( args: ParsedArgs, ): SafeParseResult { return UploadPullArgsSchema.safeParse({ projectSlug: getStringArg(args, "project", "p", "project-slug"), projectDir: getStringArg(args, "project-dir", "dir", "d"), uploads: args._.slice(2).filter((value): value is string => typeof value === "string"), path: getStringArg(args, "path"), all: getBooleanArg(args, "all"), outputDir: getStringArg(args, "output-dir") ?? join(cwd(), "uploads"), json: getBooleanArg(args, "json", "j"), quiet: getBooleanArg(args, "quiet", "q"), }) as SafeParseResult; } export function parseUploadsPutArgs( args: ParsedArgs, ): SafeParseResult { return UploadPutArgsSchema.safeParse({ projectSlug: getStringArg(args, "project", "p", "project-slug"), projectDir: getStringArg(args, "project-dir", "dir", "d"), uploadPath: typeof args._[2] === "string" ? args._[2] : "", from: getStringArg(args, "from") ?? "", contentType: getStringArg(args, "content-type"), json: getBooleanArg(args, "json", "j"), quiet: getBooleanArg(args, "quiet", "q"), }) as SafeParseResult; } export function parseUploadsDeleteArgs( args: ParsedArgs, ): SafeParseResult { return UploadDeleteArgsSchema.safeParse({ projectSlug: getStringArg(args, "project", "p", "project-slug"), projectDir: getStringArg(args, "project-dir", "dir", "d"), uploadPath: typeof args._[2] === "string" ? args._[2] : "", json: getBooleanArg(args, "json", "j"), quiet: getBooleanArg(args, "quiet", "q"), }) as SafeParseResult; } export function buildUploadsListUrl(projectSlug: string): string { return `/projects/${projectSlug}/uploads`; } export function buildUploadCreateUrl(projectSlug: string): string { return `/projects/${projectSlug}/uploads`; } export function buildUploadSignedUrlPath(projectSlug: string, uploadPath: string): string { return `/projects/${projectSlug}/uploads/${ encodeURIComponent(normalizeUploadPath(uploadPath)) }/url`; } function buildUploadDeleteUrl(projectSlug: string, uploadPath: string): string { return `/projects/${projectSlug}/uploads/${encodeURIComponent(normalizeUploadPath(uploadPath))}`; } /** * Report an upload's size in `size_bytes` whichever spelling the API answered * with, so the `list --json` output does not depend on the API version. */ function withUploadSizeBytes(item: UploadItem): UploadItem { if (item.size_bytes !== undefined || item.size === undefined) return item; return { ...item, size_bytes: item.size }; } export async function listAllUploads( client: ApiClient, projectSlug: string, options: Partial> = {}, ): Promise { const uploads: UploadItem[] = []; let folders: UploadItem[] | undefined; const legacyFolders: UploadItem[] = []; let cursor: string | undefined; do { const params: Record = { limit: String(options.limit ?? 100), recursive: String(options.recursive ?? true), }; if (options.path) params.path = options.path; if (cursor) params.cursor = cursor; const response = await client.get( buildUploadsListUrl(projectSlug), params, ); // Folders are unpaginated: every page carries the same `folders` array, so // the first page that has one supplies them. The `type: "folder"` items in // `data` repeat it. They are read only while no page has carried a // `folders` array, and then from every page, because an API without the // array pages its folder items with the uploads. if (response.folders) { folders ??= response.folders.map((folder) => ({ type: "folder" as const, ...folder })); } else if (!folders) { legacyFolders.push(...response.data.filter((item) => item.type === "folder")); } uploads.push( ...response.data.filter((item) => item.type !== "folder").map(withUploadSizeBytes), ); cursor = response.page_info?.next ?? undefined; } while (cursor); return [...(folders ?? legacyFolders), ...uploads]; } export function resolveUploadOutputPath(uploadPath: string, outputDir: string): string { const normalizedPath = normalizeUploadPath(uploadPath); const fullPath = resolve(outputDir, normalizedPath); const resolvedOutputDir = resolve(outputDir); if (!fullPath.startsWith(`${resolvedOutputDir}/`) && fullPath !== resolvedOutputDir) { throw INVALID_ARGUMENT.create({ detail: `Invalid upload path: ${uploadPath}` }); } return fullPath; } export async function downloadUploadToFile( client: ApiClient, projectSlug: string, uploadPath: string, outputDir: string, signal?: AbortSignal, ): Promise<{ uploadPath: string; localPath: string; bytes: number }> { const fs = privateDownloadFileSystem; signal?.throwIfAborted(); if (!client.getStream) throw new Error("API client does not support upload downloads"); const localPath = resolveUploadOutputPath(uploadPath, outputDir); const response = await client.getStream( `${buildUploadsListUrl(projectSlug)}/${encodeURIComponent(normalizeUploadPath(uploadPath))}`, { signal }, ); try { await fs.mkdir(dirname(localPath), { recursive: true }); if (!writePrivateDownload) { throw new Error("Filesystem does not support atomic streaming upload downloads"); } const bytes = await writePrivateDownload(localPath, response, signal); return { uploadPath: normalizeUploadPath(uploadPath), localPath, bytes }; } finally { await response.cancel().catch(() => {}); } } export async function uploadLocalFileToUploads( client: ApiClient, projectSlug: string, uploadPath: string, localPath: string, contentType?: string, ): Promise { const fs = createFileSystem(); const normalizedPath = normalizeUploadPath(uploadPath); const bytes = await fs.readFile(localPath); const inferredContentType = contentType ?? lookupMimeType(localPath) ?? "application/octet-stream"; const createResponse = await client.post( buildUploadCreateUrl(projectSlug), { path: normalizedPath, content_type: inferredContentType, size_bytes: bytes.byteLength, }, ); const uploadResponse = await fetch(createResponse.upload_url, { method: "PUT", headers: createResponse.required_headers, body: bytes as BodyInit, }); if (!uploadResponse.ok) { throw new Error(`Failed to upload file to storage: ${normalizedPath}`); } return createResponse; } export async function deleteUpload( client: ApiClient, projectSlug: string, uploadPath: string, ): Promise { await client.delete(buildUploadDeleteUrl(projectSlug, uploadPath)); } export async function uploadsCommand(args: ParsedArgs): Promise { const subcommand = typeof args._[1] === "string" ? args._[1] : undefined; if (!subcommand || subcommand === "help") { showUploadsUsage(); return; } await withSpan("cli.command.uploads", async () => { switch (subcommand) { case "list": { const options = parseArgsOrThrow(parseUploadsListArgs, "uploads list", args); const config = await resolveLinkedProjectConfigWithAuth( options.projectDir, options.projectSlug, ); const client = createApiClient(config); const uploads = await listAllUploads(client, config.projectSlug, options); if (options.json) { printJson(uploads); return; } if (!uploads.length) { cliLogger.info("No uploads found."); cliLogger.info( " Upload files with: veryfront uploads put --from ", ); return; } for (const upload of uploads) { console.log(formatUploadItem(upload)); } return; } case "pull": { const options = parseArgsOrThrow(parseUploadsPullArgs, "uploads pull", args); const config = await resolveLinkedProjectConfigWithAuth( options.projectDir, options.projectSlug, ); const client = createApiClient(config); let targets = options.uploads; if (options.all) { const uploads = await listAllUploads(client, config.projectSlug, { path: options.path, recursive: true, limit: 100, }); targets = uploads.filter((item) => item.type === "file").map((item) => item.path); } if (!targets.length) { throw new Error("No uploads selected. Pass upload paths or use --path with --all."); } const results = [] as Array<{ uploadPath: string; localPath: string; bytes: number }>; for (const uploadPath of targets) { const result = await downloadUploadToFile( client, config.projectSlug, uploadPath, options.outputDir, ); results.push(result); if (!options.quiet && !options.json) { cliLogger.info(`Downloaded ${uploadPath} -> ${result.localPath}`); } } if (options.json) { printJson(results); return; } if (!options.quiet) { cliLogger.info(`Pulled ${results.length} upload(s) into ${options.outputDir}.`); } return; } case "put": { const options = parseArgsOrThrow(parseUploadsPutArgs, "uploads put", args); const config = await resolveLinkedProjectConfigWithAuth( options.projectDir, options.projectSlug, ); const client = createApiClient(config); const result = await uploadLocalFileToUploads( client, config.projectSlug, options.uploadPath, options.from, options.contentType, ); if (options.json) { printJson(result); return; } if (!options.quiet) { cliLogger.info(`Uploaded ${options.from} -> ${normalizeUploadPath(options.uploadPath)}`); } return; } case "delete": case "rm": { const options = parseArgsOrThrow(parseUploadsDeleteArgs, "uploads delete", args); const config = await resolveLinkedProjectConfigWithAuth( options.projectDir, options.projectSlug, ); const client = createApiClient(config); await deleteUpload(client, config.projectSlug, options.uploadPath); if (options.json) { printJson({ success: true, path: normalizeUploadPath(options.uploadPath) }); return; } if (!options.quiet) { cliLogger.info(`Deleted upload ${normalizeUploadPath(options.uploadPath)}`); } return; } default: showUploadsUsage(); } }); }