payload-cloudinary
Version:
A Cloudinary storage plugin for Payload CMS
39 lines • 1.71 kB
JavaScript
import path from "path";
export const getHandleDelete = ({ cloudinary, folder }) => async ({ filename, doc }) => {
const filePath = path.posix.join(folder, filename);
const docWithCloudinary = doc;
try {
// Extract public_id without file extension
let publicId = filePath.replace(/\.[^/.]+$/, "");
// Try to get metadata from doc if available (from Cloudinary plugin)
let resourceType = "image";
let deliveryType = "upload";
if (docWithCloudinary.cloudinary) {
// Use stored Cloudinary metadata if available
publicId = docWithCloudinary.cloudinary.public_id || publicId;
resourceType =
docWithCloudinary.cloudinary.resource_type || resourceType;
deliveryType = docWithCloudinary.cloudinary.type || deliveryType;
}
// Attempt deletion with proper parameters
const result = await cloudinary.uploader.destroy(publicId, {
resource_type: resourceType,
type: deliveryType,
invalidate: true, // Invalidate CDN cache
});
// Log the result for debugging
const okResults = new Set(["ok", "not found", "already deleted"]);
if (!okResults.has(result?.result)) {
console.warn(`Cloudinary destroy returned unexpected result for ${publicId}:`, result);
}
else {
console.log(`Successfully deleted ${publicId} from Cloudinary`);
}
}
catch (error) {
console.error("Error deleting file from Cloudinary:", error);
// Don't throw - allow the CMS deletion to proceed
// throw error
}
};
//# sourceMappingURL=handleDelete.js.map