mongodb - Uploading multiuple images with Node.js using Multer S3/Amazon S3 -


overview: have figured out how allow users upload multiple images amazon s3 using node, express & multers3. code shows solution below. working well. issue on edit route. want users able edit or delete images. far way have been able have new images override previous filepaths.

this big problem because means user has reupload every single image every time try edit post. not good. think solution might create images array instead of having individual image paths. appreciate advice.

i've seen code creating array local storage i'm not sure how alter work amazon s3:

if(req.files.length) {  for(var = 0; < req.files.length; i++) {  updatedlisting.moreimages.push() // needs push amazon s3 here somehow.  }  updatedlisting.save(); 

first show "listings" mongoose model:

var listingsschema = new mongoose.schema({    name: string,    description: string,    image: string,    image2: string,    image3: string,    image4: string,    image5: string,    author: {       id:{        type: mongoose.schema.types.objectid,        ref: "user"       },        username: string,        email: string    } }); 

my code upload images amazon s3:

aws.config.update({     secretaccesskey: 'xxxxxxxxxxxxxxxxxxxxxxxx',     accesskeyid: 'xxxxxxxxxxxxx',     region: 'us-east-2' });  var s3 = new aws.s3();  var storage =  multers3({             s3: s3,             bucket: 'mybucket',             key: function (req, file, cb) {                 var fileextension = file.originalname.split(".")[1];                 var path = "uploads/" + req.user._id + date.now() + "." + fileextension;                 cb(null, path);              },         }) 

here code find images , create new listing. you'll see below how hard coding check each filepath. believe better solution here loop unsure how update code.

var upload = multer({storage: storage}).any("image", 5);  router.post("/", middleware.isloggedin, function(req, res, next){          upload(req,res,function(err) {         if(err) {         console.log(err);         res.redirect('/')         }         var filepath = undefined;        var filepath2 = undefined;        var filepath3 = undefined;        var filepath4 = undefined;        var filepath5 = undefined;      if(req.files[0]) {         filepath = req.files[0].key;     }         if(req.files[1]) {         filepath2 = req.files[1].key;     }        if(req.files[2]) {         filepath3 = req.files[2].key;     }         if(req.files[3]) {         filepath4 = req.files[3].key;     }         if(req.files[4]) {         filepath5 = req.files[4].key;     }      var name = req.body.name;     var desc = req.body.description;     var image = filepath;     var image2 = filepath2;     var image3 = filepath3;     var image4 = filepath4;     var image5 = filepath5;       var newlistings = {name: name, description: description, image: image, image2: image2, image3: image3, image4: image4, image5: image5}      // create new listing , save db     listings.create(newlistings, function(err, newlycreated){         if(err){             console.log(err);         } else {  res.redirect("/listings");         }     });     });     });     }); 

and lastly part of update put page. real issue is. want user able upload new images without overriding old images. you'll see below filepaths same on post route overrides images regardless of how many images selected.

router.put("/:id", middleware.checklistingsownership, function(req, res){               upload(req,res,function(err) {             if(err) {             console.log(err);             res.redirect('/')             }             var filepath = undefined;            var filepath2 = undefined;            var filepath3 = undefined;            var filepath4 = undefined;            var filepath5 = undefined;          if(req.files[0]) {             filepath = req.files[0].key;         }             if(req.files[1]) {             filepath2 = req.files[1].key;         }            if(req.files[2]) {             filepath3 = req.files[2].key;         }             if(req.files[3]) {             filepath4 = req.files[3].key;         }             if(req.files[4]) {             filepath5 = req.files[4].key;         }          var name = req.body.name;         var desc = req.body.description;         var image = filepath;         var image2 = filepath2;         var image3 = filepath3;         var image4 = filepath4;         var image5 = filepath5; 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -