Skip to content

Commit

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

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

if (checkForEmptyString(theComment)) return;
if (checkForEmptyString(theWriter)) {
theWriter = 'Anonymous'
}

const metadata = {
user: theWriter,
text: theComment,
Expand All @@ -34,7 +39,12 @@ function commentHandler(app, __dirname) {
const uniqueVideoID = req.query.vidlink;
const uniqueCommentID = req.query.commentid;
const theComment = sanitizeHtml(req.query.text);
const theWriter = sanitizeHtml(req.query.username);
let theWriter = sanitizeHtml(req.query.username);

if (checkForEmptyString(theComment)) return;
if (checkForEmptyString(theWriter)) {
theWriter = 'Anonymous'
}

const metadata = {
user: theWriter,
Expand Down
17 changes: 11 additions & 6 deletions components/frontEndHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const multer = require('multer');
const path = require('path');
const fs = require('fs');
const url = require('url');
const {makeid} = require('./idGenerator')
const { makeid, checkForEmptyString } = require('./idGenerator')

function frontEndHandler(app, __dirname) {
app.get('/', (req, res) => {
Expand All @@ -18,10 +18,15 @@ function frontEndHandler(app, __dirname) {
const files = await fs.readdir("uploads");
const total = [];
const promises = files.map(async (id) => {
const data = await fs.readFile(__dirname + "/uploads/" + id + "/metadata.json");
const parsedData = JSON.parse(data);
parsedData.id = id;
total.push(parsedData);
try {
const data = await fs.readFile(__dirname + "/uploads/" + id + "/metadata.json");
const parsedData = JSON.parse(data);
parsedData.id = id;
total.push(parsedData);
} catch (error) {
console.error('Corrupted video! Deleting...')
fs.rm(__dirname + "/uploads/" + id, { recursive: true, force: true })
}
});

await Promise.all(promises);
Expand Down Expand Up @@ -111,4 +116,4 @@ function frontEndHandler(app, __dirname) {
});
}

module.exports = {frontEndHandler}
module.exports = { frontEndHandler }
10 changes: 9 additions & 1 deletion components/idGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ function makeid(length) {
return result;
}

module.exports = { makeid };
function checkForEmptyString(string) {
if (!string.replace(/\s/g, '').length) {
return true;
} else {
return false;
}
}

module.exports = { makeid, checkForEmptyString };
14 changes: 11 additions & 3 deletions components/uploadHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const multer = require('multer');
const path = require('path');
const fs = require('fs');
const url = require('url');
const {makeid} = require('./idGenerator')
const {makeid,checkForEmptyString} = require('./idGenerator')
const sanitizeHtml = require('sanitize-html')

function handleUpload(app, __dirname) {
Expand All @@ -23,10 +23,18 @@ function handleUpload(app, __dirname) {

app.post('/upload', upload.single('video'), (req, res) => {
const uniqueFolder = req.file.destination.split(path.sep).pop();
let videoTitle = req.body.title
let videoDescription = req.body.description

if (checkForEmptyString(videoTitle)) {
videoTitle = 'Untitled'
} if (checkForEmptyString(videoDescription)) {
videoDescription = ''
}

const metadata = {
title: sanitizeHtml(req.body.title) || 'Untitled',
description: sanitizeHtml(req.body.description) || '',
title: sanitizeHtml(videoTitle),
description: sanitizeHtml(videoDescription),
filename: req.file.originalname,
path: `/uploads/${uniqueFolder}/${req.file.originalname}`,
};
Expand Down
40 changes: 40 additions & 0 deletions components/voteVideo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const url = require('url');
const { makeid, checkForEmptyString } = require('./idGenerator')
const sanitizeHtml = require('sanitize-html')

function voteVideo(app, __dirname) {
app.get('/vote', (req, res) => {
try {
const uniqueVideoID = req.query.vidlink;
let theVote = sanitizeHtml(req.query.vote);

if (theVote == 'up') {
theVote = 1
} else if (theVote == 'down') {
theVote = -1
} else {
return
}

const videoFilePath = path.join(__dirname, 'uploads', uniqueVideoID, 'metadata.json');
fs.readFile((videoFilePath), (err, data) => {
const videoData = JSON.parse(data);
if (!videoData.votes) {
videoData.votes = 0
}
videoData.votes += theVote
fs.writeFileSync(videoFilePath, JSON.stringify(videoData, null, 2));

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

module.exports = { voteVideo }
2 changes: 2 additions & 0 deletions nexdex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ const express = require('express');
const {handleUpload} = require('./components/uploadHandler')
const {commentHandler} = require('./components/commentHandler')
const {frontEndHandler} = require('./components/frontEndHandler')
const {voteVideo} = require('./components/voteVideo')

const app = express();
const port = 3000;

handleUpload(app, __dirname)
commentHandler(app, __dirname)
frontEndHandler(app, __dirname)
voteVideo(app, __dirname)

app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
Expand Down
6 changes: 6 additions & 0 deletions pages/style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ fieldset button {
video {
width: 100%;
aspect-ratio: 16/9;
background: black;
border: rgb(192, 192, 192) groove 2px;
}

.video {
Expand All @@ -57,4 +59,8 @@ iframe {
border: none;
width: 100%;
height: 157px;
}

.votefieldset button {
width: 26% !important;
}
59 changes: 57 additions & 2 deletions pages/watch.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ <h1>[nexdex]</h1>
<span id="desc"></span>
</fieldset>
<br><br>
<fieldset class="votefieldset">
<b>Vote</b>
<br>
<button onclick="downvote()"><</button>
<button id="upvotespan">0</button>
<button onclick="upvote()">></button>
</fieldset>
<br><br>
<fieldset>
<b>Write a comment</b>
<br>
Expand All @@ -42,6 +50,8 @@ <h1>[nexdex]</h1>
const Query = window.location.search;
const urlParams = new URLSearchParams(Query);
const videosrc = urlParams.get('v');
let videometadata
let voted = false

async function getVideos() {
document.getElementById('video').src = 'embed?v=' + videosrc
Expand All @@ -52,6 +62,9 @@ <h1>[nexdex]</h1>

document.getElementById('title').innerHTML = data.title
document.getElementById('desc').innerHTML = data.description
document.getElementById('upvotespan').innerText = data.votes || 0

videometadata = data
}

function comment(text, user) {
Expand All @@ -75,6 +88,50 @@ <h1>[nexdex]</h1>
}
}

function upvote() {
if (voted) return;
voted = true

const url = '/vote?' + new URLSearchParams({
vidlink: videosrc,
vote: 'up'
});

fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})

if (!videometadata.votes) {
videometadata.votes = 0
}
document.getElementById('upvotespan').innerText = videometadata.votes + 1
}

function downvote() {
if (voted) return;
voted = true

const url = '/vote?' + new URLSearchParams({
vidlink: videosrc,
vote: 'down'
});

fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})

if (!videometadata.votes) {
videometadata.votes = 0
}
document.getElementById('upvotespan').innerText = videometadata.votes - 1
}

async function reply(text, user, commentid) {
if (text !== undefined && user !== undefined) {
const url = '/reply?' + new URLSearchParams({
Expand All @@ -84,7 +141,6 @@ <h1>[nexdex]</h1>
commentid: commentid
});

console.log(url)
await fetch(url, {
method: 'GET',
headers: {
Expand Down Expand Up @@ -164,7 +220,6 @@ <h1>[nexdex]</h1>
reply(commentinput.value, usernameinput.value, element.id)
commentinput.value = ''
usernameinput.value = ''
commentbutton.innerHTML = 'Reply sent!'
}

if (element.replies) {
Expand Down

0 comments on commit 0c7bcb2

Please sign in to comment.