diff --git a/index.js b/index.js index 01f4571..2dc3301 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ var fs = require('fs'), util = require('util'), - finished = require('finished'), + finished = require('on-finished'), debug = require('debug')('multer-autoreap:middleware'); @@ -23,15 +23,20 @@ module.exports = function(req, res, next) { } fs.stat(file.path, function(err, stats) { if (!err && stats.isFile()) { - fs.unlink(file.path); - debug('removed %s', file.path); - res.emit('autoreap', file); + fs.unlink(file.path, function(err) { + if (err ) return console.warn(err); + debug('removed %s', file.path); + res.emit('autoreap', file); + }); } }); }; var reapFiles = function reapFiles(err) { var file, done = []; + var mapFn = function(file) { + processFile(err, file); + }; if (typeof req.files === "object") { for(var key in req.files) { if (Object.prototype.hasOwnProperty.call(req.files, key) && !~done.indexOf(key)) { @@ -39,9 +44,7 @@ module.exports = function(req, res, next) { if (!(file instanceof Array)) { file = [file]; } - file.forEach(function(file) { - processFile(err, file); - }); + file.forEach(mapFn); done.push(key); } } @@ -52,11 +55,8 @@ module.exports = function(req, res, next) { } }; - res.on('error', function(err) { - reapFiles(err); - }); + finished(res, reapFiles); - finished(res, reapFiles); next(); }; diff --git a/package.json b/package.json index 0609562..a2c8735 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "multer-autoreap", - "version": "0.1.2", + "version": "0.1.3", "description": "Connect middleware providing auto gc of tmp uploaded files by multer or any multipart middleware.", "license": "GPL-2.0+", "main": "index.js", @@ -27,7 +27,7 @@ }, "dependencies": { "debug": "^2.2.0", - "finished": "^1.2.2" + "on-finished": "^2.3.0" }, "devDependencies": { "chai": "^3.5.x",