Untitled

 avatar
Anis
plain_text
a month ago
1.0 kB
3
Indexable
Never
import crypto from "crypto";
import axios from 'axios';

const generateSHA1 =(data: any) => {
    const hash = crypto.createHash("sha1");
    hash.update(data);
    return hash.digest("hex");
}

const generateSignature = (publicId: string, apiSecret: string) => {
	const timestamp = new Date().getTime();
	return `public_id=${publicId}&timestamp=${timestamp}${apiSecret}`;
};

const handleDeleteImage = async (publicId) => {
  const cloudName = 'your_cloud_name';
  const timestamp = new Date().getTime();
  const apiKey = 'your_api_key';
  const apiSecret = 'your_api_secret'
  const signature = generateSHA1(generateSignature(publicId, apiSecret));
  const url = `https://api.cloudinary.com/v1_1/${cloudName}/image/destroy`;

  try {
    const response = await axios.post(url, {
      public_id: publicId,
      signature: signature,
      api_key: apiKey,
      timestamp: timestamp,
    });

    console.error(response);

  } catch (error) {
    console.error(error);
  }
};
Leave a Comment