added download list
This commit is contained in:
63
src/app/actions/get-available-downloads.ts
Normal file
63
src/app/actions/get-available-downloads.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
'use server';
|
||||
|
||||
import { readdirSync, unlinkSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { requireAdmin } from '@/lib/auth';
|
||||
|
||||
interface DownloadFile {
|
||||
name: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export async function getAvailableDownloads(datasetId: number): Promise<DownloadFile[]> {
|
||||
const result = await requireAdmin();
|
||||
if (!result.authenticated || !result.admin) {
|
||||
throw new Error('Unauthorized');
|
||||
}
|
||||
|
||||
try {
|
||||
const downloadsDir = join(process.cwd(), 'public', 'downloads');
|
||||
const files = readdirSync(downloadsDir);
|
||||
|
||||
const pattern = new RegExp(`^dataset-${datasetId}-export-`);
|
||||
const matching = files.filter(file => pattern.test(file) && file.endsWith('.zip'));
|
||||
|
||||
// Sort by timestamp descending (most recent first)
|
||||
matching.sort().reverse();
|
||||
|
||||
return matching.map(file => ({
|
||||
name: file,
|
||||
url: `/public/downloads/${file}`
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error('Error fetching available downloads:', error);
|
||||
throw new Error('Failed to fetch available downloads');
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteDownload(fileName: string): Promise<void> {
|
||||
const result = await requireAdmin();
|
||||
if (!result.authenticated || !result.admin) {
|
||||
throw new Error('Unauthorized');
|
||||
}
|
||||
|
||||
try {
|
||||
// Validate filename to prevent directory traversal attacks
|
||||
if (fileName.includes('..') || fileName.includes('/') || fileName.includes('\\')) {
|
||||
throw new Error('Invalid filename');
|
||||
}
|
||||
|
||||
const downloadsDir = join(process.cwd(), 'public', 'downloads');
|
||||
const filePath = join(downloadsDir, fileName);
|
||||
|
||||
// Ensure the file is within the downloads directory
|
||||
if (!filePath.startsWith(downloadsDir)) {
|
||||
throw new Error('Invalid file path');
|
||||
}
|
||||
|
||||
unlinkSync(filePath);
|
||||
} catch (error) {
|
||||
console.error('Error deleting download:', error);
|
||||
throw new Error('Failed to delete download');
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { dataset } from '@/lib/model/dataset';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import EditableDatasetHeader from '@/components/EditableDatasetHeader';
|
||||
import DatasetEntriesList from '@/components/DatasetEntriesList';
|
||||
import DatasetDownloadsModal from '@/components/DatasetDownloadsModal';
|
||||
import UploadDatasetEntriesModal from '@/components/UploadDatasetEntriesModal';
|
||||
import UpdateDatasetMetadataModal from '@/components/UpdateDatasetMetadataModal';
|
||||
import RemoveAllEntriesButton from '@/components/RemoveAllEntriesButton';
|
||||
@@ -71,6 +72,7 @@ export default async function DatasetPage({ params }: DatasetPageProps) {
|
||||
<div className="flex gap-3 mb-6">
|
||||
<UploadDatasetEntriesModal datasetId={datasetId} />
|
||||
<UpdateDatasetMetadataModal datasetId={datasetId} />
|
||||
<DatasetDownloadsModal datasetId={datasetId} />
|
||||
<RemoveAllEntriesButton datasetId={datasetId} />
|
||||
<DeleteDatasetButton datasetId={datasetId} datasetName={ds.name} />
|
||||
</div>
|
||||
|
||||
93
src/components/DatasetDownloadsList.tsx
Normal file
93
src/components/DatasetDownloadsList.tsx
Normal file
@@ -0,0 +1,93 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { getAvailableDownloads, deleteDownload } from '@/app/actions/get-available-downloads';
|
||||
|
||||
interface DownloadFile {
|
||||
name: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
interface DatasetDownloadsListProps {
|
||||
datasetId: number;
|
||||
}
|
||||
|
||||
export default function DatasetDownloadsList({ datasetId }: DatasetDownloadsListProps) {
|
||||
const [downloads, setDownloads] = useState<DownloadFile[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [deleting, setDeleting] = useState<string | null>(null);
|
||||
|
||||
const fetchDownloads = async () => {
|
||||
try {
|
||||
setError(null);
|
||||
const files = await getAvailableDownloads(datasetId);
|
||||
setDownloads(files);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to load downloads');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchDownloads();
|
||||
}, [datasetId]);
|
||||
|
||||
const handleDelete = async (fileName: string) => {
|
||||
if (!confirm(`Are you sure you want to delete "${fileName}"?`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setDeleting(fileName);
|
||||
try {
|
||||
await deleteDownload(fileName);
|
||||
setDownloads(downloads.filter(d => d.name !== fileName));
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to delete download');
|
||||
} finally {
|
||||
setDeleting(null);
|
||||
}
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return <div className="text-gray-500 text-sm">Loading available downloads...</div>;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <div className="text-red-500 text-sm">Error: {error}</div>;
|
||||
}
|
||||
|
||||
if (downloads.length === 0) {
|
||||
return <div className="text-gray-500 text-sm">No downloads available for this dataset</div>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="bg-white rounded-lg shadow p-6">
|
||||
<h2 className="text-lg font-semibold mb-4">Available Downloads</h2>
|
||||
<div className="space-y-2">
|
||||
{downloads.map((download) => (
|
||||
<div 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="ml-4 flex gap-2">
|
||||
<a
|
||||
href={download.url}
|
||||
download
|
||||
className="px-3 py-1 bg-blue-500 hover:bg-blue-600 text-white text-sm rounded transition-colors whitespace-nowrap"
|
||||
>
|
||||
Download
|
||||
</a>
|
||||
<button
|
||||
onClick={() => handleDelete(download.name)}
|
||||
disabled={deleting === download.name}
|
||||
className="px-3 py-1 bg-red-500 hover:bg-red-600 disabled:bg-gray-400 text-white text-sm rounded transition-colors whitespace-nowrap"
|
||||
>
|
||||
{deleting === download.name ? 'Deleting...' : 'Delete'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
106
src/components/DatasetDownloadsModal.tsx
Normal file
106
src/components/DatasetDownloadsModal.tsx
Normal file
@@ -0,0 +1,106 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import Modal from './Modal';
|
||||
import { getAvailableDownloads, deleteDownload } from '@/app/actions/get-available-downloads';
|
||||
|
||||
interface DownloadFile {
|
||||
name: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
interface DatasetDownloadsModalProps {
|
||||
datasetId: number;
|
||||
}
|
||||
|
||||
export default function DatasetDownloadsModal({ datasetId }: DatasetDownloadsModalProps) {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [downloads, setDownloads] = useState<DownloadFile[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [deleting, setDeleting] = useState<string | null>(null);
|
||||
|
||||
const fetchDownloads = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const files = await getAvailableDownloads(datasetId);
|
||||
setDownloads(files);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to load downloads');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleOpenModal = () => {
|
||||
setIsOpen(true);
|
||||
if (downloads.length === 0) {
|
||||
fetchDownloads();
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (fileName: string) => {
|
||||
if (!confirm(`Are you sure you want to delete "${fileName}"?`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setDeleting(fileName);
|
||||
try {
|
||||
await deleteDownload(fileName);
|
||||
setDownloads(downloads.filter(d => d.name !== fileName));
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Failed to delete download');
|
||||
} finally {
|
||||
setDeleting(null);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
onClick={handleOpenModal}
|
||||
className="px-4 py-2 bg-green-500 hover:bg-green-600 text-white rounded transition-colors"
|
||||
>
|
||||
View Downloads
|
||||
</button>
|
||||
|
||||
<Modal isOpen={isOpen} onClose={() => setIsOpen(false)} title="Available Downloads">
|
||||
{loading ? (
|
||||
<div className="text-gray-500 text-sm">Loading available downloads...</div>
|
||||
) : error ? (
|
||||
<div className="text-red-500 text-sm">Error: {error}</div>
|
||||
) : downloads.length === 0 ? (
|
||||
<div className="text-gray-500 text-sm">No downloads available for this dataset</div>
|
||||
) : (
|
||||
<div className="space-y-2 max-h-96 overflow-y-auto">
|
||||
{downloads.map((download) => (
|
||||
<div
|
||||
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="ml-4 flex gap-2">
|
||||
<a
|
||||
href={download.url}
|
||||
download
|
||||
className="px-3 py-1 bg-blue-500 hover:bg-blue-600 text-white text-sm rounded transition-colors whitespace-nowrap"
|
||||
>
|
||||
Download
|
||||
</a>
|
||||
<button
|
||||
onClick={() => handleDelete(download.name)}
|
||||
disabled={deleting === download.name}
|
||||
className="px-3 py-1 bg-red-500 hover:bg-red-600 disabled:bg-gray-400 text-white text-sm rounded transition-colors whitespace-nowrap"
|
||||
>
|
||||
{deleting === download.name ? 'Deleting...' : 'Delete'}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user