(request: NextRequest)
| 21 | }); |
| 22 | |
| 23 | export async function POST(request: NextRequest) { |
| 24 | // Logging the start of the upload process |
| 25 | console.log(`Upload API call started`); |
| 26 | |
| 27 | // Retrieving the file from the form data |
| 28 | const data = await request.formData(); |
| 29 | const file: File | null = data.get('file') as unknown as File; |
| 30 | |
| 31 | // Check if a file was provided in the request |
| 32 | if (!file) { |
| 33 | console.log('No file found in the request'); |
| 34 | return NextResponse.json({ success: false }); |
| 35 | } |
| 36 | |
| 37 | // Convert file to buffer and write to a temporary location |
| 38 | const bytes = await file.arrayBuffer(); |
| 39 | const buffer = Buffer.from(bytes); |
| 40 | const path = `/tmp/${file.name}`; |
| 41 | await writeFile(path, buffer); |
| 42 | console.log(`File written to ${path}`); |
| 43 | |
| 44 | try { |
| 45 | // Uploading the file to OpenAI |
| 46 | console.log('Starting file upload to OpenAI'); |
| 47 | const fileForRetrieval = await openai.files.create({ |
| 48 | file: createReadStream(path), |
| 49 | purpose: "assistants", |
| 50 | }); |
| 51 | console.log(`File uploaded, ID: ${fileForRetrieval.id}`); |
| 52 | |
| 53 | // Respond with the file ID |
| 54 | return NextResponse.json({ success: true, fileId: fileForRetrieval.id }); |
| 55 | } catch (error) { |
| 56 | // Log and respond to any errors during the upload process |
| 57 | console.error('Error uploading file:', error); |
| 58 | return NextResponse.json({ success: false, message: 'Error uploading file' }); |
| 59 | } |
| 60 | } |
nothing calls this directly
no outgoing calls
no test coverage detected