Quantumbraid's picture
download
raw
1.14 kB
import { NextResponse } from 'next/server';
import fs from 'fs';
import { getDatasetsRoot } from '@/server/settings';
export async function POST(request: Request) {
try {
const body = await request.json();
const { imgPath, caption, ext } = 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
try {
await fs.promises.access(imgPath);
} catch {
return NextResponse.json({ error: 'Image does not exist' }, { status: 404 });
}
// check for caption (default extension txt)
const captionExt = ((ext || 'txt') as string).replace(/^\.+/, '').trim() || 'txt';
const captionPath = imgPath.replace(/\.[^/.]+$/, '') + '.' + captionExt;
// save caption to file
await fs.promises.writeFile(captionPath, caption);
return NextResponse.json({ success: true });
} catch (error) {
return NextResponse.json({ error: 'Failed to create dataset' }, { status: 500 });
}
}

Xet Storage Details

Size:
1.14 kB
·
Xet hash:
20d1cf6036f7850f86253fe7f9656b9c0872142788311b8be9aa03b72a7456a4

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.