easy-express-cwa
Version:
CLI tool to setup a common Express.js backend developed by codewithashim
48 lines (41 loc) • 1.07 kB
text/typescript
import { v2 as cloudinary } from "cloudinary";
import fs from "fs";
import config from "../../../config";
cloudinary.config({
cloud_name: config.cloudinary.cloud_name,
api_key: config.cloudinary.api_key,
api_secret: config.cloudinary.api_secret,
});
interface UploadApiResponse {
public_id: string;
version: number;
signature: string;
width: number;
height: number;
format: string;
resource_type: string;
created_at: string;
tags: string[];
bytes: number;
type: string;
etag: string;
placeholder: boolean;
url: string;
secure_url: string;
original_filename: string;
[key: string]: any;
}
const uploadOnCloudinary = async (localFilePath: string): Promise<UploadApiResponse | null> => {
try {
if (!localFilePath) return null;
const response: UploadApiResponse = await cloudinary.uploader.upload(localFilePath, {
resource_type: "auto",
});
fs.unlinkSync(localFilePath);
return response;
} catch (error) {
fs.unlinkSync(localFilePath);
return null;
}
};
export { uploadOnCloudinary };