( cloudKey: string, userId: string, raw = false, versioned = false, signal: AbortSignal | undefined = undefined )
| 218 | } |
| 219 | |
| 220 | async 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, |
no test coverage detected