Quellcode durchsuchen

Create ability to upload new galleries.

Lee Morgan vor 5 Jahren
Ursprung
Commit
13327f3ff1
4 geänderte Dateien mit 57 neuen und 7 gelöschten Zeilen
  1. 10 0
      controllers/createId.js
  2. 6 6
      controllers/gallery.js
  3. 1 1
      routes.js
  4. 40 0
      views/gallery/new.html

+ 10 - 0
controllers/createId.js

@@ -0,0 +1,10 @@
+module.exports = (length)=>{
+    let result = "";
+    let characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+
+    for(let i = 0; i < length; i++){
+        result += characters.charAt(Math.floor(Math.random() * characters.length));
+    }
+
+    return result;
+}

+ 6 - 6
controllers/gallery.js

@@ -1,18 +1,19 @@
 const Uploader = require("../models/uploader.js");
 const Gallery = require("../models/gallery.js");
+const createId = require("./createId.js");
 
 module.exports = {
     /*
     POST: create a new gallery
     req.body = {
-        owner: String (id of owner),
+        uploader: String (id of owner),
         password: String,
         tags: [String]
     }
     req.files = [images]
     */
     create: function(req, res){
-        Uploader.findOne({_id: req.body.id})
+        Uploader.findOne({_id: req.body.uploader})
             .then((uploader)=>{
                 if(uploader === null) throw "uploader";
                 if(req.body.password !== uploader.password) throw "pass";
@@ -25,18 +26,17 @@ module.exports = {
 
                 let files = req.files.images;
                 for(let i = 0; i < files.length; i++){
-                    let fileString = `/galleryImages/${files[i].name}`;
+                    let fileString = `/galleryImages/${createId(25)}.jpg`;
                     files[i].mv(`${__dirname}/..${fileString}`);
-                    gallery.push(fileString);
+                    gallery.images.push(fileString);
                 };
 
                 return gallery.save();
             })
             .then((gallery)=>{
-            return res.redirect("/");
+                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");

+ 1 - 1
routes.js

@@ -43,7 +43,7 @@ module.exports = function(app){
     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.get("/gallery/new", (req, res)=>{res.sendFile(`${views}/gallery/new.html`)});
     app.post("/gallery/new", gallery.create);
 
     //LEARN

+ 40 - 0
views/gallery/new.html

@@ -3,7 +3,47 @@
     <head>
         <meta charset="utf-8">
         <title>Lee Morgan</title>
+        <style>
+            form{
+                display: flex;
+                flex-direction: column
+            }
+
+            form > *{
+                margin: 15px;
+            }
+
+            input[type=submit]{
+                max-width: 250px;
+            }
+        </style>
     </head>
     <body>
+        <h1>New Gallery</h1>
+
+        <form
+            id="form"
+            action="/gallery/new"
+            method="post"
+            enctype="multipart/form-data"
+        >
+            <label>Uploader
+                <input name="uploader" type="text" required>
+            </label>
+
+            <label>Password
+                <input name="password" type="password" required>
+            </label>
+
+            <label>Tags (comma delineated)
+                <input name="tags" type="text" required>
+            </label>
+
+            <label>Images
+                <input name="images" type="file" required multiple>
+            </label>
+
+            <input type="submit" value="Submit">
+        </form>
     </body>
 </html>