mirror of
https://github.com/ostris/ai-toolkit.git
synced 2026-03-12 22:19:48 +00:00
Handle image deletion
This commit is contained in:
34
ui/src/app/api/img/delete/route.ts
Normal file
34
ui/src/app/api/img/delete/route.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import fs from 'fs';
|
||||
import { getDatasetsRoot } from '@/app/api/datasets/utils';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { imgPath } = body;
|
||||
let datasetsPath = await getDatasetsRoot();
|
||||
// make sure the dataset path is in the image path
|
||||
if (!imgPath.startsWith(datasetsPath)) {
|
||||
return NextResponse.json({ error: 'Invalid image path' }, { status: 400 });
|
||||
}
|
||||
|
||||
// if img doesnt exist, ignore
|
||||
if (!fs.existsSync(imgPath)) {
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
|
||||
// delete it and return success
|
||||
fs.unlinkSync(imgPath);
|
||||
|
||||
// check for caption
|
||||
const captionPath = imgPath.replace(/\.[^/.]+$/, '') + '.txt';
|
||||
if (fs.existsSync(captionPath)) {
|
||||
// delete caption file
|
||||
fs.unlinkSync(captionPath);
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
return NextResponse.json({ error: 'Failed to create dataset' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,12 @@ export default function DatasetPage({ params }: { params: { datasetName: string
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
|
||||
{imgList.length === 0 && <p>No images found</p>}
|
||||
{imgList.map(img => (
|
||||
<DatasetImageCard key={img.img_path} alt="image" imageUrl={img.img_path} />
|
||||
<DatasetImageCard
|
||||
key={img.img_path}
|
||||
alt="image"
|
||||
imageUrl={img.img_path}
|
||||
onDelete={() => refreshImageList(datasetName)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user