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

Function handleLocalFile

apps/sim/app/api/files/serve/[...path]/route.ts:161–218  ·  view source on GitHub ↗
(
  filename: string,
  userId: string,
  raw: boolean,
  versioned: boolean,
  signal: AbortSignal | undefined
)

Source from the content-addressed store, hash-verified

159)
160
161async function handleLocalFile(
162 filename: string,
163 userId: string,
164 raw: boolean,
165 versioned: boolean,
166 signal: AbortSignal | undefined
167): Promise<NextResponse> {
168 const ownerKey = `user:${userId}`
169 try {
170 const contextParam: StorageContext | undefined = inferContextFromKey(filename) as
171 | StorageContext
172 | undefined
173
174 const hasAccess = await verifyFileAccess(
175 filename,
176 userId,
177 undefined, // customConfig
178 contextParam, // context
179 true // isLocal
180 )
181
182 if (!hasAccess) {
183 logger.warn('Unauthorized local file access attempt', { userId, filename })
184 throw new FileNotFoundError(`File not found: ${filename}`)
185 }
186
187 const filePath = await findLocalFile(filename)
188
189 if (!filePath) {
190 throw new FileNotFoundError(`File not found: ${filename}`)
191 }
192
193 const rawBuffer = await readFile(filePath)
194 const segment = filename.split('/').pop() || filename
195 const displayName = stripStorageKeyPrefix(segment)
196 const workspaceId = getWorkspaceIdForCompile(filename)
197 const { buffer: fileBuffer, contentType } = await compileDocumentIfNeeded(
198 rawBuffer,
199 displayName,
200 workspaceId,
201 raw,
202 ownerKey,
203 signal
204 )
205
206 logger.info('Local file served', { userId, filename, size: fileBuffer.length })
207
208 return createFileResponse({
209 buffer: fileBuffer,
210 contentType,
211 filename: displayName,
212 cacheControl: resolveServeCacheControl(versioned, contextParam),
213 })
214 } catch (error) {
215 logger.error('Error reading local file:', error)
216 throw error
217 }
218}

Callers 1

route.tsFile · 0.70

Calls 12

inferContextFromKeyFunction · 0.90
verifyFileAccessFunction · 0.90
findLocalFileFunction · 0.90
createFileResponseFunction · 0.90
readFileFunction · 0.85
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