ai-clip-factory / src /app /server /utils /deleteFileIfExists.ts
jbilcke-hf's picture
jbilcke-hf HF staff
initial commit
9a42933
raw
history blame contribute delete
497 Bytes
import { existsSync, promises as fs } from "node:fs"
export const deleteFileIfExists = async (filePath: string) => {
const safePath = filePath.trim()
// just a sanity check
if (safePath.includes("*") ||safePath === "/" || safePath === "~" || safePath === ".") {
throw new Error(`lol, no.`)
}
if (existsSync(filePath)) {
try {
await fs.unlink(safePath)
return true
} catch (err) {
console.log(`failed to delete file ${safePath}`)
}
}
return false
}