Skip to content

Commit

Permalink
This is the way the world ends. Not with a bang but a whimper.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewyatesg committed Apr 8, 2018
1 parent 7c64635 commit 0e4298c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 82 deletions.
120 changes: 66 additions & 54 deletions routes/create/reply.js
Original file line number Diff line number Diff line change
@@ -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('\\>'), "<span style='color: #789922;'>>");
if(lineContent.includes("<span style='color: #789922;'>>")) {
contentLines[i] = lineContent + "</span>";
}
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<contentLines.length) {
contentFinal += "<br>";
}
}
content = contentFinal;

let reply = new Reply({ threadId: threadId, ip: ip, content: content });
if (attachment && !allowedExt.includes(attachment.originalname.split('.').pop())) {
attachment = null;
}

if (attachment) {
let target_path = path + attachment.filename + "." + attachment.originalname.split('.').pop();
reply.attachment_path = target_path;
reply.attachment_name = attachment.originalname;
}
//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('\\>'), "<span style='color: #789922;'>>");
if (lineContent.includes("<span style='color: #789922;'>>")) {
contentLines[i] = lineContent + "</span>";
}

reply.save(function(err) {
console.log(req.files);
if(err) {
return next(err);
contentFinal += contentLines[i];
if (i + 1 < contentLines.length) {
contentFinal += "<br>";
}
}
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);
}

});
});
}
}
}
}
};
50 changes: 25 additions & 25 deletions routes/create/thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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('\\>'), "<span style='color: #789922;'>>");
if(lineContent.includes("<span style='color: #789922;'>>")) {
contentLines[i] = lineContent + "</span>";
}
if (attachment && !allowedExt.includes(attachment.originalname.split('.').pop())) {
attachment = null;
}

contentFinal += contentLines[i];
if(i+1<contentLines.length) {
contentFinal += "<br>";
}
}
content = contentFinal;

Thread.findOne({ title: title }, function (err, result) {
//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('\\>'), "<span style='color: #789922;'>>");
if (lineContent.includes("<span style='color: #789922;'>>")) {
contentLines[i] = lineContent + "</span>";
}

contentFinal += contentLines[i];
if (i + 1 < contentLines.length) {
contentFinal += "<br>";
}
}
content = contentFinal;

Thread.findOne({title: title}, function (err, result) {
if (err) {
return next(err);
}
Expand All @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/components/Thread.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class="name">{{ thread.name }}</span> </span> <span class="dateTime"
>{{ new Date(thread.timeStamp) }}</span>
<span class="postNum"> <a :href="'#' + thread._id" title="Link to this post">No.</a> <a
href="{JS FOR APPENDING ID TO REPLY}"
href="#" v-on:click="appendUserIdToReplyContent(thread._id)"
title="Reply to this post">{{ thread._id }}</a> </span> <span v-if="isMod"><button v-on:click="ban(thread)">Ban</button></span></div>
<blockquote class="postMessage" v-html="thread.content">
</blockquote>
Expand All @@ -47,7 +47,7 @@
<div class="postInfo"><span class="nameBlock"> <span class="name">{{ reply.name }}</span> </span> <span
class="dateTime">{{ new Date(reply.time) }}</span> <span
class="postNum"> <a :href="'#' + reply_id" title="Link to this post">No.</a> <a
href="{JS FOR APPENDING ID TO REPLY}" title="Reply to this post">{{ reply._id }}</a> </span> <span v-if="isMod"><button v-on:click="ban(reply)">Ban</button> <button v-on:click="deleteReply(reply)">Delete</button></span>
href="#" v-on:click="appendUserIdToReplyContent(reply._id)" title="Reply to this post">{{ reply._id }}</a> </span> <span v-if="isMod"><button v-on:click="ban(reply)">Ban</button> <button v-on:click="deleteReply(reply)">Delete</button></span>
</div>
<blockquote
class="postMessage"> {{ reply.content }}
Expand All @@ -73,7 +73,7 @@
</tr>
<tr data-type="Content">
<td>Content</td>
<td><textarea name="content" cols="48" rows="4" wrap="soft" tabindex="4"></textarea></td>
<td><textarea v-model="replyContent" name="content" cols="48" rows="4" wrap="soft" tabindex="4"></textarea></td>
</tr>
<tr data-type="File">
<td>File</td>
Expand Down Expand Up @@ -248,6 +248,7 @@
replies: [],
isMod: false,
replyBoxShown: false,
replyContent: '',
boardList: "t / r / a / p"
}
},
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 0e4298c

Please sign in to comment.