Coder Social home page Coder Social logo

jafl / multer-autoreap Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joelabair/multer-autoreap

0.0 0.0 0.0 48 KB

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

License: MIT License

JavaScript 100.00%

multer-autoreap's Introduction

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...

multer-autoreap's People

Contributors

joelabair avatar jafl avatar rferro avatar kapouer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.