Skip to content

Express middleware for auto cleanup (reap) uploaded files by multer or any multipart middleware propagating req.files.

License

Notifications You must be signed in to change notification settings

joelabair/multer-autoreap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

multer-autoreap

Express middleware for reaping uploaded files saved to disk by multer or any multipart middleware propagating the req.files object. The middleware will automatically remove any uploaded files left in their temporary location upon response end or close.

Installation

$ npm install multer-autoreap

Usage

as app middleware

const express = require('express');
const multer  = require('multer');
const autoReap  = require('multer-autoreap');

let app = express();
app.use(multer({ dest: '/tmp/' }));
app.use(autoReap);
...

or attaching to a route / router.

const express = require('express');
const router = express.Router();

const multer  = require('multer');
const autoReap  = require('multer-autoreap');

let app = express();
app.use(multer({ dest: '/tmp/' }));

app.route('/upload-a').post(autoReap, function(req, res, next) {
	res.on('autoreap', function(file) {
		console.log('auto-reaped: ', file);
	});

	res.send('ok');
});

router.use('/upload-b', autoReap, function(req, res, next) {
	res.on('autoreap', function(file) {
		console.log('auto-reaped: ', file);
	});

	res.send('ok');
});
...

Multer is an efficient multipart/form-data handling middleware that uses busboy. Files encoded in a miltipart request body are piped to a temporary upload location (def: multer options dest ). This can have the effect of leaving open an attack vector where disk space can be consumed by these temporary files. Its prudent and generally good form to clean them up. While reap cleans based on age, multer-autoreap cleans them up as soon as the request is done.

Options

autoReap.options = {
	reapOnError: true
};
  • reapOnError (boolean) - If an error occurs, continue reaping the file, or no.
var autoReap  = require('multer-autoreap');
autoReap.options.reapOnError = false;

Events

The middleware will emit an 'autoreap' event on the Response object when removing files. The event will include the original file object from req.files[].

res.on('autoreap', function(reapedFile) {
	console.log(reapedFile);
});

Please report any issues...

About

Express middleware for auto cleanup (reap) uploaded files by multer or any multipart middleware propagating req.files.

Resources

License

Stars

Watchers

Forks

Packages

No packages published