-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cloudinary support for uploads (#48)
Co-authored-by: moonlitgrace <[email protected]>
- Loading branch information
1 parent
a59f486
commit f171e5e
Showing
10 changed files
with
226 additions
and
804 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { NextResponse } from 'next/server'; | ||
import cloudinary from '@/lib/cloudinary'; | ||
import { validateFile } from '@/lib/utils'; | ||
|
||
export async function POST(req: Request) { | ||
try { | ||
const formData = await req.formData(); | ||
const file = formData.get('file') as File; | ||
|
||
if (!validateFile(file)) { | ||
return NextResponse.json({ message: 'No file provided' }, { status: 400 }); | ||
} | ||
|
||
const buffer = await file.arrayBuffer(); | ||
const base64String = Buffer.from(buffer).toString('base64'); | ||
const base64Image = `data:${file.type};base64,${base64String}`; | ||
|
||
const uploadRes = await cloudinary.uploader.upload(base64Image); | ||
|
||
return NextResponse.json({ url: uploadRes.url }); | ||
} catch (error) { | ||
if (error instanceof Error) | ||
return NextResponse.json({ message: error.message }, { 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { v2 as cloudinary } from 'cloudinary'; | ||
|
||
cloudinary.config({ | ||
cloud_name: process.env.CLOUDINARY_CLOUD_NAME, | ||
api_key: process.env.CLOUDINARY_API_KEY, | ||
api_secret: process.env.CLOUDINARY_API_SECRET, | ||
secure: true, | ||
}); | ||
|
||
export default cloudinary; |
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
Oops, something went wrong.