Video streaming

This commit is contained in:
MrFry 2019-10-26 20:42:34 +02:00
parent ea31d5c0f1
commit 28f65e699e
2 changed files with 68 additions and 0 deletions

View file

@ -49,6 +49,49 @@ app.use(bodyParser.json({
// --------------------------------------------------------------
app.get('/*.mp4', function (req, res) {
let p = req.url.replace(/%20/g, ' ')
let fp = p.split('?')
fp.pop()
const fpath = './public/files' + fp.join('/')
if (req.query.stream) {
const stat = fs.statSync(fpath)
const fileSize = stat.size
const range = req.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(fpath, { start, end })
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4'
}
res.writeHead(206, head)
file.pipe(res)
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': 'video/mp4'
}
res.writeHead(200, head)
fs.createReadStream(fpath).pipe(res)
}
} else {
let fname = p.split('/')
fname = fname.pop()
res.render('stuff/video', {
path: p,
fname
})
}
})
app.get('/*', function (req, res) {
let parsedUrl = req.url.replace(/%20/g, ' ')
let curr = listedFiles + '/' + parsedUrl.substring('/'.length, parsedUrl.length).split('?')[0]