gallery.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const Uploader = require("../models/uploader.js");
  2. const Gallery = require("../models/gallery.js");
  3. const createId = require("./createId.js");
  4. module.exports = {
  5. /*
  6. POST: create a new gallery
  7. req.body = {
  8. uploader: String (id of owner)
  9. password: String
  10. title: String
  11. tags: [String]
  12. coordinates: String
  13. }
  14. req.files = [images]
  15. */
  16. create: function(req, res){
  17. Uploader.findOne({_id: req.body.uploader})
  18. .then((uploader)=>{
  19. if(uploader === null) throw "uploader";
  20. if(req.body.password !== uploader.password) throw "pass";
  21. let coords = req.body.coordinates.split(", ")
  22. let gallery = new Gallery({
  23. owner: uploader._id,
  24. title: req.body.title,
  25. tags: req.body.tags.split(","),
  26. location: {
  27. type: "Point",
  28. coordinates: [parseFloat(coords[0]), parseFloat(coords[1])]
  29. },
  30. images: []
  31. });
  32. let handleImage = (fileData)=>{
  33. let fileString = `/galleryImages/${createId(25)}.jpg`;
  34. fileData.mv(`${__dirname}/..${fileString}`);
  35. gallery.images.push(fileString)
  36. }
  37. if(req.files.images.length === undefined) handleImage(req.files.images);
  38. for(let i = 0; i < req.files.images.length; i++){
  39. handleImage(req.files.images[i]);
  40. };
  41. return gallery.save();
  42. })
  43. .then((gallery)=>{
  44. return res.redirect(`/gallery/${gallery._id}`);
  45. })
  46. .catch((err)=>{
  47. console.error(err);
  48. if(err === "uploader") return res.json("You do not have permission to upload");
  49. if(err === "pass") return res.json("Incorrect password");
  50. return res.json("ERROR: something went wrong");
  51. });
  52. },
  53. getGalleries: function(req, res){
  54. Gallery.find()
  55. .then((galleries)=>{
  56. return res.json({galleries: galleries, mapboxKey: process.env.MAPBOX_KEY});
  57. })
  58. .catch((err)=>{
  59. return res.json("ERROR: could not fetch galleries");
  60. });
  61. },
  62. /*
  63. GET: fetch the data for a single gallery
  64. req.params.id = String (gallery id)
  65. response = Gallery
  66. */
  67. getGallery: function(req, res){
  68. Gallery.findOne({_id: req.params.id})
  69. .then((gallery)=>{
  70. return res.json(gallery);
  71. })
  72. .catch((err)=>{
  73. return res.json("ERROR: could not fetch gallery");
  74. });
  75. },
  76. /*
  77. POST: updates a gallery
  78. req.body = {
  79. uploader: String
  80. password: String
  81. title: String
  82. tags: String
  83. location: String
  84. }
  85. req.files = {
  86. images: undefined || Image || [Image]
  87. }
  88. req.params.id = String (id of gallery)
  89. */
  90. updateGallery: function(req, res){
  91. Promise.all([
  92. Uploader.findOne({_id: req.body.uploader}),
  93. Gallery.findOne({_id: req.params.id})
  94. ])
  95. .then((response)=>{
  96. if(response[0] === null || response[1].owner.toString() !== req.body.uploader) throw "uploader";
  97. if(response[0].password !== req.body.password) throw "password";
  98. let coords = req.body.location.split(", ");
  99. response[1].title = req.body.title;
  100. response[1].tags = req.body.tags.split(",");
  101. response[1].location = {
  102. type: "Point",
  103. coordinates: [parseFloat(coords[0]), parseFloat(coords[1])]
  104. };
  105. let handleImage = (fileData)=>{
  106. let fileString = `/galleryImages/${createId(25)}.jpg`;
  107. fileData.mv(`${__dirname}/..${fileString}`);
  108. response[1].images.push(fileString);
  109. }
  110. if(req.files !== null){
  111. if(req.files.images.length === undefined) handleImage(req.files.images);
  112. for(let i = 0; i < req.files.images.length; i++){
  113. handleImage(req.files.images[i]);
  114. }
  115. }
  116. return response[1].save()
  117. })
  118. .then((gallery)=>{
  119. return res.redirect(`/gallery/${gallery._id}`);
  120. })
  121. .catch((err)=>{
  122. if(err === "uploader") return res.json("Incorrect uploader");
  123. if(err === "password") return res.json("Incorrect password");
  124. return res.json("ERROR: a whoopsie happened");
  125. })
  126. }
  127. }