(
userFile: UserFile,
requestId: string,
logger: Logger,
options: { maxBytes?: number } = {}
)
| 264 | * @returns Buffer containing file data |
| 265 | */ |
| 266 | export async function downloadFileFromStorage( |
| 267 | userFile: UserFile, |
| 268 | requestId: string, |
| 269 | logger: Logger, |
| 270 | options: { maxBytes?: number } = {} |
| 271 | ): Promise<Buffer> { |
| 272 | let buffer: Buffer |
| 273 | if (options.maxBytes !== undefined && userFile.size > options.maxBytes) { |
| 274 | assertKnownSizeWithinLimit(userFile.size, options.maxBytes, 'storage file download') |
| 275 | } |
| 276 | |
| 277 | if (isExecutionFile(userFile)) { |
| 278 | logger.info(`[${requestId}] Downloading from execution storage: ${userFile.key}`) |
| 279 | const { downloadExecutionFile } = await import( |
| 280 | '@/lib/uploads/contexts/execution/execution-file-manager' |
| 281 | ) |
| 282 | buffer = await downloadExecutionFile(userFile, { maxBytes: options.maxBytes }) |
| 283 | } else if (userFile.key) { |
| 284 | const context = (userFile.context as StorageContext) || inferContextFromKey(userFile.key) |
| 285 | logger.info( |
| 286 | `[${requestId}] Downloading from ${context} storage (${userFile.context ? 'explicit' : 'inferred'}): ${userFile.key}` |
| 287 | ) |
| 288 | |
| 289 | const { downloadFile } = await import('@/lib/uploads/core/storage-service') |
| 290 | buffer = await downloadFile({ |
| 291 | key: userFile.key, |
| 292 | context, |
| 293 | maxBytes: options.maxBytes, |
| 294 | }) |
| 295 | } else { |
| 296 | throw new Error('File has no key - cannot download') |
| 297 | } |
| 298 | |
| 299 | if (options.maxBytes !== undefined) { |
| 300 | assertKnownSizeWithinLimit(buffer.length, options.maxBytes, 'storage file download') |
| 301 | } |
| 302 | |
| 303 | return buffer |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Result of {@link downloadServableFileFromStorage}: the bytes a consumer should |
no test coverage detected