diff --git a/README.md b/README.md index 3ea54f7..d593374 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/components/commentHandler.js b/components/commentHandler.js index 3eca2f9..5ada681 100644 --- a/components/commentHandler.js +++ b/components/commentHandler.js @@ -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, @@ -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, diff --git a/components/frontEndHandler.js b/components/frontEndHandler.js index 221e28d..fa5434e 100644 --- a/components/frontEndHandler.js +++ b/components/frontEndHandler.js @@ -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) => { @@ -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); @@ -111,4 +116,4 @@ function frontEndHandler(app, __dirname) { }); } -module.exports = {frontEndHandler} \ No newline at end of file +module.exports = { frontEndHandler } \ No newline at end of file diff --git a/components/idGenerator.js b/components/idGenerator.js index 9352d36..6e7fab9 100644 --- a/components/idGenerator.js +++ b/components/idGenerator.js @@ -10,4 +10,12 @@ function makeid(length) { return result; } -module.exports = { makeid }; \ No newline at end of file +function checkForEmptyString(string) { + if (!string.replace(/\s/g, '').length) { + return true; + } else { + return false; + } +} + +module.exports = { makeid, checkForEmptyString }; \ No newline at end of file diff --git a/components/uploadHandler.js b/components/uploadHandler.js index 4f9fc4e..127b045 100644 --- a/components/uploadHandler.js +++ b/components/uploadHandler.js @@ -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) { @@ -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}`, }; diff --git a/components/voteVideo.js b/components/voteVideo.js new file mode 100644 index 0000000..031e2fb --- /dev/null +++ b/components/voteVideo.js @@ -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 } \ No newline at end of file diff --git a/nexdex.js b/nexdex.js index 160d8db..5958969 100644 --- a/nexdex.js +++ b/nexdex.js @@ -2,6 +2,7 @@ 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; @@ -9,6 +10,7 @@ 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}`); diff --git a/pages/style/style.css b/pages/style/style.css index 2814c64..e8fdb65 100644 --- a/pages/style/style.css +++ b/pages/style/style.css @@ -31,6 +31,8 @@ fieldset button { video { width: 100%; aspect-ratio: 16/9; + background: black; + border: rgb(192, 192, 192) groove 2px; } .video { @@ -57,4 +59,8 @@ iframe { border: none; width: 100%; height: 157px; +} + +.votefieldset button { + width: 26% !important; } \ No newline at end of file diff --git a/pages/watch.html b/pages/watch.html index 290f519..8e5c425 100644 --- a/pages/watch.html +++ b/pages/watch.html @@ -21,6 +21,14 @@