summaryrefslogtreecommitdiff
path: root/app/api/contracts/get-template/route.ts
blob: b7965481dfab3e80835846ed6cbb270fbcb6d9e0 (plain)
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
import { NextRequest, NextResponse } from "next/server";
import { readFile } from "fs/promises";
import path from "path";

export async function POST(request: NextRequest) {
  try {
    const { templatePath } = await request.json();

    if (!templatePath) {
      return NextResponse.json(
        { error: "템플릿 경로가 필요합니다." },
        { status: 400 }
      );
    }

    // /api/files로 시작하는 경우 제거
    const cleanPath = templatePath.startsWith('/api/files') 
      ? templatePath.slice('/api/files'.length) 
      : templatePath;

    const isDev = process.env.NODE_ENV === 'development';

    const fullPath = isDev
      ? path.join(process.cwd(), `public`, cleanPath)
      : path.join(`${process.env.NAS_PATH}`, cleanPath);

    const fileBuffer = await readFile(fullPath);

    return new NextResponse(fileBuffer, {
      headers: {
        'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'Content-Disposition': `attachment; filename="template.docx"`
      }
    });

  } catch (error) {
    console.error("템플릿 파일 읽기 실패:", error);
    return NextResponse.json(
      { error: "템플릿 파일을 읽을 수 없습니다." },
      { status: 500 }
    );
  }
}