(options: DownloadFileOptions)
| 364 | * Download a file from the configured storage provider |
| 365 | */ |
| 366 | export async function downloadFile(options: DownloadFileOptions): Promise<Buffer> { |
| 367 | const { key, context, maxBytes } = options |
| 368 | |
| 369 | if (context) { |
| 370 | const config = getStorageConfig(context) |
| 371 | |
| 372 | if (USE_BLOB_STORAGE) { |
| 373 | const { downloadFromBlob } = await import('@/lib/uploads/providers/blob/client') |
| 374 | const blobConfig = createBlobConfig(config) |
| 375 | return maxBytes === undefined |
| 376 | ? downloadFromBlob(key, blobConfig) |
| 377 | : downloadFromBlob(key, blobConfig, maxBytes) |
| 378 | } |
| 379 | |
| 380 | if (USE_S3_STORAGE) { |
| 381 | const { downloadFromS3 } = await import('@/lib/uploads/providers/s3/client') |
| 382 | const s3Config = createS3Config(config) |
| 383 | return maxBytes === undefined |
| 384 | ? downloadFromS3(key, s3Config) |
| 385 | : downloadFromS3(key, s3Config, maxBytes) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | const { readFile, stat } = await import('fs/promises') |
| 390 | const { join } = await import('path') |
| 391 | const { UPLOAD_DIR_SERVER } = await import('./setup.server') |
| 392 | |
| 393 | const safeKey = sanitizeFileKey(key) |
| 394 | const filePath = join(UPLOAD_DIR_SERVER, safeKey) |
| 395 | |
| 396 | if (maxBytes !== undefined) { |
| 397 | const fileStats = await stat(filePath) |
| 398 | assertKnownSizeWithinLimit(fileStats.size, maxBytes, 'storage download') |
| 399 | } |
| 400 | |
| 401 | return readFile(filePath) |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Stream a file out of the configured storage provider without buffering it in memory. |
no test coverage detected