-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated to BunnyCDN for image upload (#337)
* Migrated to BunnyCDN for image upload * Reverted package.json * Refactored env for CDN * Added docs for image cdn domain config * Fixed lint
- Loading branch information
1 parent
90e9ee4
commit 167def8
Showing
8 changed files
with
77 additions
and
75 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
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
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
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
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
This file was deleted.
Oops, something went wrong.
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,49 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
const CDN_BASE_UPLOAD_URL = process.env.CDN_BASE_UPLOAD_URL; | ||
const CDN_BASE_ACCESS_URL = process.env.CDN_BASE_ACCESS_URL; | ||
const CDN_API_KEY = process.env.CDN_API_KEY!; | ||
|
||
export async function POST(req: Request): Promise<NextResponse> { | ||
try { | ||
const formData = await req.formData(); | ||
const file = formData.get('file') as File; | ||
const uniqueFileName = formData.get('uniqueFileName') || uuidv4(); // Generate unique key if not provided | ||
|
||
if (!file) { | ||
return NextResponse.json({ error: 'File is required' }, { status: 400 }); | ||
} | ||
|
||
const uploadUrl = `${CDN_BASE_UPLOAD_URL}/${uniqueFileName}`; | ||
|
||
const fileBuffer = Buffer.from(await file.arrayBuffer()); | ||
|
||
const response = await fetch(uploadUrl, { | ||
method: 'PUT', | ||
headers: { | ||
AccessKey: CDN_API_KEY, | ||
'Content-Type': 'application/octet-stream', | ||
}, | ||
body: fileBuffer, | ||
}); | ||
|
||
if (response.ok) { | ||
return NextResponse.json({ | ||
message: 'File uploaded successfully', | ||
url: `${CDN_BASE_ACCESS_URL}/${uniqueFileName}`, | ||
}); | ||
} else { | ||
return NextResponse.json( | ||
{ error: 'Failed to upload file' }, | ||
{ status: response.status } | ||
); | ||
} | ||
} catch (error) { | ||
console.error('Error uploading file to BunnyCDN:', error); | ||
return NextResponse.json( | ||
{ error: 'Internal server error' }, | ||
{ status: 500 } | ||
); | ||
} | ||
} |
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