simple-auth-cli
Version:
An implementation of authentication system supporting multiple providers ready to be used with a single command.
46 lines (41 loc) • 1.29 kB
text/typescript
import { v2 as cloudinary, UploadApiResponse } from "cloudinary";
import fs from "fs";
// Configure Cloudinary with environment variables.
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
const uploadOnCloudinary = async (
localFilePath: string
): Promise<UploadApiResponse | null> => {
try {
if (!localFilePath) return null;
const response = await cloudinary.uploader.upload(localFilePath, {
resource_type: "auto",
});
if (!localFilePath.includes("https://ui-avatars.com/")) {
fs.unlinkSync(localFilePath);
}
return response;
} catch (error) {
if (!localFilePath.includes("https://ui-avatars.com/")) {
fs.unlinkSync(localFilePath);
}
return null;
}
};
const deleteOnCloudinary = async (
filePath: string|undefined
): Promise<boolean | null> => {
try {
if (!filePath) return null;
// Extract the publicId from the filePath.
const publicId = filePath.split('/').pop()?.split('.')[0] || "";
await cloudinary.uploader.destroy(publicId);
return true;
} catch (error) {
return null;
}
};
export { uploadOnCloudinary, deleteOnCloudinary };