Buckets:
| import { NextResponse } from 'next/server'; | |
| import fs from 'fs'; | |
| import { getDatasetsRoot, getTrainingFolder } from '@/server/settings'; | |
| const fileExists = (p: string) => fs.promises.access(p).then(() => true).catch(() => false); | |
| 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 (!(await fileExists(imgPath))) { | |
| return NextResponse.json({ success: true }); | |
| } | |
| // delete it and return success | |
| await fs.promises.unlink(imgPath); | |
| // check for caption | |
| const captionPath = imgPath.replace(/\.[^/.]+$/, '') + '.txt'; | |
| if (await fileExists(captionPath)) { | |
| // delete caption file | |
| await fs.promises.unlink(captionPath); | |
| } | |
| return NextResponse.json({ success: true }); | |
| } catch (error) { | |
| return NextResponse.json({ error: 'Failed to create dataset' }, { status: 500 }); | |
| } | |
| } | |
Xet Storage Details
- Size:
- 1.48 kB
- Xet hash:
- 1fbc19b3f084f20f04d17e9fc152ee031fda111e1aab9fcb722a72c71196d757
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.