diff --git a/src/app/actions/get-available-downloads.ts b/src/app/actions/get-available-downloads.ts index f096579..f8575e0 100644 --- a/src/app/actions/get-available-downloads.ts +++ b/src/app/actions/get-available-downloads.ts @@ -1,12 +1,13 @@ 'use server'; -import { readdirSync, unlinkSync } from 'fs'; +import { readdirSync, unlinkSync, statSync } from 'fs'; import { join } from 'path'; import { requireAdmin } from '@/lib/auth'; interface DownloadFile { name: string; url: string; + size: number; } export async function getAvailableDownloads(datasetId: number): Promise { @@ -25,10 +26,15 @@ export async function getAvailableDownloads(datasetId: number): Promise ({ - name: file, - url: `/public/downloads/${file}` - })); + return matching.map(file => { + const filePath = join(downloadsDir, file); + const stats = statSync(filePath); + return { + name: file, + url: `/public/downloads/${file}`, + size: stats.size + }; + }); } catch (error) { console.error('Error fetching available downloads:', error); throw new Error('Failed to fetch available downloads'); diff --git a/src/components/DatasetDownloadsModal.tsx b/src/components/DatasetDownloadsModal.tsx index 1c4e07f..9a11511 100644 --- a/src/components/DatasetDownloadsModal.tsx +++ b/src/components/DatasetDownloadsModal.tsx @@ -7,6 +7,15 @@ import { getAvailableDownloads, deleteDownload } from '@/app/actions/get-availab interface DownloadFile { name: string; url: string; + size: number; +} + +function formatFileSize(bytes: number): string { + if (bytes === 0) return '0 B'; + const k = 1024; + const sizes = ['B', 'KB', 'MB', 'GB']; + const i = Math.floor(Math.log(bytes) / Math.log(k)); + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } interface DatasetDownloadsModalProps { @@ -79,7 +88,10 @@ export default function DatasetDownloadsModal({ datasetId }: DatasetDownloadsMod key={download.name} className="flex items-center justify-between p-3 bg-gray-50 rounded hover:bg-gray-100 transition-colors" > - {download.name} +
+
{download.name}
+
{formatFileSize(download.size)}
+