( key: string, customConfig?: StorageConfig )
| 26 | * @returns File metadata object with userId, workspaceId, originalName, uploadedAt, etc. |
| 27 | */ |
| 28 | export async function getFileMetadata( |
| 29 | key: string, |
| 30 | customConfig?: StorageConfig |
| 31 | ): Promise<Record<string, string>> { |
| 32 | const { getFileMetadataByKey } = await import('../server/metadata') |
| 33 | const metadataRecord = await getFileMetadataByKey(key) |
| 34 | |
| 35 | if (metadataRecord) { |
| 36 | return { |
| 37 | userId: metadataRecord.userId, |
| 38 | workspaceId: metadataRecord.workspaceId || '', |
| 39 | originalName: metadataRecord.originalName, |
| 40 | uploadedAt: metadataRecord.uploadedAt.toISOString(), |
| 41 | purpose: metadataRecord.context, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (USE_BLOB_STORAGE) { |
| 46 | const { getBlobServiceClient } = await import('@/lib/uploads/providers/blob/client') |
| 47 | const { BLOB_CONFIG } = await import('@/lib/uploads/config') |
| 48 | |
| 49 | let blobServiceClient = await getBlobServiceClient() |
| 50 | let containerName = BLOB_CONFIG.containerName |
| 51 | |
| 52 | if (customConfig) { |
| 53 | const { BlobServiceClient, StorageSharedKeyCredential } = await import('@azure/storage-blob') |
| 54 | if (customConfig.connectionString) { |
| 55 | blobServiceClient = BlobServiceClient.fromConnectionString(customConfig.connectionString) |
| 56 | } else if (customConfig.accountName && customConfig.accountKey) { |
| 57 | const credential = new StorageSharedKeyCredential( |
| 58 | customConfig.accountName, |
| 59 | customConfig.accountKey |
| 60 | ) |
| 61 | blobServiceClient = new BlobServiceClient( |
| 62 | `https://${customConfig.accountName}.blob.core.windows.net`, |
| 63 | credential |
| 64 | ) |
| 65 | } |
| 66 | containerName = customConfig.containerName || containerName |
| 67 | } |
| 68 | |
| 69 | const containerClient = blobServiceClient.getContainerClient(containerName) |
| 70 | const blockBlobClient = containerClient.getBlockBlobClient(key) |
| 71 | const properties = await blockBlobClient.getProperties() |
| 72 | return properties.metadata || {} |
| 73 | } |
| 74 | |
| 75 | if (USE_S3_STORAGE) { |
| 76 | const { getS3Client } = await import('@/lib/uploads/providers/s3/client') |
| 77 | const { HeadObjectCommand } = await import('@aws-sdk/client-s3') |
| 78 | const { S3_CONFIG } = await import('@/lib/uploads/config') |
| 79 | |
| 80 | const s3Client = getS3Client() |
| 81 | const bucket = customConfig?.bucket || S3_CONFIG.bucket |
| 82 | |
| 83 | if (!bucket) { |
| 84 | throw new Error('S3 bucket not configured') |
| 85 | } |
no test coverage detected