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!
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", []);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With