File streaming

This commit is contained in:
MrFry 2019-11-06 13:18:39 +01:00
parent 273b4c321b
commit b8316bb989
3 changed files with 50 additions and 43 deletions

View file

@ -49,50 +49,57 @@ app.use(bodyParser.json({
// -------------------------------------------------------------- // --------------------------------------------------------------
app.get('/*.mp4', function (req, res) { // app, '/*.mp4', 'video/mp4', 'stuff/video'
let p = req.url.replace(/%20/g, ' ') function appGetFileType (app, wildcard, contentType, pageToRender) {
let fp = p.split('?') app.get(wildcard, function (req, res) {
fp.pop() let p = req.url.replace(/%20/g, ' ')
const fpath = './public/files' + fp.join('/') let fp = p.split('?')
if (req.query.stream) { fp.pop()
const stat = fs.statSync(fpath) const fpath = './public/files' + fp.join('/')
const fileSize = stat.size if (req.query.stream) {
const range = req.headers.range const stat = fs.statSync(fpath)
if (range) { const fileSize = stat.size
const parts = range.replace(/bytes=/, '').split('-') const range = req.headers.range
const start = parseInt(parts[0], 10) if (range) {
const end = parts[1] const parts = range.replace(/bytes=/, '').split('-')
? parseInt(parts[1], 10) const start = parseInt(parts[0], 10)
: fileSize - 1 const end = parts[1]
const chunksize = (end - start) + 1 ? parseInt(parts[1], 10)
const file = fs.createReadStream(fpath, { start, end }) : fileSize - 1
const head = { const chunksize = (end - start) + 1
'Content-Range': `bytes ${start}-${end}/${fileSize}`, const file = fs.createReadStream(fpath, { start, end })
'Accept-Ranges': 'bytes', const head = {
'Content-Length': chunksize, 'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Content-Type': 'video/mp4' 'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': contentType
}
res.writeHead(206, head)
file.pipe(res)
} else {
const head = {
'Content-Length': fileSize,
'Content-Type': contentType
}
res.writeHead(200, head)
fs.createReadStream(fpath).pipe(res)
} }
res.writeHead(206, head)
file.pipe(res)
} else { } else {
const head = { logger.LogReq(req)
'Content-Length': fileSize, let fname = p.split('/')
'Content-Type': 'video/mp4' fname = fname.pop()
} res.render(pageToRender, {
res.writeHead(200, head) path: p,
fs.createReadStream(fpath).pipe(res) fname,
url,
contentType
})
} }
} else { })
logger.LogReq(req) }
let fname = p.split('/')
fname = fname.pop() appGetFileType(app, '/*.mp4', 'video/mp4', 'stuff/video')
res.render('stuff/video', { appGetFileType(app, '/*.mp3', 'audio/mpeg', 'stuff/audio')
path: p,
fname,
url
})
}
})
app.get('/*', function (req, res) { app.get('/*', function (req, res) {
let parsedUrl = req.url.replace(/%20/g, ' ') let parsedUrl = req.url.replace(/%20/g, ' ')

@ -1 +1 @@
Subproject commit cbfaffa70ceb29b4beac4b13f6cbb4b85baac8a8 Subproject commit fe74436ab119a31fdb35f7a3bbae50a572b0fb21

View file

@ -21,7 +21,7 @@
<%= fname %> <%= fname %>
</h2> </h2>
</center> </center>
<video id="videoPlayer" controls muted="muted" autoplay> <video id="videoPlayer" controls>
<source src="<%= url %><%= path %>?stream=true" type="video/mp4"> <source src="<%= url %><%= path %>?stream=true" type="video/mp4">
</video> </video>
</body> </body>