Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,977 Bytes
8b1baa1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import { NextResponse } from 'next/server';
import fs from 'fs';
import path from 'path';
import { getDatasetsRoot } from '@/server/settings';
const sanitizeSegment = (value: string): string => {
if (!value || typeof value !== 'string') {
return '';
}
return value
.toLowerCase()
.replace(/[^a-z0-9]+/g, '_')
.replace(/^_+|_+$/g, '');
};
export async function POST(request: Request) {
try {
const body = await request.json();
const { currentName, newName } = body ?? {};
if (!currentName || typeof currentName !== 'string') {
return NextResponse.json({ error: 'Current dataset name is required' }, { status: 400 });
}
if (!newName || typeof newName !== 'string') {
return NextResponse.json({ error: 'New dataset name is required' }, { status: 400 });
}
const datasetsRoot = await getDatasetsRoot();
const sanitizedCurrent = sanitizeSegment(currentName);
const currentPath = path.join(datasetsRoot, sanitizedCurrent);
if (!fs.existsSync(currentPath)) {
return NextResponse.json({ error: `Dataset '${currentName}' not found` }, { status: 404 });
}
const sanitizedNew = sanitizeSegment(newName);
if (!sanitizedNew) {
return NextResponse.json({ error: 'New dataset name is invalid' }, { status: 400 });
}
if (sanitizedCurrent === sanitizedNew) {
return NextResponse.json({ success: true, name: sanitizedCurrent, path: currentPath });
}
const newPath = path.join(datasetsRoot, sanitizedNew);
if (fs.existsSync(newPath)) {
return NextResponse.json({ error: `Dataset '${sanitizedNew}' already exists` }, { status: 409 });
}
fs.renameSync(currentPath, newPath);
return NextResponse.json({ success: true, name: sanitizedNew, path: newPath });
} catch (error: any) {
console.error('Dataset rename error:', error);
return NextResponse.json({ error: error?.message || 'Failed to rename dataset' }, { status: 500 });
}
}
|