Browse Source

Created basic functionality for sending videos to the client after they have logged in.

pull/6/head
jrtechs 5 years ago
parent
commit
d99baac6da
3 changed files with 61 additions and 0 deletions
  1. +0
    -0
      html/videos.html
  2. +3
    -0
      html/watch.html
  3. +58
    -0
      server.js

+ 0
- 0
html/videos.html View File


+ 3
- 0
html/watch.html View File

@ -0,0 +1,3 @@
<video id="videoPlayer" width="768" height="432" controls>
<source src="http://localhost:5000/video" type="video/mp4">
</video>

+ 58
- 0
server.js View File

@ -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}!`));

Loading…
Cancel
Save