diff --git a/routes/create/reply.js b/routes/create/reply.js index 8896fad..5ef3043 100644 --- a/routes/create/reply.js +++ b/routes/create/reply.js @@ -1,76 +1,88 @@ const Reply = require(_base + 'models/reply'); +const Thread = require(_base + 'models/thread'); const fs = require('fs'); const path = 'uploads/'; const multer = require('multer'); -const upload = multer({ dest: './uploads/'}); +const upload = multer({dest: './uploads/'}); const striptags = require('striptags'); module.exports = { - '/create/reply' : { - methods: ['post'], - middleware: [upload.single("attachment")], - fn: function (req, res, next) { - console.log(req.file); - let threadId = req.body.threadId, - attachment = req.file, - ip = req.connection.remoteAddress, - content = req.body.content; + '/create/reply': { + methods: ['post'], + middleware: [upload.single("attachment")], + fn: function (req, res, next) { + let allowedExt = ['png', 'jpg', 'jpeg', 'webm']; + let threadId = req.body.threadId, + attachment = req.file, + ip = req.connection.remoteAddress, + content = req.body.content; - if(!allowedExt.includes(attachment.originalname.split('.').pop())) { - attachment = null; - } - - //String formatting (Yes, I know this is janky) - content = striptags(content); - let contentLines = content.split(new RegExp('\r?\n', 'g')); - let contentFinal = ""; - for(let i=0; i< contentLines.length; i++) { - lineContent = contentLines[i].replace(new RegExp('\\>'), ">"); - if(lineContent.includes(">")) { - contentLines[i] = lineContent + ""; - } + Thread.findById(threadId, function (err, result) { + if (err) { + return next(err); + } else if (!result) { + return next(new Error('No thread with that ID found!')); + } - contentFinal += contentLines[i]; - if(i+1'), ">"); + if (lineContent.includes(">")) { + contentLines[i] = lineContent + ""; + } - reply.save(function(err) { - console.log(req.files); - if(err) { - return next(err); + contentFinal += contentLines[i]; + if (i + 1 < contentLines.length) { + contentFinal += "
"; + } } + content = contentFinal; + + let reply = new Reply({threadId: threadId, ip: ip, content: content}); + let target_path; if (attachment) { - //Save file to fs - fs.rename(attachment.path, target_path, function(err) { - if(err) { - return next(err); - } + target_path = path + attachment.filename + "." + attachment.originalname.split('.').pop(); + reply.attachment_path = target_path; + reply.attachment_name = attachment.originalname; + } + + reply.save(function (err) { + console.log(req.files); + if (err) { + return next(err); + } - fs.unlink(attachment.path, function() { - if(err) { + if (attachment) { + //Save file to fs + fs.rename(attachment.path, target_path, function (err) { + if (err) { return next(err); } - res.json({ result: { threadId: threadId, attachment_path: target_path, attachment_name: attachment.originalname, ip: ip, content: content } }); + fs.unlink(attachment.path, function () { + if (err) { + return next(err); + } + + // res.json({ result: { threadId: threadId, attachment_path: target_path, attachment_name: attachment.originalname, ip: ip, content: content } }); + res.redirect('/' + result.boardId + '/thread/' + threadId); + }); }); - }); - } else { - res.json({ result: { threadId: threadId, ip: ip, content: content } }); - } + } else { + // res.json({ result: { threadId: threadId, ip: ip, content: content } }); + res.redirect('/' + result.boardId + '/thread/' + threadId); + } + }); }); - } - } + } + } }; diff --git a/routes/create/thread.js b/routes/create/thread.js index e1e84a6..00d5c89 100644 --- a/routes/create/thread.js +++ b/routes/create/thread.js @@ -10,7 +10,7 @@ module.exports = { methods: ['post'], middleware: [upload.single("attachment")], fn: function (req, res, next) { - let allowedExt = ['png', 'jpg', 'jpeg', 'webm']; + let allowedExt = ['png', 'jpg', 'jpeg', 'webm']; let name = req.body.name, board = req.body.board, attachment = req.file, @@ -19,28 +19,28 @@ module.exports = { content = req.body.content, title = req.body.title + ":"; - if(!allowedExt.includes(attachment.originalname.split('.').pop())) { - attachment = null; - } - - //String formatting (Yes, I know this is janky) - content = striptags(content); - let contentLines = content.split(new RegExp('\r?\n', 'g')); - let contentFinal = ""; - for(let i=0; i< contentLines.length; i++) { - lineContent = contentLines[i].replace(new RegExp('\\>'), ">"); - if(lineContent.includes(">")) { - contentLines[i] = lineContent + ""; - } + if (attachment && !allowedExt.includes(attachment.originalname.split('.').pop())) { + attachment = null; + } - contentFinal += contentLines[i]; - if(i+1'), ">"); + if (lineContent.includes(">")) { + contentLines[i] = lineContent + ""; + } + + contentFinal += contentLines[i]; + if (i + 1 < contentLines.length) { + contentFinal += "
"; + } + } + content = contentFinal; + + Thread.findOne({title: title}, function (err, result) { if (err) { return next(err); } @@ -57,9 +57,9 @@ module.exports = { title: title }); - console.log(content); - - let target_path; + console.log(content); + + let target_path; if (attachment) { target_path = path + attachment.filename + "." + attachment.originalname.split('.').pop(); thread.attachment_path = target_path; diff --git a/src/components/Thread.vue b/src/components/Thread.vue index 0d9e4ad..e95a71f 100644 --- a/src/components/Thread.vue +++ b/src/components/Thread.vue @@ -29,7 +29,7 @@ class="name">{{ thread.name }}
{{ new Date(thread.timeStamp) }} No. {{ thread._id }}
@@ -47,7 +47,7 @@
{{ reply.content }} @@ -73,7 +73,7 @@ Content - + File @@ -248,6 +248,7 @@ replies: [], isMod: false, replyBoxShown: false, + replyContent: '', boardList: "t / r / a / p" } }, @@ -339,6 +340,11 @@ let vm = this; vm.replyBoxShown = !vm.replyBoxShown; }, + appendUserIdToReplyContent(id) { + let vm = this; + vm.replyContent += ">>" + id; + vm.replyBoxShown = true; + }, compileBoardList() { let vm = this; _api.boards(function (err, res) {