-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an image upload endpoint for the
Import
extension demo
- Loading branch information
1 parent
82359fa
commit 7d9c3e7
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} |