Multer create new folder with data Multer create new folder with data express express

Multer create new folder with data


I will try to answer your question with a real life example, at least you might learn some stuff from it. If you wish to delete all except the most recent upload, then you need to code some sort of logic to differentiate between which upload is recent and which ones are old. Below I describe, how I would go about solving this issue, might not be perfect but, that is how I do it.

The folder will never be deleted automatically, unless you delete it either manually or programmatically.

The size of folder with 100 calls assuming that in each call you upload a file of x size would be x multiplied by 100

You don't want to limit the file upload, don't supply limits configs, however it is recommended to specify a file upload limit.

You can obviously attach the multer to the app or create an instance of it and pass that to a route. I prefer the second method:

multer config

var storage = multer.diskStorage({  destination: function (req, file, cb) {    cb(null, 'uploads/')  },  filename: function (req, file, cb) {    var filename = file.originalname;    var fileExtension = filename.split(".")[1];    cb(null, Date.now() + "." + fileExtension);  }}); 

As shown above, I do not let multer give a random name to the uploaded file. What I do is, get the file name, strip off its extension and then use Date.now() which will give me the current timestamp appended with the uploaded file extension. If I make six uploads, they would show as follow (most of my uploads were .jpg, that's taken from filename).

How upload would end up (timestamps would differ)

1453414099665.jpg (oldest) 1453414746114.JPG1453414748991.jpg1453414751662.jpg1453414754815.jpg (most recent)

I attached the above storage to an instance of multer as follow:

var upload = multer({storage: storage});

Now I can pass the upload to a route which handle the file upload as follow:

attach upload to route as shown below

//simple route to return upload form, simple jade fileapp.get('/upload', function(req, res){  res.render('upload');});//this route processes the upload request, see below upload.single('file') //is the passed multerapp.post('/upload', upload.single('file'),  function(req,res){  res.status(204).end();});

Let's say you keep uploading and then at some point you want to list all files in the uploads directory. The route would be as follow:

List all files in uploads directory

//lists all files in the uploads directory and return it to browser as json responseapp.get('/listAllFiles', function(req, res) {  //reading directory in synchronous way  var files = fs.readdirSync('./uploads');  res.json(files);});

You want to delete all files in the upload directory, the route would look as follow:

Delete all files in uploads directory

//delete all files in the upload direcotry asynchronouslyapp.get('/deleteAllFiles', function(req, res) {  fs.readdir('./uploads', function(err, items) {    items.forEach(function(file) {        fs.unlink('./uploads/' + file);        console.log('Deleted ' + file);    });    res.status(204).end();  });});

If you wish to delete all files synchronously then you have to invoke the sync version of readdir (readdirSync) and unlink (unlinkSync)

var filenames = fs.readdirSync('./uploads');filenames.forEach(function(file) {  fs.unlinkSync('./uploads/' + file);});

Now to your point of deleting all but, the most recent uploaded file. Well, I have already made all filename be timestamps. So I would do something as follow:

Delete all files except most recent (where most recent being the one with most recent timestamp as its filename).

//delets all file asynchronously except the most recent in which case the file//with name being the latest timestamp is skipped.app.get('/deleteAllExceptMostRecent', function(req, res) {  console.log('/deleteAllFilesExceptMostRecent');  fs.readdir('./uploads', function(err, items) {    //sort the array of files names in reverse, so we have most recent file on top    items.reverse();    var flag = true;    items.forEach(function(file) {      //skip deletion of most recent file. if condition executed onces only.      if(flag) {        flag = false;      } else {        fs.unlink('./uploads/' + file);        console.log('Deleted ' + file);      }    });  });  res.status(204).end();});

I have not added any limits in my example but, it is recommended. The default file size limit is infinity and if you don't include it in a prod environment then you will be vulnerable to DoS attacks as indicated in the comments.

For the above file operation to work you need to load

var fs = require('fs'); 

In regards to your second point, simply skip the limits property and the default limit will be infinity.

For demonstration purposes, I have setup the above in a working nodejs app, see below:

app.js

var express = require('express');var multer = require('multer');var bodyParser = require('body-parser');var path = require('path');var fs = require('fs');var app = new express();app.use(bodyParser.json());// view engine setupapp.set('views', path.join(__dirname, 'views'));app.set('view engine', 'jade');var storage = multer.diskStorage({  destination: function (req, file, cb) {    cb(null, 'uploads/')  },  filename: function (req, file, cb) {    /* if you need to retain the original filename, then use filename and append to it date     * if you don't need original filename but, just extension, then append extension at the     * end of current timestamp. If you don't need extenion then just use Date.now() which     */    var filename = file.originalname;    var fileExtension = filename.split(".")[1];    cb(null, Date.now() + "." + fileExtension);  }})var upload = multer({storage: storage});//get upload formapp.get('/upload', function(req, res){  res.render('upload');});//process uploadapp.post('/upload', upload.single('file'),  function(req,res){  res.status(204).end();});//lists all files in the uploads directory.app.get('/listAllFiles', function(req, res) {  var files = fs.readdirSync('./uploads');  res.json(files);});//delete all files in the upload direcotry asynchronouslyapp.get('/deleteAllFiles', function(req, res) {  fs.readdir('./uploads', function(err, items) {    items.forEach(function(file) {        fs.unlink('./uploads/' + file);        console.log('Deleted ' + file);    });    res.status(204).end();  });});//delets all file asynchronously except the most recent in which case the file//with name being the latest timestamp is skipped.app.get('/deleteAllExceptMostRecent', function(req, res) {  console.log('/deleteAllFilesExceptMostRecent');  fs.readdir('./uploads', function(err, items) {    items.reverse();    var flag = true;    items.forEach(function(file) {      if(flag) {        flag = false;      } else {        fs.unlink('./uploads/' + file);        console.log('Deleted ' + file);      }    });  });  res.status(204).end();});//delete all files of a direcotry in synchronous wayapp.get('/deleteAllSync', function(req, res) {  var filenames = fs.readdirSync('./uploads');  filenames.forEach(function(file) {    fs.unlinkSync('./uploads/' + file);  });});//delete all files except most recent in synchronous wayapp.get('/deleteAllSyncExceptMostRecent', function(req, res) {  var filenames = fs.readdirSync('./uploads');  filenames.reverse();  var flag = true;  filenames.forEach(function(file) {    if(flag)       flag = false;    else      fs.unlinkSync('./uploads/' + file);  });});var port = 3000;app.listen( port, function(){ console.log('listening on port '+port); } );

views/upload.jade

html  head    title  body    form(method="post",enctype="multipart/form-data",action="/upload")      p        input(type="file",name="file")      p        input(type="submit")


(1) When you make the following call you are telling multer to place uploaded files into a directory called uploads. So it will create that dir for you if it's not already present when your app starts up.

app.use(multer({        dest: './uploads'    }).single('file'));

In terms of that directory's life cycle, it will stay there as long as you don't delete it, and are still asking multer to use it as the destination. Uploaded files are being added there, so the size of its contents depends on what is being uploaded.

(2) As for limiting the file size, the default according to the docs is Infinity. So there is no limit unless you set one.

But, not knowing your application, I would still strongly suggest setting a limit, even if you need it to be fairly high. Removing the size limit altogether may lead to big problems.


edit
If you would like to delete the contents of ./uploads when a new file is uploaded, Node provides a way to delete a file: fs.unlink. See also SO about removing a file in node

In your upload handler, inspect the contents of ./uploads, and unlink any file that is not the file used in the current request. See req.file for the current upload.


Que 1) You can decide folders life cycle.

  1. If you want to store files on server & retrive it back at any time without file size limit , u need bigger storage
  2. If you are using third party like Amazon S3 to store, no point in storing locally, as soon as its uploaded to server you can remove from local storage.You can use after Hook when response sent.

    app.use(function (req, res, next) {function afterResponse() {    res.removeListener('finish', afterRequest);    res.removeListener('close', afterRequest);    // Delete files from multer     // you can delete older one from upload folder as you see new one came here.}res.on('finish', afterResponse);res.on('close', afterResponse);// action before request// eventually calling `next()` });

Que 2) Default is limitless.