MCPcopy Index your code
hub / github.com/simstudioai/sim / handleCloudFile

Function handleCloudFile

apps/sim/app/api/files/parse/route.ts:608–797  ·  view source on GitHub ↗

* Handle file stored in cloud storage * If executionContext is provided and file is not already from execution storage, * copies the file to execution storage and returns UserFile

(
  filePath: string,
  fileType: string,
  explicitContext: string | undefined,
  userId: string,
  executionContext?: ExecutionContext,
  maxDownloadBytes = MAX_DOWNLOAD_SIZE_BYTES,
  maxParsedOutputBytes?: number
)

Source from the content-addressed store, hash-verified

606 * copies the file to execution storage and returns UserFile
607 */
608async function handleCloudFile(
609 filePath: string,
610 fileType: string,
611 explicitContext: string | undefined,
612 userId: string,
613 executionContext?: ExecutionContext,
614 maxDownloadBytes = MAX_DOWNLOAD_SIZE_BYTES,
615 maxParsedOutputBytes?: number
616): Promise<ParseResult> {
617 try {
618 const cloudKey = extractStorageKey(filePath)
619
620 logger.info('Extracted cloud key:', cloudKey)
621
622 const context = (explicitContext as StorageContext) || inferContextFromKey(cloudKey)
623
624 const hasAccess = await verifyFileAccess(
625 cloudKey,
626 userId,
627 undefined, // customConfig
628 context, // context
629 false // isLocal
630 )
631
632 if (!hasAccess) {
633 logger.warn('Unauthorized cloud file parse attempt', { userId, key: cloudKey, context })
634 return {
635 success: false,
636 error: 'File not found',
637 filePath,
638 }
639 }
640
641 let originalFilename: string | undefined
642 if (context === 'workspace') {
643 try {
644 const fileRecord = await getFileMetadataByKey(cloudKey, 'workspace')
645
646 if (fileRecord) {
647 originalFilename = fileRecord.originalName
648 logger.debug(`Found original filename for workspace file: ${originalFilename}`)
649 }
650 } catch (dbError) {
651 logger.debug(`Failed to lookup original filename for ${cloudKey}:`, dbError)
652 }
653 }
654
655 const fileBuffer = await StorageService.downloadFile({
656 key: cloudKey,
657 context,
658 maxBytes: maxDownloadBytes,
659 })
660 logger.info(
661 `Downloaded file from ${context} storage (${explicitContext ? 'explicit' : 'inferred'}): ${cloudKey}, size: ${fileBuffer.length} bytes`
662 )
663
664 const filename = originalFilename || cloudKey.split('/').pop() || cloudKey
665 const extension = path.extname(filename).toLowerCase().substring(1)

Callers 1

parseFileSingleFunction · 0.85

Calls 15

extractStorageKeyFunction · 0.90
inferContextFromKeyFunction · 0.90
verifyFileAccessFunction · 0.90
getFileMetadataByKeyFunction · 0.90
getMimeTypeFromExtensionFunction · 0.90
getViewerUrlFunction · 0.90
generateShortIdFunction · 0.90
uploadExecutionFileFunction · 0.90
isSupportedFileTypeFunction · 0.90
isPayloadSizeLimitErrorFunction · 0.90
handlePdfBufferFunction · 0.85

Tested by

no test coverage detected