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
|
import { NextRequest, NextResponse } from 'next/server';
import fs from 'fs/promises';
import path from 'path';
import db from "@/db/db";
import { basicContractTemplates } from '@/db/schema';
import { eq } from 'drizzle-orm';
export async function POST(request: NextRequest) {
try {
const { templateId } = await request.json();
if (!templateId) {
return NextResponse.json({ error: '템플릿 ID가 누락되었습니다.' }, { status: 400 });
}
// 데이터베이스에서 템플릿 정보 가져오기
const template = await db.query.basicContractTemplates.findFirst({
where: eq(basicContractTemplates.id, templateId)
});
if (!template) {
return NextResponse.json({ error: '템플릿을 찾을 수 없습니다.' }, { status: 404 });
}
if (!template.filePath) {
return NextResponse.json({ error: '템플릿 파일 경로가 없습니다.' }, { status: 404 });
}
// 파일 시스템에서 파일 읽기
const fullPath = path.join(process.cwd(), "public", template.filePath);
try {
const fileBuffer = await fs.readFile(fullPath);
// 파일을 Blob으로 반환
return new NextResponse(fileBuffer, {
status: 200,
headers: {
'Content-Type': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'Content-Disposition': `attachment; filename="${encodeURIComponent(template.fileName || 'template.docx')}"`,
},
});
} catch (fileError) {
console.error('파일 읽기 오류:', fileError);
return NextResponse.json({ error: '템플릿 파일을 읽을 수 없습니다.' }, { status: 500 });
}
} catch (error) {
console.error('템플릿 가져오기 오류:', error);
return NextResponse.json({ error: '서버 오류가 발생했습니다.' }, { status: 500 });
}
}
|