File size: 1,073 Bytes
ad31128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 }
    )
  }
}