-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the way the world ends. Not with a bang but a whimper.
- Loading branch information
1 parent
7c64635
commit 0e4298c
Showing
3 changed files
with
100 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
}); | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters