gallery.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const Uploader = require("../models/uploader.js");
  2. const Gallery = require("../models/gallery.js");
  3. module.exports = {
  4. /*
  5. POST: create a new gallery
  6. req.body = {
  7. owner: String (id of owner),
  8. password: String,
  9. tags: [String]
  10. }
  11. req.files = [images]
  12. */
  13. create: function(req, res){
  14. Uploader.findOne({_id: req.body.id})
  15. .then((uploader)=>{
  16. if(uploader === null) throw "uploader";
  17. if(req.body.password !== uploader.password) throw "pass";
  18. let gallery = new Gallery({
  19. owner: uploader._id,
  20. tags: req.body.tags.split(","),
  21. images: []
  22. });
  23. let files = req.files.images;
  24. for(let i = 0; i < files.length; i++){
  25. let fileString = `/galleryImages/${files[i].name}`;
  26. files[i].mv(`${__dirname}/..${fileString}`);
  27. gallery.push(fileString);
  28. };
  29. return gallery.save();
  30. })
  31. .then((gallery)=>{
  32. return res.redirect("/");
  33. })
  34. .catch((err)=>{
  35. console.log(err);
  36. if(err === "uploader") return res.json("You do not have permission to upload");
  37. if(err === "pass") return res.json("Incorrect password");
  38. return res.json("ERROR: something went wrong");
  39. })
  40. }
  41. }