Explorar el Código

Add backend for creating new galleries.
Update gitignore for gallery.

Lee Morgan hace 5 años
padre
commit
ed3ee06996
Se han modificado 4 ficheros con 61 adiciones y 1 borrados
  1. 2 1
      .gitignore
  2. 45 0
      controllers/gallery.js
  3. 5 0
      routes.js
  4. 9 0
      views/gallery/new.html

+ 2 - 1
.gitignore

@@ -2,4 +2,5 @@
 /content/*
 *.swp
 /thumbNails/*
-/documents/*
+/documents/*
+/galleryImages/*

+ 45 - 0
controllers/gallery.js

@@ -0,0 +1,45 @@
+const Uploader = require("../models/uploader.js");
+const Gallery = require("../models/gallery.js");
+
+module.exports = {
+    /*
+    POST: create a new gallery
+    req.body = {
+        owner: String (id of owner),
+        password: String,
+        tags: [String]
+    }
+    req.files = [images]
+    */
+    create: function(req, res){
+        Uploader.findOne({_id: req.body.id})
+            .then((uploader)=>{
+                if(uploader === null) throw "uploader";
+                if(req.body.password !== uploader.password) throw "pass";
+
+                let gallery = new Gallery({
+                    owner: uploader._id,
+                    tags: req.body.tags.split(","),
+                    images: []
+                });
+
+                let files = req.files.images;
+                for(let i = 0; i < files.length; i++){
+                    let fileString = `/galleryImages/${files[i].name}`;
+                    files[i].mv(`${__dirname}/..${fileString}`);
+                    gallery.push(fileString);
+                };
+
+                return gallery.save();
+            })
+            .then((gallery)=>{
+            return res.redirect("/");
+            })
+            .catch((err)=>{
+                console.log(err);
+                if(err === "uploader") return res.json("You do not have permission to upload");
+                if(err === "pass") return res.json("Incorrect password");
+                return res.json("ERROR: something went wrong");
+            })
+    }
+}

+ 5 - 0
routes.js

@@ -1,5 +1,6 @@
 const writing = require("./controllers/writing.js");
 const travel = require("./controllers/travel.js");
+const gallery = require("./controllers/gallery.js");
 const learn = require("./controllers/learn.js");
 
 module.exports = function(app){
@@ -41,6 +42,10 @@ module.exports = function(app){
     app.get("/travel/images/*", travel.getImages);
     app.get("/travel/*", (req, res)=>res.sendFile(`${views}/travel/index.html`));
 
+    //GALLERY
+    app.get("/gallery/new", (req, res)=>{res.sendFile(`${views}/travel/new.html`)});
+    app.post("/gallery/new", gallery.create);
+
     //LEARN
     app.get("/learn/style", (req, res)=>{res.sendFile(`${views}/learn/index.css`)});
     

+ 9 - 0
views/gallery/new.html

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+        <title>Lee Morgan</title>
+    </head>
+    <body>
+    </body>
+</html>