MCPcopy
hub / github.com/simstudioai/sim / handleCloudProxy

Function handleCloudProxy

apps/sim/app/api/files/serve/[...path]/route.ts:220–285  ·  view source on GitHub ↗
(
  cloudKey: string,
  userId: string,
  raw = false,
  versioned = false,
  signal: AbortSignal | undefined = undefined
)

Source from the content-addressed store, hash-verified

218}
219
220async function handleCloudProxy(
221 cloudKey: string,
222 userId: string,
223 raw = false,
224 versioned = false,
225 signal: AbortSignal | undefined = undefined
226): Promise<NextResponse> {
227 const ownerKey = `user:${userId}`
228 try {
229 const context = inferContextFromKey(cloudKey)
230 logger.info(`Inferred context: ${context} from key pattern: ${cloudKey}`)
231
232 const hasAccess = await verifyFileAccess(
233 cloudKey,
234 userId,
235 undefined, // customConfig
236 context, // context
237 false // isLocal
238 )
239
240 if (!hasAccess) {
241 logger.warn('Unauthorized cloud file access attempt', { userId, key: cloudKey, context })
242 throw new FileNotFoundError(`File not found: ${cloudKey}`)
243 }
244
245 let rawBuffer: Buffer
246
247 if (context === 'copilot') {
248 rawBuffer = await CopilotFiles.downloadCopilotFile(cloudKey)
249 } else {
250 rawBuffer = await downloadFile({
251 key: cloudKey,
252 context,
253 })
254 }
255
256 const segment = cloudKey.split('/').pop() || 'download'
257 const displayName = stripStorageKeyPrefix(segment)
258 const workspaceId = getWorkspaceIdForCompile(cloudKey)
259 const { buffer: fileBuffer, contentType } = await compileDocumentIfNeeded(
260 rawBuffer,
261 displayName,
262 workspaceId,
263 raw,
264 ownerKey,
265 signal
266 )
267
268 logger.info('Cloud file served', {
269 userId,
270 key: cloudKey,
271 size: fileBuffer.length,
272 context,
273 })
274
275 return createFileResponse({
276 buffer: fileBuffer,
277 contentType,

Callers 1

route.tsFile · 0.85

Calls 11

inferContextFromKeyFunction · 0.90
verifyFileAccessFunction · 0.90
downloadFileFunction · 0.90
createFileResponseFunction · 0.90
stripStorageKeyPrefixFunction · 0.85
getWorkspaceIdForCompileFunction · 0.85
compileDocumentIfNeededFunction · 0.85
resolveServeCacheControlFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80
warnMethod · 0.65

Tested by

no test coverage detected