"use strict";Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const fs_1=require("fs"),path_1=require("path"),utils_1=require("../utils"),validation_1=require("@b/utils/validation"),error_1=require("@b/utils/error");exports.metadata={summary:"Deletes an image file by id",operationId:"deleteImageFile",tags:["Admin","Content","Media"],parameters:[{index:0,name:"id",in:"path",required:!0,description:"The relative id of the image file to delete",schema:{type:"string"}}],responses:{200:{description:"Image file deleted successfully",content:{"application/json":{schema:{type:"object",properties:{message:{type:"string"}}}}}},400:{description:"Bad request if the id is not specified"},404:{description:"Not found if the image file does not exist"},500:{description:"Internal server error"}},requiresAuth:!0,permission:"delete.content.media",logModule:"ADMIN_CMS",logTitle:"Delete media file"};exports.default=async e=>{const{params:i,ctx:t}=e,r=i.id;null==t||t.step("Validating image ID");if(!r){null==t||t.fail("Image ID is required");throw(0,error_1.createError)({statusCode:400,message:"Image id is required"})}null==t||t.step("Sanitizing file path");const s=(0,validation_1.sanitizePath)(r.replace(/_/g,path_1.sep)),a=(0,path_1.join)(utils_1.publicDirectory,s);try{null==t||t.step("Deleting image file");await fs_1.promises.unlink(a);(0,utils_1.filterMediaCache)(s);null==t||t.success("Image file deleted successfully");return{message:"Image file deleted successfully"}}catch(e){if("ENOENT"===e.code){null==t||t.fail("Image file not found");throw(0,error_1.createError)({statusCode:404,message:"Image file not found"})}if("EBUSY"===e.code){null==t||t.fail("File is busy or locked");throw(0,error_1.createError)({statusCode:409,message:"File is busy or locked"})}null==t||t.fail("Failed to delete image file");throw(0,error_1.createError)({statusCode:500,message:"Failed to delete image file"})}};