6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
132 lines
4.2 KiB
TypeScript
132 lines
4.2 KiB
TypeScript
import { isRecordLike } from '@sim/utils/object'
|
|
import type { StorageContext } from '@/lib/uploads/shared/types'
|
|
|
|
export interface ApiFallbackUploadOptions {
|
|
workspaceId?: string
|
|
workflowId?: string
|
|
executionId?: string
|
|
}
|
|
|
|
export interface ApiFallbackUploadMetadata {
|
|
path: string
|
|
key?: string
|
|
id?: string
|
|
name?: string
|
|
size?: number
|
|
type?: string
|
|
uploadedAt?: string
|
|
expiresAt?: string
|
|
}
|
|
|
|
function getOptionalString(value: unknown): string | undefined {
|
|
if (typeof value !== 'string') return undefined
|
|
const trimmed = value.trim()
|
|
return trimmed || undefined
|
|
}
|
|
|
|
function getOptionalNumber(value: unknown): number | undefined {
|
|
return typeof value === 'number' && Number.isFinite(value) ? value : undefined
|
|
}
|
|
|
|
function getUploadFailureReason(value: unknown, response: Response): string {
|
|
if (isRecordLike(value)) {
|
|
const message = getOptionalString(value.message) ?? getOptionalString(value.error)
|
|
if (message) return message
|
|
}
|
|
|
|
return `Failed to upload file: ${response.status}`
|
|
}
|
|
|
|
function normalizeUploadMetadata(value: unknown): ApiFallbackUploadMetadata {
|
|
if (!isRecordLike(value)) {
|
|
throw new Error('Invalid upload response: expected file metadata')
|
|
}
|
|
|
|
const fileInfo = isRecordLike(value.fileInfo) ? value.fileInfo : undefined
|
|
const path =
|
|
getOptionalString(fileInfo?.path) ??
|
|
getOptionalString(value.path) ??
|
|
getOptionalString(value.url)
|
|
if (!path) {
|
|
throw new Error('Invalid upload response: missing path')
|
|
}
|
|
|
|
return {
|
|
path,
|
|
key: getOptionalString(fileInfo?.key) ?? getOptionalString(value.key),
|
|
id: getOptionalString(fileInfo?.id) ?? getOptionalString(value.id),
|
|
name:
|
|
getOptionalString(fileInfo?.name) ??
|
|
getOptionalString(value.name) ??
|
|
getOptionalString(value.fileName),
|
|
size: getOptionalNumber(fileInfo?.size) ?? getOptionalNumber(value.size),
|
|
type: getOptionalString(fileInfo?.type) ?? getOptionalString(value.type),
|
|
uploadedAt: getOptionalString(fileInfo?.uploadedAt) ?? getOptionalString(value.uploadedAt),
|
|
expiresAt: getOptionalString(fileInfo?.expiresAt) ?? getOptionalString(value.expiresAt),
|
|
}
|
|
}
|
|
|
|
async function parseUploadResponse(response: Response): Promise<ApiFallbackUploadMetadata> {
|
|
if (!response.ok) {
|
|
const errorData: unknown = await response.json().catch(() => null)
|
|
throw new Error(getUploadFailureReason(errorData, response))
|
|
}
|
|
|
|
let data: unknown
|
|
try {
|
|
data = await response.json()
|
|
} catch {
|
|
throw new Error('Invalid upload response: response was not JSON')
|
|
}
|
|
|
|
const results = isRecordLike(data) && Array.isArray(data.files) ? data.files : [data]
|
|
if (results.length === 0) {
|
|
throw new Error('Invalid upload response: no files returned')
|
|
}
|
|
if (results.length > 1) {
|
|
throw new Error('Invalid upload response: multiple files returned for a single-file upload')
|
|
}
|
|
|
|
return normalizeUploadMetadata(results[0])
|
|
}
|
|
|
|
/**
|
|
* Uploads one file through the server-proxied multipart fallback and returns
|
|
* normalized metadata for either the singular or `{ files: [...] }` response.
|
|
*/
|
|
export async function uploadViaApiFallbackWithMetadata(
|
|
file: File,
|
|
context: StorageContext,
|
|
options: ApiFallbackUploadOptions = {}
|
|
): Promise<ApiFallbackUploadMetadata> {
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
formData.append('context', context)
|
|
if (options.workspaceId) {
|
|
formData.append('workspaceId', options.workspaceId)
|
|
}
|
|
if (options.workflowId) {
|
|
formData.append('workflowId', options.workflowId)
|
|
}
|
|
if (options.executionId) {
|
|
formData.append('executionId', options.executionId)
|
|
}
|
|
|
|
// boundary-raw-fetch: local-dev fallback when cloud storage is not configured; multipart upload incompatible with requestJson
|
|
const response = await fetch('/api/files/upload', { method: 'POST', body: formData })
|
|
return parseUploadResponse(response)
|
|
}
|
|
|
|
/**
|
|
* Server-proxied fallback used only when cloud storage isn't configured (local dev).
|
|
* Production always takes the presigned PUT path.
|
|
*/
|
|
export async function uploadViaApiFallback(
|
|
file: File,
|
|
context: StorageContext,
|
|
workspaceId?: string
|
|
): Promise<{ path: string; key?: string }> {
|
|
const { path, key } = await uploadViaApiFallbackWithMetadata(file, context, { workspaceId })
|
|
return { path, key }
|
|
}
|