From d99baac6da5ca84a4fb29e0b53b944997c642ba5 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 2 Feb 2019 15:42:03 -0500 Subject: [PATCH] Created basic functionality for sending videos to the client after they have logged in. --- html/videos.html | 0 html/watch.html | 3 +++ server.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 html/videos.html create mode 100644 html/watch.html diff --git a/html/videos.html b/html/videos.html new file mode 100644 index 0000000..e69de29 diff --git a/html/watch.html b/html/watch.html new file mode 100644 index 0000000..0c7dcfc --- /dev/null +++ b/html/watch.html @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/server.js b/server.js index 6c36891..456eeba 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,10 @@ const fileIO = require('./fileIO'); const userUtils = require('./user.js'); +const path = require('path'); + + +const fs = require('fs'); const app = express(); @@ -74,4 +78,58 @@ app.post('/login', function(request, result) result.redirect('/'); }); + +app.get('/videos', (req, res) => renderHTML(req, res, "videos.html", null)); +app.get('/watch', (req, res) => renderHTML(req, res, "watch.html", null)); + + +app.get('/video', function(request, result) +{ + if(request.session.login === true) + { + const path = '/home/jeff/public/CheckerMoves.mp4'; + const stat = fs.statSync(path); + const fileSize = stat.size; + const range = request.headers.range; + + if (range) + { + const parts = range.replace(/bytes=/, "").split("-"); + const start = parseInt(parts[0], 10); + const end = parts[1] + ? parseInt(parts[1], 10) + : fileSize-1; + + const chunksize = (end-start)+1; + const file = fs.createReadStream(path, {start, end}); + const head = + { + 'Content-Range': `bytes ${start}-${end}/${fileSize}`, + 'Accept-Ranges': 'bytes', + 'Content-Length': chunksize, + 'Content-Type': 'video/mp4', + }; + + result.writeHead(206, head); + file.pipe(result); + } + else + { + const head = + { + 'Content-Length': fileSize, + 'Content-Type': 'video/mp4', + }; + + result.writeHead(200, head); + fs.createReadStream(path).pipe(result); + } + } + else + { + result.status(401); + result.send('None shall pass'); + } +}); + app.listen(config.port, () => console.log(`App listening on port ${config.port}!`)); \ No newline at end of file