javascript - how to prevent the sailsjs file upload override -


i'm trying make file uploader in sails.js, have followed docs , used saveas function this:

create: function(req, res){     var filename = req.file('file')._files[0].stream.filename;     var file = req.file('file');     file.upload({dirname: '../../assets/uploads/', saveas: function (__newfilestream,cb) { cb(null, filename); }}, function(err, uploadedfiles){         if(err){             return res.json(err);         }         archivo.create({nombre: uploadedfiles[0].filename, url: '/uploads/' + uploadedfiles[0].filename}).exec(function(err, uploadedfile){             if(err){                 return res.json(err);             }             return res.json(uploadedfile);         });      }); }, 

the create controller uploads file filename assets folder, , creates archivo record

my problem dont want override file if upload again, example if upload file called image.jpg, , upload again want second time renames image(1).jpg

someone knows how it?

the following when used in conjunction give number of files match 1 uploading based on file name being uploaded. use regex match based on template image(1).jpg.

so if upload image.jpg in directory , find files match, of following match.

image.jpg image(1).jpg image(2).jpg image(3).jpg image(4).jpg 

so system return files letting know have 5 matching. can use number rename file uploading @ image(5).jpg.

this should started. uses npm fs-finder library.

var finder = require('fs-finder');,     filename = req.file('file')._files[0].stream.filename,     directory = '../../assets/uploads/',     fileprefix = filename.split('.')[0].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'),     fileextension = filename.split('.')[1].replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')),       var numberofmatchingfiles =          finder             .in(direcotry)             .findfiles(                 fileprefix +                  '(\([0-9]*\))*\' +                  fileextension             ).length 

like said, started. have , issue delete image(2).jpg , have 4 files , attempting name newly uploaded file image(4).jpg on write file. can check against list of files returned fs-finder.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -