Spaces:
Sleeping
Sleeping
| import { NextRequest, NextResponse } from 'next/server' | |
| import fs from 'fs/promises' | |
| import path from 'path' | |
| export async function POST(request: NextRequest) { | |
| try { | |
| const { document, sessionId } = await request.json() | |
| if (!document || !sessionId) { | |
| return NextResponse.json( | |
| { error: 'Document and session ID are required' }, | |
| { status: 400 } | |
| ) | |
| } | |
| // Create session-specific directory for LaTeX documents | |
| const sessionDir = path.join(process.cwd(), 'data', 'sessions', sessionId, 'latex') | |
| // Ensure directory exists | |
| await fs.mkdir(sessionDir, { recursive: true }) | |
| // Save document | |
| const filePath = path.join(sessionDir, `${document.id}.json`) | |
| await fs.writeFile(filePath, JSON.stringify(document, null, 2)) | |
| return NextResponse.json({ | |
| success: true, | |
| documentId: document.id, | |
| sessionId | |
| }) | |
| } catch (error) { | |
| console.error('Error saving LaTeX document:', error) | |
| return NextResponse.json( | |
| { error: 'Failed to save document' }, | |
| { status: 500 } | |
| ) | |
| } | |
| } |