added download size
This commit is contained in:
@@ -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<DownloadFile[]> {
|
||||
@@ -25,10 +26,15 @@ export async function getAvailableDownloads(datasetId: number): Promise<Download
|
||||
// Sort by timestamp descending (most recent first)
|
||||
matching.sort().reverse();
|
||||
|
||||
return matching.map(file => ({
|
||||
return matching.map(file => {
|
||||
const filePath = join(downloadsDir, file);
|
||||
const stats = statSync(filePath);
|
||||
return {
|
||||
name: file,
|
||||
url: `/public/downloads/${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');
|
||||
|
||||
@@ -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"
|
||||
>
|
||||
<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">
|
||||
<a
|
||||
href={download.url}
|
||||
|
||||
Reference in New Issue
Block a user