added download size

This commit is contained in:
averel10
2026-03-20 08:43:12 +01:00
parent c3bede86fe
commit ea7c75c523
2 changed files with 24 additions and 6 deletions

View File

@@ -1,12 +1,13 @@
'use server'; 'use server';
import { readdirSync, unlinkSync } from 'fs'; import { readdirSync, unlinkSync, statSync } from 'fs';
import { join } from 'path'; import { join } from 'path';
import { requireAdmin } from '@/lib/auth'; import { requireAdmin } from '@/lib/auth';
interface DownloadFile { interface DownloadFile {
name: string; name: string;
url: string; url: string;
size: number;
} }
export async function getAvailableDownloads(datasetId: number): Promise<DownloadFile[]> { export async function getAvailableDownloads(datasetId: number): Promise<DownloadFile[]> {
@@ -25,10 +26,15 @@ export async function getAvailableDownloads(datasetId: number): Promise<Download
// Sort by timestamp descending (most recent first) // Sort by timestamp descending (most recent first)
matching.sort().reverse(); matching.sort().reverse();
return matching.map(file => ({ return matching.map(file => {
const filePath = join(downloadsDir, file);
const stats = statSync(filePath);
return {
name: file, name: file,
url: `/public/downloads/${file}` url: `/public/downloads/${file}`,
})); size: stats.size
};
});
} catch (error) { } catch (error) {
console.error('Error fetching available downloads:', error); console.error('Error fetching available downloads:', error);
throw new Error('Failed to fetch available downloads'); throw new Error('Failed to fetch available downloads');

View File

@@ -7,6 +7,15 @@ import { getAvailableDownloads, deleteDownload } from '@/app/actions/get-availab
interface DownloadFile { interface DownloadFile {
name: string; name: string;
url: 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 { interface DatasetDownloadsModalProps {
@@ -79,7 +88,10 @@ export default function DatasetDownloadsModal({ datasetId }: DatasetDownloadsMod
key={download.name} key={download.name}
className="flex items-center justify-between p-3 bg-gray-50 rounded hover:bg-gray-100 transition-colors" className="flex items-center justify-between p-3 bg-gray-50 rounded hover:bg-gray-100 transition-colors"
> >
<span className="text-gray-700 text-sm truncate flex-1">{download.name}</span> <div className="flex-1 truncate">
<div className="text-gray-700 text-sm truncate">{download.name}</div>
<div className="text-gray-500 text-xs">{formatFileSize(download.size)}</div>
</div>
<div className="ml-4 flex gap-2"> <div className="ml-4 flex gap-2">
<a <a
href={download.url} href={download.url}