ソースを参照

Backend now saves file name as well as using an ID number to store the file.

Lee Morgan 5 年 前
コミット
28a524f95c
4 ファイル変更19 行追加112 行削除
  1. 0 3
      app.js
  2. 15 4
      controllers/learn.js
  3. 0 104
      htmlCreator.js
  4. 4 1
      models/lecture.js

+ 0 - 3
app.js

@@ -1,5 +1,3 @@
-const htmlCreator = require("./htmlCreator.js");
-
 const express = require("express");
 const compression = require("compression");
 const mongoose = require("mongoose");
@@ -9,7 +7,6 @@ const bodyParser = require("body-parser");
 const fileUpload = require("express-fileupload");
 
 const app = express();
-htmlCreator(app);
 
 let mongooseOptions = {
     useNewUrlParser: true,

+ 15 - 4
controllers/learn.js

@@ -1,6 +1,7 @@
 const Uploader = require("../models/uploader.js");
 const Course = require("../models/course.js");
 const Lecture = require("../models/lecture.js");
+const createId = require("./createId.js");
 
 module.exports = {
     /*
@@ -81,14 +82,24 @@ module.exports = {
                 if(req.files !== null){
                     let files = req.files.documents;
                     if(files.length === undefined){
-                        let fileString = `/documents/${files.name}`;
+                        let fileId = createId(25);
+                        let fileParts = files.name.split(".");
+                        let fileString = `/documents/${fileId}.${fileParts[1]}`;
                         files.mv(`${__dirname}/..${fileString}`);
-                        lecture.documents.push(fileString);
+                        lecture.documents.push({
+                            name: fileParts[0],
+                            link: fileString
+                        })
                     }else{
                         for(let i = 0; i < files.length; i++){
-                            let fileString = `/documents/${files[i].name}`;
+                            let fileId = createId(25);
+                            let fileParts = files[i].name.split(".");
+                            let fileString = `/documents/${fileId}.${fileParts[1]}`;
                             files[i].mv(`${__dirname}/..${fileString}`);
-                            lecture.documents.push(fileString);
+                            lecture.documents.push({
+                                name: fileParts[0],
+                                link: fileString
+                            });
                         }
                     }
                 }

+ 0 - 104
htmlCreator.js

@@ -1,104 +0,0 @@
-const fs = require("fs");
-
-module.exports = (app)=>{
-    let createHTML = (dir)=>{
-        let article = fs.readFileSync(`${dir}/article.txt`).toString().split("\n");
-        let meta = fs.readFileSync(`${dir}/meta.txt`).toString().split("\n");
-        let route = dir.substring(dir.indexOf("/content/writing/") + 17).replace(/\//g, "-");
-
-        let html = `
-        <!DOCTYPE html>
-        <html lang="en">
-            <head>
-                <meta charset="utf-8">
-                <title>${meta[0]}</title>
-                <link rel="stylesheet" href="/writing/style">
-            </head>
-            <body>
-                <div class="article">
-                    <h1>${meta[0]}</h1>
-
-                    <h3 class="metaData">By ${meta[1]}</h3>
-
-		    <h3 class="metaData">On ${meta[2]}</h3>
-        `;
-
-        for(let i = 0; i < article.length; i++){
-            if(article[i] === "") continue;
-
-            html += `<p class="paragraph">${article[i]}</p>`;
-        }
-
-        html += `
-                    </div>
-
-                    <form id="createComment">
-                        <h2>Add Comment</h2>
-
-                        <label>Display Name:
-                            <input id="nameInput" type="text">
-                        </label>
-
-                        <label>Comment:
-                            <textarea id="commentInput" rows="10" columns="80" required></textarea>
-                        </label>
-
-                        <input type="Submit" value="SUBMIT">
-
-                        <input id="articleInput" type="hidden" article="${route}">
-                    </form>
-
-                    <div id="comments">
-                        <h2>Comments</h2>
-                        
-                        <template id="comment">
-                            <div class="comment">
-                                <div>
-                                    <h3></h3>
-
-                                    <p></p>
-                                </div>
-                                
-                                <p></p>
-                            </div>
-                        </template>
-                    </div>
-
-                    <script src="/writing/code"></script>
-                </body>
-            </html>
-        `;
-
-        fs.writeFileSync(`${dir}/article.html`, html);
-    }
-
-    let createRoute = (dir)=>{
-        let route = dir.substring(dir.indexOf("/content/writing/") + 8);
-
-        let contents = fs.readdirSync(dir);
-        for(let i = 0; i < contents.length; i++){
-            if(contents[i].includes(".jpg") === true){
-                app.get(`${route}/${contents[i]}`, (req, res)=>{res.sendFile(`${__dirname}/content/${route}/${contents[i]}`)});
-            }
-        }
-
-        app.get(route, (req, res)=>{res.sendFile(`${__dirname}/content/${route}/article.html`)});
-    }
-
-    let traverseDirectory = (dir)=>{
-        let contents = fs.readdirSync(dir);
-
-        let hasHtml = false;
-        for(let i = 0; i < contents.length; i++){
-            if(contents[i].includes(".") === false) traverseDirectory(`${dir}/${contents[i]}`);
-            if(contents[i].includes(".html") === true) hasHtml = true;
-        }
-
-        if(contents.includes("article.txt") === true){
-            if(hasHtml === false) createHTML(dir);
-            createRoute(dir);
-        }
-    }
-
-    traverseDirectory(`${__dirname}/content/writing`);
-}

+ 4 - 1
models/lecture.js

@@ -18,7 +18,10 @@ const LectureSchema = new mongoose.Schema({
         type: String,
         required: true,
     },
-    documents: [String],
+    documents: [{
+        name: String,
+        link: String
+    }],
     exercises: [String],
     url: String,
     date: {