Created
March 14, 2023 14:00
-
-
Save Avi-E-Koenig/4299c565396c5e35c6bf1ee4cf31c7e1 to your computer and use it in GitHub Desktop.
basic cloudinary crud
This file contains hidden or 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
import cloudinary from 'cloudinary'; | |
import fs from 'fs'; | |
import { CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET, CLOUDINARY_CLOUD_NAME } from '@/config'; | |
cloudinary.v2.config({ | |
cloud_name: CLOUDINARY_CLOUD_NAME, | |
api_key: CLOUDINARY_API_KEY, | |
api_secret: CLOUDINARY_API_SECRET, | |
}); | |
type ResultImageData = { | |
public_id: string; | |
secure_url: string; | |
}; | |
const uploadImage = async (filePath: string): Promise<ResultImageData> => { | |
const result = await cloudinary.v2.uploader.upload(filePath); | |
fs.unlinkSync(filePath); | |
return result; | |
}; | |
const updateImage = async (filePath: string, public_id: string): Promise<ResultImageData> => { | |
const result = await cloudinary.v2.uploader.upload(filePath, { public_id }); | |
fs.unlinkSync(filePath); | |
return result; | |
}; | |
const deleteImage = async (public_id: string) => { | |
await cloudinary.v2.uploader.destroy(public_id); | |
return true; | |
}; | |
export default { | |
uploadImage, | |
updateImage, | |
deleteImage, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment