|
| 1 | +import { type NextRequest, NextResponse } from 'next/server' |
| 2 | +import { createLogger } from '@/lib/logs/console/logger' |
| 3 | +import { getPresignedUrl, getPresignedUrlWithConfig, isUsingCloudStorage } from '@/lib/uploads' |
| 4 | +import { BLOB_EXECUTION_FILES_CONFIG, S3_EXECUTION_FILES_CONFIG } from '@/lib/uploads/setup' |
| 5 | +import { createErrorResponse } from '@/app/api/files/utils' |
| 6 | + |
| 7 | +const logger = createLogger('FileDownload') |
| 8 | + |
| 9 | +export const dynamic = 'force-dynamic' |
| 10 | + |
| 11 | +export async function POST(request: NextRequest) { |
| 12 | + try { |
| 13 | + const body = await request.json() |
| 14 | + const { key, name, storageProvider, bucketName, isExecutionFile } = body |
| 15 | + |
| 16 | + if (!key) { |
| 17 | + return createErrorResponse(new Error('File key is required'), 400) |
| 18 | + } |
| 19 | + |
| 20 | + logger.info(`Generating download URL for file: ${name || key}`) |
| 21 | + |
| 22 | + if (isUsingCloudStorage()) { |
| 23 | + // Generate a fresh 5-minute presigned URL for cloud storage |
| 24 | + try { |
| 25 | + let downloadUrl: string |
| 26 | + |
| 27 | + // Use execution files storage if flagged as execution file |
| 28 | + if (isExecutionFile) { |
| 29 | + logger.info(`Using execution files storage for file: ${key}`) |
| 30 | + downloadUrl = await getPresignedUrlWithConfig( |
| 31 | + key, |
| 32 | + { |
| 33 | + bucket: S3_EXECUTION_FILES_CONFIG.bucket, |
| 34 | + region: S3_EXECUTION_FILES_CONFIG.region, |
| 35 | + }, |
| 36 | + 5 * 60 // 5 minutes |
| 37 | + ) |
| 38 | + } else if (storageProvider && (storageProvider === 's3' || storageProvider === 'blob')) { |
| 39 | + // Use explicitly specified storage provider (legacy support) |
| 40 | + logger.info(`Using specified storage provider '${storageProvider}' for file: ${key}`) |
| 41 | + |
| 42 | + if (storageProvider === 's3') { |
| 43 | + downloadUrl = await getPresignedUrlWithConfig( |
| 44 | + key, |
| 45 | + { |
| 46 | + bucket: bucketName || S3_EXECUTION_FILES_CONFIG.bucket, |
| 47 | + region: S3_EXECUTION_FILES_CONFIG.region, |
| 48 | + }, |
| 49 | + 5 * 60 // 5 minutes |
| 50 | + ) |
| 51 | + } else { |
| 52 | + // blob |
| 53 | + downloadUrl = await getPresignedUrlWithConfig( |
| 54 | + key, |
| 55 | + { |
| 56 | + accountName: BLOB_EXECUTION_FILES_CONFIG.accountName, |
| 57 | + accountKey: BLOB_EXECUTION_FILES_CONFIG.accountKey, |
| 58 | + connectionString: BLOB_EXECUTION_FILES_CONFIG.connectionString, |
| 59 | + containerName: bucketName || BLOB_EXECUTION_FILES_CONFIG.containerName, |
| 60 | + }, |
| 61 | + 5 * 60 // 5 minutes |
| 62 | + ) |
| 63 | + } |
| 64 | + } else { |
| 65 | + // Use default storage (regular uploads) |
| 66 | + logger.info(`Using default storage for file: ${key}`) |
| 67 | + downloadUrl = await getPresignedUrl(key, 5 * 60) // 5 minutes |
| 68 | + } |
| 69 | + |
| 70 | + return NextResponse.json({ |
| 71 | + downloadUrl, |
| 72 | + expiresIn: 300, // 5 minutes in seconds |
| 73 | + fileName: name || key.split('/').pop() || 'download', |
| 74 | + }) |
| 75 | + } catch (error) { |
| 76 | + logger.error(`Failed to generate presigned URL for ${key}:`, error) |
| 77 | + return createErrorResponse( |
| 78 | + error instanceof Error ? error : new Error('Failed to generate download URL'), |
| 79 | + 500 |
| 80 | + ) |
| 81 | + } |
| 82 | + } else { |
| 83 | + // For local storage, return the direct path |
| 84 | + const downloadUrl = `${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/api/files/serve/${key}` |
| 85 | + |
| 86 | + return NextResponse.json({ |
| 87 | + downloadUrl, |
| 88 | + expiresIn: null, // Local URLs don't expire |
| 89 | + fileName: name || key.split('/').pop() || 'download', |
| 90 | + }) |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + logger.error('Error in file download endpoint:', error) |
| 94 | + return createErrorResponse( |
| 95 | + error instanceof Error ? error : new Error('Internal server error'), |
| 96 | + 500 |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
0 commit comments