-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Cloudinary integration (#1071)
* cloudinary integration wip * cloudinary integration wip * fetch & proxy * fix lock * remove lib due to wasm nonsense * clean up * Update apps/web/src/components/ImageCloudinary/index.tsx Co-authored-by: Jordan Frankfurt <[email protected]> * Update apps/web/src/components/ImageCloudinary/index.tsx Co-authored-by: Jordan Frankfurt <[email protected]> * add logger to the routes * env example * fix loading --------- Co-authored-by: Jordan Frankfurt <[email protected]>
- Loading branch information
1 parent
46005fd
commit 9a52003
Showing
14 changed files
with
328 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { v2 as cloudinary } from 'cloudinary'; | ||
import { createHash } from 'crypto'; | ||
import { logger } from 'apps/web/src/utils/logger'; | ||
|
||
// Configure Cloudinary | ||
cloudinary.config({ | ||
cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME, | ||
api_key: process.env.CLOUDINARY_API_KEY, | ||
api_secret: process.env.CLOUDINARY_API_SECRET, | ||
}); | ||
|
||
const folderName = 'base-org-uploads'; | ||
|
||
export type CloudinaryMediaUrlRequest = { | ||
media: string; | ||
width: number; | ||
}; | ||
|
||
export type CloudinaryMediaUrlResponse = { | ||
url: string; | ||
}; | ||
|
||
function generateAssetId(media: string): string { | ||
return createHash('md5').update(media).digest('hex'); | ||
} | ||
|
||
async function checkAssetExists(assetId: string): Promise<string | false> { | ||
try { | ||
const response = await cloudinary.api.resources_by_ids([`${folderName}/${assetId}`]); | ||
if (response && response.resources.length > 0) { | ||
const image = response.resources[0]; | ||
return image.secure_url; | ||
} else { | ||
return false; | ||
} | ||
} catch (error) { | ||
// For other errors, log and assume the asset doesn't exist | ||
logger.error('Error checking if asset exists in Cloudinary', error, { assetId }); | ||
return false; | ||
} | ||
} | ||
|
||
async function uploadToCloudinary(media: string, width: number) { | ||
try { | ||
const assetId = generateAssetId(media); | ||
|
||
// Otherwise upload it to group | ||
const result = await cloudinary.uploader.upload(media, { | ||
public_id: assetId, | ||
folder: folderName, | ||
format: 'webp', | ||
transformation: { | ||
width: width, | ||
}, | ||
}); | ||
|
||
return result; | ||
} catch (error) { | ||
logger.error('Failed to upload asset', error, { media }); | ||
return false; | ||
} | ||
} | ||
|
||
async function getCloudinaryMediaUrl({ | ||
media, | ||
width, | ||
}: CloudinaryMediaUrlRequest): Promise<string | false> { | ||
// Asset idea based on URL | ||
const assetId = generateAssetId(media); | ||
|
||
// Return the asset if already uploaded | ||
const existingAssetUrl = await checkAssetExists(assetId); | ||
if (existingAssetUrl) { | ||
return existingAssetUrl; | ||
} | ||
|
||
const cloudinaryUpload = await uploadToCloudinary(media, width); | ||
|
||
if (cloudinaryUpload) { | ||
return cloudinaryUpload.secure_url; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export async function POST(request: NextRequest) { | ||
try { | ||
const body = (await request.json()) as CloudinaryMediaUrlRequest; | ||
|
||
const { media, width } = body; | ||
|
||
if (!media || !width) { | ||
return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 }); | ||
} | ||
|
||
const cloudinaryUrl = await getCloudinaryMediaUrl({ media, width }); | ||
if (cloudinaryUrl) { | ||
const response: CloudinaryMediaUrlResponse = { | ||
url: cloudinaryUrl, | ||
}; | ||
return NextResponse.json(response); | ||
} else { | ||
return NextResponse.json({ error: 'Failed to upload Cloudinary URL' }, { status: 500 }); | ||
} | ||
} catch (error) { | ||
logger.error('Error processing Cloudinary URL:', error); | ||
return NextResponse.json({ error: 'Failed to process Cloudinary URL' }, { status: 500 }); | ||
} | ||
} |
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
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
Oops, something went wrong.