From d1162d1bd12cbb3eae9889768adbbb293852c066 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Fri, 1 Feb 2019 13:02:50 -0500 Subject: Remove /photo/ to uploads folder --- .gitignore | 2 + package-lock.json | 147 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 + src/html/admin.html | 36 +++++++++++++ src/html/bread.html | 2 +- src/html/index.html | 2 +- src/index.js | 3 +- src/photo/back.jpg | Bin 1181073 -> 0 bytes src/photo/ban1.jpg | Bin 1106159 -> 0 bytes src/photo/ban2.jpg | Bin 1411456 -> 0 bytes src/photo/cider.jpg | Bin 725680 -> 0 bytes src/photo/first-1.jpg | Bin 283944 -> 0 bytes src/photo/first-2.jpg | Bin 296466 -> 0 bytes src/photo/flat1.jpg | Bin 1129498 -> 0 bytes src/photo/flat2.jpg | Bin 905668 -> 0 bytes src/photo/good1.jpg | Bin 241664 -> 0 bytes src/photo/good2.jpg | Bin 1847478 -> 0 bytes src/photo/nyt1.jpg | Bin 2142167 -> 0 bytes src/photo/nyt2.jpg | Bin 1286502 -> 0 bytes src/photo/pizza1.jpg | Bin 244008 -> 0 bytes src/photo/pizza2.jpg | Bin 1727783 -> 0 bytes src/photo/pretty.jpg | Bin 251665 -> 0 bytes src/photo/wheat.jpg | Bin 2669180 -> 0 bytes src/photo/white-1.jpg | Bin 1652366 -> 0 bytes src/photo/white-2.jpg | Bin 1811909 -> 0 bytes src/server.js | 57 ++++++++++++++++++-- 26 files changed, 242 insertions(+), 9 deletions(-) create mode 100644 src/html/admin.html delete mode 100644 src/photo/back.jpg delete mode 100644 src/photo/ban1.jpg delete mode 100644 src/photo/ban2.jpg delete mode 100644 src/photo/cider.jpg delete mode 100644 src/photo/first-1.jpg delete mode 100644 src/photo/first-2.jpg delete mode 100644 src/photo/flat1.jpg delete mode 100644 src/photo/flat2.jpg delete mode 100644 src/photo/good1.jpg delete mode 100644 src/photo/good2.jpg delete mode 100644 src/photo/nyt1.jpg delete mode 100644 src/photo/nyt2.jpg delete mode 100644 src/photo/pizza1.jpg delete mode 100644 src/photo/pizza2.jpg delete mode 100644 src/photo/pretty.jpg delete mode 100644 src/photo/wheat.jpg delete mode 100644 src/photo/white-1.jpg delete mode 100644 src/photo/white-2.jpg diff --git a/.gitignore b/.gitignore index 67b12f5..e72625d 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,5 @@ typings/ .next config.json +uploads/ + diff --git a/package-lock.json b/package-lock.json index 114e4d0..2fe6214 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34,6 +34,11 @@ "uri-js": "^4.2.2" } }, + "append-field": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz", + "integrity": "sha1-HjRA6RXwsSA9I3SOeO3XubW0PlY=" + }, "array-flatten": { "version": "1.1.1", "resolved": "http://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", @@ -102,6 +107,20 @@ "resolved": "https://registry.npmjs.org/bootstrap/-/bootstrap-4.1.3.tgz", "integrity": "sha512-rDFIzgXcof0jDyjNosjv4Sno77X4KuPeFxG2XZZv1/Kc8DRVGVADdoQyyOVDwPqL36DDmtCQbrpMCqvpPLJQ0w==" }, + "buffer-from": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==" + }, + "busboy": { + "version": "0.2.14", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz", + "integrity": "sha1-bCpiLvz0fFe7vh4qnDetNseSVFM=", + "requires": { + "dicer": "0.2.5", + "readable-stream": "1.1.x" + } + }, "bytes": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", @@ -129,6 +148,46 @@ "delayed-stream": "~1.0.0" } }, + "concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "requires": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" + }, + "dependencies": { + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "readable-stream": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + } + } + }, "content-disposition": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz", @@ -190,6 +249,15 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" }, + "dicer": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/dicer/-/dicer-0.2.5.tgz", + "integrity": "sha1-WZbAhrszIYyBLAkL3cCc0S+stw8=", + "requires": { + "readable-stream": "1.1.x", + "streamsearch": "0.1.2" + } + }, "dottie": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/dottie/-/dottie-2.0.1.tgz", @@ -414,6 +482,11 @@ "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", "integrity": "sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=" }, + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, "isstream": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", @@ -502,6 +575,19 @@ "mime-db": "~1.37.0" } }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "requires": { + "minimist": "0.0.8" + } + }, "moment": { "version": "2.24.0", "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", @@ -520,6 +606,21 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "multer": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/multer/-/multer-1.4.1.tgz", + "integrity": "sha512-zzOLNRxzszwd+61JFuAo0fxdQfvku12aNJgnla0AQ+hHxFmfc/B7jBVuPr5Rmvu46Jze/iJrFpSOsD7afO8SDw==", + "requires": { + "append-field": "^1.0.0", + "busboy": "^0.2.11", + "concat-stream": "^1.5.2", + "mkdirp": "^0.5.1", + "object-assign": "^4.1.1", + "on-finished": "^2.3.0", + "type-is": "^1.6.4", + "xtend": "^4.0.0" + } + }, "mysql2": { "version": "1.6.4", "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-1.6.4.tgz", @@ -570,6 +671,11 @@ "resolved": "https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz", "integrity": "sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==" }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, "on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", @@ -593,6 +699,11 @@ "resolved": "https://registry.npmjs.org/performance-now/-/performance-now-2.1.0.tgz", "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=" }, + "process-nextick-args": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", + "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==" + }, "proxy-addr": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.4.tgz", @@ -638,6 +749,17 @@ "unpipe": "1.0.0" } }, + "readable-stream": { + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, "request": { "version": "2.88.0", "resolved": "https://registry.npmjs.org/request/-/request-2.88.0.tgz", @@ -800,6 +922,16 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" }, + "streamsearch": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz", + "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=" + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + }, "terraformer": { "version": "1.0.9", "resolved": "https://registry.npmjs.org/terraformer/-/terraformer-1.0.9.tgz", @@ -860,6 +992,11 @@ "mime-types": "~2.1.18" } }, + "typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -873,6 +1010,11 @@ "punycode": "^2.1.0" } }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -916,6 +1058,11 @@ "@types/node": "*" } }, + "xtend": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" + }, "yallist": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", diff --git a/package.json b/package.json index 072b66b..716b581 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,10 @@ "author": "", "license": "ISC", "dependencies": { + "body-parser": "^1.18.3", "bootstrap": "^4.1.3", "express": "^4.16.4", + "multer": "^1.4.1", "mysql2": "^1.6.4", "request": "^2.88.0", "sequelize": "^4.42.0", diff --git a/src/html/admin.html b/src/html/admin.html new file mode 100644 index 0000000..7c87ba6 --- /dev/null +++ b/src/html/admin.html @@ -0,0 +1,36 @@ + + + + + Mark's Kitchen - Admin + + + + + + + +
+

