|
|
@@ -0,0 +1,56 @@
|
|
|
+import { NextRequest, NextResponse } from 'next/server';
|
|
|
+import { promises as fs } from 'fs';
|
|
|
+import path from 'path';
|
|
|
+import { isPathSafe } from '@/app/lib/novel-server-utils';
|
|
|
+
|
|
|
+export async function GET(request: NextRequest) {
|
|
|
+ const projectPath = request.nextUrl.searchParams.get('projectPath') || '';
|
|
|
+ if (!projectPath || !isPathSafe(projectPath)) {
|
|
|
+ return NextResponse.json({ success: false, error: '无效路径' }, { status: 400 });
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const graphDir = path.join(projectPath, 'event-graph');
|
|
|
+ const [eventsRaw, edgesRaw, allocationRaw] = await Promise.all([
|
|
|
+ fs.readFile(path.join(graphDir, 'events.json'), 'utf-8').catch(() => '[]'),
|
|
|
+ fs.readFile(path.join(graphDir, 'edges.json'), 'utf-8').catch(() => '[]'),
|
|
|
+ fs.readFile(path.join(graphDir, 'chapter-allocation.json'), 'utf-8').catch(() => '[]'),
|
|
|
+ ]);
|
|
|
+ return NextResponse.json({
|
|
|
+ success: true,
|
|
|
+ events: JSON.parse(eventsRaw),
|
|
|
+ edges: JSON.parse(edgesRaw),
|
|
|
+ chapterAllocations: JSON.parse(allocationRaw),
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ return NextResponse.json({ success: false, error: (error as Error).message }, { status: 500 });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export async function POST(request: NextRequest) {
|
|
|
+ const body = await request.json().catch(() => null);
|
|
|
+ if (!body) return NextResponse.json({ success: false, error: '无效请求' }, { status: 400 });
|
|
|
+
|
|
|
+ const { projectPath, events, edges, chapterAllocations } = body;
|
|
|
+ if (!projectPath || !isPathSafe(projectPath)) {
|
|
|
+ return NextResponse.json({ success: false, error: '无效路径' }, { status: 400 });
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const graphDir = path.join(projectPath, 'event-graph');
|
|
|
+ const writes: Promise<void>[] = [];
|
|
|
+ if (events !== undefined) {
|
|
|
+ writes.push(fs.writeFile(path.join(graphDir, 'events.json'), JSON.stringify(events, null, 2), 'utf-8'));
|
|
|
+ }
|
|
|
+ if (edges !== undefined) {
|
|
|
+ writes.push(fs.writeFile(path.join(graphDir, 'edges.json'), JSON.stringify(edges, null, 2), 'utf-8'));
|
|
|
+ }
|
|
|
+ if (chapterAllocations !== undefined) {
|
|
|
+ writes.push(fs.writeFile(path.join(graphDir, 'chapter-allocation.json'), JSON.stringify(chapterAllocations, null, 2), 'utf-8'));
|
|
|
+ }
|
|
|
+ await Promise.all(writes);
|
|
|
+ return NextResponse.json({ success: true });
|
|
|
+ } catch (error) {
|
|
|
+ return NextResponse.json({ success: false, error: (error as Error).message }, { status: 500 });
|
|
|
+ }
|
|
|
+}
|