Download ui/src/app/api/img/delete/route.ts from michaelu0826/ai_toolkit_code: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/datasets/michaelu0826/ai_toolkit_code/resolve/main/ui/src/app/api/img/delete/route.ts
- Command line
-
hf download hf://datasets/michaelu0826/ai_toolkit_code/ui/src/app/api/img/delete/route.ts
-
curl -L -o route.ts https://huggingface.co/datasets/michaelu0826/ai_toolkit_code/resolve/main/ui/src/app/api/img/delete/route.ts
1.36 kB
| import { NextResponse } from 'next/server'; | |
| import fs from 'fs'; | |
| import { getDatasetsRoot, getTrainingFolder } from '@/server/settings'; | |
| export async function POST(request: Request) { | |
| try { | |
| const body = await request.json(); | |
| const { imgPath } = body; | |
| let datasetsPath = await getDatasetsRoot(); | |
| const trainingPath = await getTrainingFolder(); | |
| // make sure the dataset path is in the image path | |
| if (!imgPath.startsWith(datasetsPath) && !imgPath.startsWith(trainingPath)) { | |
| return NextResponse.json({ error: 'Invalid image path' }, { status: 400 }); | |
| } | |
| // make sure it is an image | |
| if (!/\.(jpg|jpeg|png|bmp|gif|tiff|webp|mp4|mp3|wav|flac|ogg)$/i.test(imgPath.toLowerCase())) { | |
| return NextResponse.json({ error: 'Not an image' }, { 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 }); | |
| } | |
| } | |