summaryrefslogtreecommitdiff
path: root/app/api/upload/purchase-request/route.ts
blob: 8e49afcdc4b5192aaa029d3cc2b57cf421c23f06 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
// app/api/upload/purchase-request/route.ts
import { NextRequest, NextResponse } from "next/server";
import { saveDRMFile } from "@/lib/file-stroage";
import { getServerSession } from 'next-auth/next'
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
import { decryptWithServerAction } from '@/components/drm/drmUtils'

export async function POST(request: NextRequest) {
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user?.id) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    const formData = await request.formData();
    const files = formData.getAll("files") as File[];
    
    if (!files || files.length === 0) {
      return NextResponse.json({ error: "No files provided" }, { status: 400 });
    }

    const uploadResults = [];
    
    for (const file of files) {
      const result = await saveDRMFile(
        file,
        decryptWithServerAction,
        "purchase-requests",
        session.user.id,
      );

      if (!result.success) {
        return NextResponse.json(
          { error: result.error || "파일 업로드 실패" },
          { status: 400 }
        );
      }

      uploadResults.push({
        fileName: result.fileName!,
        originalFileName: file.name,
        filePath: result.publicPath!,
        fileSize: result.fileSize!,
        fileType: file.type,
      });
    }

    return NextResponse.json({ files: uploadResults });
  } catch (error) {
    console.error("Upload error:", error);
    return NextResponse.json(
      { error: "파일 업로드 중 오류가 발생했습니다" },
      { status: 500 }
    );
  }
}