File size: 1,680 Bytes
250ae22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
// app/api/file/upload/route.ts
import { cookies } from 'next/headers';
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';

export async function POST(request: NextRequest) {
  try {
    // Get FormData from request
    const formData = await request.formData();
    
    // Get auth token from cookies
    const cookieStore = await cookies();
    const token = cookieStore.get('auth_token')?.value;

    if (!token) {
      return NextResponse.json(
        { message: 'Not authenticated' },
        { status: 401 }
      );
    }

    // Forward the FormData to external API
    const response = await fetch(
      'https://byteriot-candidateexplorer.hf.space/CandidateExplorer/file/upload',
      {
        method: 'POST',
        headers: {
          Authorization: `Bearer ${token}`,
          // Don't set Content-Type, let fetch handle it for FormData
        },
        body: formData, // Pass FormData directly
      }
    );

    if (!response.ok) {
      const errorData = await response.json().catch(() => null);
      return NextResponse.json(
        { message: errorData?.message || 'Upload failed', error: errorData },
        { status: response.status }
      );
    }

    // Get response data from external API
    const data = await response.json();

    return NextResponse.json(
      { 
        success: true, 
        message: 'Files uploaded successfully',
        data: data 
      }, 
      { status: 200 }
    );

  } catch (error) {
    console.error('Upload CV error:', error);
    return NextResponse.json(
      { message: 'Failed to upload files', error: String(error) },
      { status: 500 }
    );
  }
}