Create Post

+
+
+
+ +
+
+ Images: +
+
+ +
+ +
+
+
+ + + \ No newline at end of file diff --git a/src/html/bread.html b/src/html/bread.html index 8004803..e6eeb47 100644 --- a/src/html/bread.html +++ b/src/html/bread.html @@ -17,7 +17,7 @@ posts: [] }, created() { - fetch(new Request('/bread/posts.json')).then(response => response.json()) + fetch(new Request('/posts/bread')).then(response => response.json()) .then(response => this.posts = response.data); } }); diff --git a/src/html/index.html b/src/html/index.html index 7a1d5d5..c7e398b 100644 --- a/src/html/index.html +++ b/src/html/index.html @@ -18,7 +18,7 @@ posts: [] }, created() { - fetch(new Request('/index/posts.json')).then(response => response.json()) + fetch(new Request('/posts/index')).then(response => response.json()) .then(response => this.posts = response.data); } }); diff --git a/src/index.js b/src/index.js index fdcef74..b47824a 100644 --- a/src/index.js +++ b/src/index.js @@ -40,8 +40,7 @@ function setUpModels(){ type: { type: Sequelize.STRING, allowNull: false, - }, - }), + },}), "pictures": database.define('pictures', { source: { type: Sequelize.TEXT, allowNull: false}, }) diff --git a/src/photo/back.jpg b/src/photo/back.jpg deleted file mode 100644 index 9e8511e..0000000 Binary files a/src/photo/back.jpg and /dev/null differ diff --git a/src/photo/ban1.jpg b/src/photo/ban1.jpg deleted file mode 100644 index 0330443..0000000 Binary files a/src/photo/ban1.jpg and /dev/null differ diff --git a/src/photo/ban2.jpg b/src/photo/ban2.jpg deleted file mode 100644 index f868504..0000000 Binary files a/src/photo/ban2.jpg and /dev/null differ diff --git a/src/photo/cider.jpg b/src/photo/cider.jpg deleted file mode 100644 index 176a094..0000000 Binary files a/src/photo/cider.jpg and /dev/null differ diff --git a/src/photo/first-1.jpg b/src/photo/first-1.jpg deleted file mode 100644 index 664acd7..0000000 Binary files a/src/photo/first-1.jpg and /dev/null differ diff --git a/src/photo/first-2.jpg b/src/photo/first-2.jpg deleted file mode 100644 index 11f2ab7..0000000 Binary files a/src/photo/first-2.jpg and /dev/null differ diff --git a/src/photo/flat1.jpg b/src/photo/flat1.jpg deleted file mode 100644 index ff6df1c..0000000 Binary files a/src/photo/flat1.jpg and /dev/null differ diff --git a/src/photo/flat2.jpg b/src/photo/flat2.jpg deleted file mode 100644 index d07f638..0000000 Binary files a/src/photo/flat2.jpg and /dev/null differ diff --git a/src/photo/good1.jpg b/src/photo/good1.jpg deleted file mode 100644 index 3ff6e6b..0000000 Binary files a/src/photo/good1.jpg and /dev/null differ diff --git a/src/photo/good2.jpg b/src/photo/good2.jpg deleted file mode 100644 index bec268d..0000000 Binary files a/src/photo/good2.jpg and /dev/null differ diff --git a/src/photo/nyt1.jpg b/src/photo/nyt1.jpg deleted file mode 100644 index a31e79e..0000000 Binary files a/src/photo/nyt1.jpg and /dev/null differ diff --git a/src/photo/nyt2.jpg b/src/photo/nyt2.jpg deleted file mode 100644 index 5c955da..0000000 Binary files a/src/photo/nyt2.jpg and /dev/null differ diff --git a/src/photo/pizza1.jpg b/src/photo/pizza1.jpg deleted file mode 100644 index 051d5d0..0000000 Binary files a/src/photo/pizza1.jpg and /dev/null differ diff --git a/src/photo/pizza2.jpg b/src/photo/pizza2.jpg deleted file mode 100644 index c5e9021..0000000 Binary files a/src/photo/pizza2.jpg and /dev/null differ diff --git a/src/photo/pretty.jpg b/src/photo/pretty.jpg deleted file mode 100644 index 39afb35..0000000 Binary files a/src/photo/pretty.jpg and /dev/null differ diff --git a/src/photo/wheat.jpg b/src/photo/wheat.jpg deleted file mode 100644 index b039eb6..0000000 Binary files a/src/photo/wheat.jpg and /dev/null differ diff --git a/src/photo/white-1.jpg b/src/photo/white-1.jpg deleted file mode 100644 index a58d50b..0000000 Binary files a/src/photo/white-1.jpg and /dev/null differ diff --git a/src/photo/white-2.jpg b/src/photo/white-2.jpg deleted file mode 100644 index 87b8343..0000000 Binary files a/src/photo/white-2.jpg and /dev/null differ diff --git a/src/server.js b/src/server.js index 4a2a581..4cf76d1 100644 --- a/src/server.js +++ b/src/server.js @@ -1,9 +1,35 @@ const express = require('express'); +const bodyParser = require('body-parser'); const request = require('request'); +const multer = require('multer'); +var storage = multer.diskStorage({ + destination: function (req, file, cb) { + cb(null, 'src/uploads/') + }, + filename: function (req, file, cb) { + console.log(file); + var ext = ""; + if(file.originalname.includes(".")){ + ext = "." + file.originalname.split(".")[1]; + console.log(ext); + } + console.log(ext); + return cb(null, 'img-' + Date.now()+ext) + } + }) +var upload = multer({ storage: storage }) + const port = 80; const server = express(); +// server.use(bodyParser.json()); +server.use(bodyParser.urlencoded({ extended: true })); +server.use(function (req, res, next) { + console.debug("express:", req.method, req.originalUrl); + next() +}) + function listen(){ server.listen(port, () => console.info(`Listening on port ${port}!`)); @@ -11,6 +37,8 @@ function listen(){ function setUpRoutes(models){ server.get('/', (req, res) => res.sendFile(__dirname + "/html/index.html")) + server.get('/index', (req, res) => res.sendFile(__dirname + "/html/index.html")) + server.get('/admin', (req, res) => res.sendFile(__dirname + "/html/admin.html")); server.get('/bread', (req, res) => res.sendFile(__dirname + "/html/bread.html")); server.get('/essay', (req, res) => res.sendFile(__dirname + "/html/essay.html")); server.get('/snake', (req, res) => res.sendFile(__dirname + "/html/snake.html")); @@ -18,15 +46,13 @@ function setUpRoutes(models){ request(`http://localhost:8000?${req.url.split("?")[1]}`, function(error, response, body) { }); }) - server.get('/:type/posts.json', async (req, res, next) => { + server.get('/posts/:type', async (req, res, next) => { try { const { type } = req.params; - console.log(type); - console.log(req.params); - var posts = await models.posts.findAll({where: { type: type }}); + var posts = await models.posts.findAll({where: { type: type }, order: [['createdAt', 'DESC']]}); posts = posts.map(x => x.get({ plain: true })); for (const post of posts) { - const images = await models.pictures.findAll({ attributes: ["source"], where: { postId: post.id } }).map(x => x.source); + const images = await models.pictures.findAll({ attributes: ["source"], where: { postId: post.id }}).map(x => x.source); post.images = images; } res.status(200).send({ success: true, data: posts }); @@ -35,6 +61,23 @@ function setUpRoutes(models){ res.status(400).send({ success: false, error: e.message }); } }) + server.post('/posts', upload.array('images'), async (req, res, next) => { + try { + console.log(req.body); + + const type = req.body.type + const newPost = await models.posts.create(req.body); + req.files.forEach(async (file) => { + await models.pictures.create({"source": "uploads/"+file.filename, "postId": newPost.id}); + console.log("uploaded ", file.path); + }) + console.log(newPost); + res.redirect(`/${type}`); + next(); + } catch (e) { + res.status(400).send({ success: false, error: e.message }); + } + }) server.get('/favicon.ico', (req, res) => res.sendFile(__dirname + "/icon/favicon.ico")) @@ -42,8 +85,12 @@ function setUpRoutes(models){ res.sendFile(__dirname + "/css/"+req.params.id); }); server.get('/photo/:id', (req, res) => { + // res.setHeater("Content-Type", "image") res.sendFile(__dirname + "/photo/"+req.params.id); }); + server.get('/uploads/:id', (req, res) => { + res.sendFile(__dirname + "/uploads/"+req.params.id); + }); server.get('/essay/:id', (req, res) => { res.sendFile(__dirname + "/html/essay/"+req.params.id); }); -- cgit v1.2.3