I am trying to delete the file object that is created and stored in the uploads folder
.
Here is my config options for multer:
const path = require('path')
multerConfigOpt = {
dest: 'uploads/',
fileFilter: function(req, file, cb) {
var ext = path.extname(file.originalname)
console.log(ext);
if (ext === '.pdf' || ext === '.doc' || ext === '.docx' || ext === '.txt') {
console.log("true!");
cb(null,true)
} else {
cb(null, false)
}
}
}
module.exports = multerConfigOpt
After processing the files that was submitted by a user via a HTML Form I would like to delete it. When I try and do the following:
await fs.unlink(this.file.path, (err) => {
if (err) {
console.log(err);
}
})
However I get the following error:
Error: EPERM: operation not permitted,
these files that are store in the uploads folder do not have an extension. I believe they are just binary files that were created by multer.
Below is an example of the file that is in the uploads folder path.
All I do with the file is send it as an attachment through email using nodemailer
if (this.file && this.isJobSeeker) {
mailOptions.attachments = [{
filename: 'resume.pdf',
content: fs.createReadStream(this.file.path)
}]
}
How can I delete these files?
Thank you for the help.
this.file.path
is? Seems like folder rather than file. And hence theEPERM
error. Orfs.unlink
couldn't find this file. – ambianBeing