express-busboy icon indicating copy to clipboard operation
express-busboy copied to clipboard

Setting to delete files after successful upload

Open sylvan-d-ash opened this issue 8 years ago • 6 comments

So after the file (or image) has been uploaded to the server and the server has done whatever it needs to do with it (e.g. save to database or store in AWS, etc), the NodeJS server should delete the files from the tmp directory (or wherever they were saved)

sylvan-d-ash avatar Feb 24 '17 20:02 sylvan-d-ash

Yes, I have no way of really knowing that you have acted on the file and done what you wanted. I guess I could add a method that cleans the files up but you would still need to call it when your process is done.

davglass avatar Feb 27 '17 14:02 davglass

Didn't mean to close it :)

davglass avatar Feb 27 '17 14:02 davglass

That's OK. And a method that could be called to delete the files after processing is done is not a bad idea... I find having to use cronjobs a bit unsatisfactory

sylvan-d-ash avatar Mar 08 '17 15:03 sylvan-d-ash

It created in my public directory some unwanted folders

like

'3d6c3049-839b-40ce-9aa3-b76f08bf140b' -> file -> myfile

I am not sure how to remove this.

ashjha1 avatar Sep 25 '17 07:09 ashjha1

I suppose a file could be sent to any post route in my application and it would then remain in the tmp directory eternally. The best behaviour from my point of view would be to cleanup after the request by default. When I want to keep the file beyond the request I'd move it to a safe location.

Maybe can do it with middleware (pseudocode). Finish is triggered after success, close is triggered on failure.

module.exports = (req, res, next) => {
    const files = req.files || {};

    function cleanupFiles () {
        res.removeListener('finish', cleanupFiles);
        res.removeListener('close', cleanupFiles);

        // delete files/dirs
    }

    res.on('finish', cleanupFiles);
    res.on('close', cleanupFiles);

    next();
};

Kequc avatar Feb 07 '18 12:02 Kequc

If you go with the default clean up, please allow an option to prevent it.

cleanup: false

Thanks,

danosaure avatar Jun 19 '18 00:06 danosaure