Skip to content

Commit

Permalink
feat: add an image upload endpoint for the Import extension demo
Browse files Browse the repository at this point in the history
  • Loading branch information
nperez0111 committed Dec 20, 2024
1 parent 82359fa commit 7d9c3e7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/app/api/upload-image/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { NextResponse } from 'next/server'

/**
* This function is called as the `imageUploadCallback` for the `Import` extension.
* This in no way is a good implementation as it will store the image within the document, bloating the document size.
* If implementing this in a real-world scenario, you should upload the image to a cloud storage service and return the URL to the image.
* It also is not even checking the proper mime type of the file, which is a security risk.
*/
export async function POST(request: Request) {
const formData = await request.formData()
const file = formData.get('file') as File

if (!file) {
return NextResponse.json({ error: 'No file provided' }, { status: 400 })
}

const arrayBuffer = await file.arrayBuffer()
const base64String = Buffer.from(arrayBuffer).toString('base64')
const mimeType = file.type
const base64Url = `data:${mimeType};base64,${base64String}`

return NextResponse.json({ url: base64Url }, { status: 200 })
}

0 comments on commit 7d9c3e7

Please sign in to comment.