Skip to content

Commit

Permalink
replies and html sanitation
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobuxstation committed Dec 13, 2023
1 parent bee3760 commit 3228c9b
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Keep an eye out for these upcoming features that will soon be available on nexde
- [x] Video viewer
- [x] Upload video form
- [x] Comments
- [ ] Replies
- [X] Replies
- [ ] Reactions (Upvote, Downvote, Report)
- [ ] Custom video player (Captions, Video quality)
- [ ] Video search
Expand Down
37 changes: 34 additions & 3 deletions components/commentHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ const multer = require('multer');
const path = require('path');
const fs = require('fs');
const url = require('url');
const {makeid} = require('./idGenerator')
const { makeid } = require('./idGenerator')
const sanitizeHtml = require('sanitize-html')

function commentHandler(app, __dirname) {
app.get('/comment', (req, res) => {
try {
const uniqueVideoID = req.query.vidlink;
const theComment = req.query.text;
const theWriter = req.query.username;
const theComment = sanitizeHtml(req.query.text);
const theWriter = sanitizeHtml(req.query.username);
const uniqueCommentID = makeid(16)

const metadata = {
user: theWriter,
text: theComment,
replies: []
};

const metadataPath = path.join(__dirname, 'uploads', uniqueVideoID, 'comment', `${uniqueCommentID}.json`);
Expand All @@ -27,6 +29,35 @@ function commentHandler(app, __dirname) {
}
});

app.get('/reply', (req, res) => {
try {
const uniqueVideoID = req.query.vidlink;
const uniqueCommentID = req.query.commentid;
const theComment = sanitizeHtml(req.query.text);
const theWriter = sanitizeHtml(req.query.username);

const metadata = {
user: theWriter,
text: theComment
};

const commentFilePath = path.join(__dirname, 'uploads', uniqueVideoID, 'comment', `${uniqueCommentID}`);
fs.readFile((commentFilePath), (err, data) => {
const commentData = JSON.parse(data);
if (!commentData.replies) {
commentData.replies = []
}
commentData.replies.push(metadata);
fs.writeFileSync(commentFilePath, JSON.stringify(commentData, null, 2));

res.json({ message: 'Reply sent successfully!' });
})
} catch (error) {
console.error(error)
res.json({ message: 'Internal Server Error' });
}
});

app.get('/comments', async (req, res) => {
const fs = require('fs').promises;
const regularfs = require('fs');
Expand Down
5 changes: 3 additions & 2 deletions components/uploadHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const fs = require('fs');
const url = require('url');
const {makeid} = require('./idGenerator')
const sanitizeHtml = require('sanitize-html')

function handleUpload(app, __dirname) {
const storage = multer.diskStorage({
Expand All @@ -24,8 +25,8 @@ function handleUpload(app, __dirname) {
const uniqueFolder = req.file.destination.split(path.sep).pop();

const metadata = {
title: req.body.title || 'Untitled',
description: req.body.description || '',
title: sanitizeHtml(req.body.title) || 'Untitled',
description: sanitizeHtml(req.body.description) || '',
filename: req.file.originalname,
path: `/uploads/${uniqueFolder}/${req.file.originalname}`,
};
Expand Down
187 changes: 185 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "AGPL-3.0",
"dependencies": {
"express": "^4.18.2",
"multer": "^1.4.5-lts.1"
"multer": "^1.4.5-lts.1",
"sanitize-html": "^2.11.0"
}
}
4 changes: 2 additions & 2 deletions pages/submit.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
</style>

<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="video" id="video" accept="video/*" required>
<input type="file" name="video" id="video" accept="video/mp4" required>
<br>
<input type="text" name="title" id="title" placeholder="title" required>
<br>
<textarea name="description" id="description" placeholder="description" rows="4"></textarea>
<br>
<button type="submit" onclick="this.innerText = 'Uploading Video...'">Upload Video</button>
<button type="submit" onclick="this.innerHTML = 'Uploading Video...'">Upload Video</button>
</form>
Loading

0 comments on commit 3228c9b

Please sign in to comment.