Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper NodeJS code to delete old files from a tmp uploads folder

I use the following code to delete old files from a tmp upload directory:

       fs.readdir( dirPath, function( err, files ) {
            if ( err ) return console.log( err );
            if (files.length > 0) {
                files.forEach(function( file ) {
                    var filePath = dirPath + file;
                    fs.stat( filePath, function( err, stat ) {
                        if ( err ) return console.log( err );
                        var livesUntil = new Date();
                        livesUntil.setHours(livesUntil.getHours() - 1);
                        if ( stat.ctime < livesUntil ) {
                            fs.unlink( filePath, function( err ) {
                                if ( err ) return console.log( err );
                            });
                        }
                    });
                });
            }
        });

I run it once an hour. This works, most of the time. However I am finding that occasionally uploads disappear unexpectedly in the middle of uploading. I think what is happening is that this code gets run while a file is actually in the middle of uploading, before stat.ctime is set - and so it gets deleted before it is finished. Any suggestions on how I can prevent this?

Thanks!

like image 307
user1031947 Avatar asked Nov 10 '22 14:11

user1031947


1 Answers

Why not clean up on each upload?

var fs = require('fs');

var fsio;
(function (fsio) {
    var uploader = (function () {
        function uploader() {
        }
        uploader.prototype.writeFile = function (fn, data, cb) {
            var tempDest = '/tmp/' + fn;

            // .. your write logic ..

            // cleanup
            cb(tempDest);
        };

        uploader.prototype.cleanup = function (fileDest) {

            // .. your cleanup logic - move or copy/del ..

            fs.unlink(fileDest);
        };

        uploader.prototype.upload = function (fn, data) {
            this.writeFile(fn, data, this.cleanup);
        };
        return uploader;
    })();
    fsio.uploader = uploader;
})(fsio || (fsio = {}));

usage:

var fileupload = new fsio.uploader();
fileupload.upload("monkey.gif", []);
like image 171
enko Avatar answered Nov 14 '22 23:11

enko