delete uploaded file

This commit is contained in:
Takeshi Kimata
2024-03-17 20:29:01 +09:00
parent 1825545bf0
commit 52046992f0
2 changed files with 36 additions and 8 deletions

View File

@@ -20,11 +20,10 @@ module.exports = function (sequelize) {
}
// delete file from folder
const uploadDir = path.join(__dirname, "../../public");
const uploadDir = path.join(__dirname, "../../public/uploads");
const url = attachment.path;
const parts = url.split('/');
const fileNameWithFolder = parts.slice(-2).join('/');
const filePath = path.join(uploadDir, fileNameWithFolder);
const fileName = url.substring(url.lastIndexOf("/") + 1);
const filePath = path.join(uploadDir, fileName);
fs.unlink(filePath, (err) => {
if (err) {
console.error('Error deleting file:', err);