MCPcopy Create free account
hub / github.com/Noumena-Network/code / uploadFile

Function uploadFile

src/services/api/filesApi.ts:379–553  ·  view source on GitHub ↗
(
  filePath: string,
  relativePath: string,
  config: FilesApiConfig,
  opts?: { signal?: AbortSignal },
)

Source from the content-addressed store, hash-verified

377 * @returns Upload result with success/failure status
378 */
379export async function uploadFile(
380 filePath: string,
381 relativePath: string,
382 config: FilesApiConfig,
383 opts?: { signal?: AbortSignal },
384): Promise<UploadResult> {
385 const baseUrl = config.baseUrl || getDefaultApiBaseUrl()
386 const url = `${baseUrl}/v1/files`
387
388 const headers = {
389 Authorization: `Bearer ${config.oauthToken}`,
390 'anthropic-version': ANTHROPIC_VERSION,
391 'anthropic-beta': FILES_API_BETA_HEADER,
392 }
393
394 logDebug(`Uploading file ${filePath} as ${relativePath}`)
395
396 // Read file content first (outside retry loop since it's not a network operation)
397 let content: Buffer
398 try {
399 content = await fs.readFile(filePath)
400 } catch (error) {
401 logEvent('ncode_file_upload_failed', {
402 error_type:
403 'file_read' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
404 })
405 return {
406 path: relativePath,
407 error: errorMessage(error),
408 success: false,
409 }
410 }
411
412 const fileSize = content.length
413
414 if (fileSize > MAX_FILE_SIZE_BYTES) {
415 logEvent('ncode_file_upload_failed', {
416 error_type:
417 'file_too_large' as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
418 })
419 return {
420 path: relativePath,
421 error: `File exceeds maximum size of ${MAX_FILE_SIZE_BYTES} bytes (actual: ${fileSize})`,
422 success: false,
423 }
424 }
425
426 // Use crypto.randomUUID for boundary to avoid collisions when uploads start same millisecond
427 const boundary = `----FormBoundary${randomUUID()}`
428 const filename = path.basename(relativePath)
429
430 // Build the multipart body
431 const bodyParts: Buffer[] = []
432
433 // File part
434 bodyParts.push(
435 Buffer.from(
436 `--${boundary}\r\n` +

Callers 3

createAndUploadGitBundleFunction · 0.85
filesApi.test.tsFile · 0.85
uploadSessionFilesFunction · 0.85

Calls 8

randomUUIDFunction · 0.90
getDefaultApiBaseUrlFunction · 0.85
retryWithBackoffFunction · 0.85
readFileMethod · 0.80
logDebugFunction · 0.70
logEventFunction · 0.70
toStringMethod · 0.65
errorMessageFunction · 0.50

Tested by

no test coverage detected