mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
File streaming
This commit is contained in:
parent
273b4c321b
commit
b8316bb989
3 changed files with 50 additions and 43 deletions
|
@ -49,7 +49,9 @@ app.use(bodyParser.json({
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
|
|
||||||
app.get('/*.mp4', function (req, res) {
|
// app, '/*.mp4', 'video/mp4', 'stuff/video'
|
||||||
|
function appGetFileType (app, wildcard, contentType, pageToRender) {
|
||||||
|
app.get(wildcard, function (req, res) {
|
||||||
let p = req.url.replace(/%20/g, ' ')
|
let p = req.url.replace(/%20/g, ' ')
|
||||||
let fp = p.split('?')
|
let fp = p.split('?')
|
||||||
fp.pop()
|
fp.pop()
|
||||||
|
@ -70,14 +72,14 @@ app.get('/*.mp4', function (req, res) {
|
||||||
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
||||||
'Accept-Ranges': 'bytes',
|
'Accept-Ranges': 'bytes',
|
||||||
'Content-Length': chunksize,
|
'Content-Length': chunksize,
|
||||||
'Content-Type': 'video/mp4'
|
'Content-Type': contentType
|
||||||
}
|
}
|
||||||
res.writeHead(206, head)
|
res.writeHead(206, head)
|
||||||
file.pipe(res)
|
file.pipe(res)
|
||||||
} else {
|
} else {
|
||||||
const head = {
|
const head = {
|
||||||
'Content-Length': fileSize,
|
'Content-Length': fileSize,
|
||||||
'Content-Type': 'video/mp4'
|
'Content-Type': contentType
|
||||||
}
|
}
|
||||||
res.writeHead(200, head)
|
res.writeHead(200, head)
|
||||||
fs.createReadStream(fpath).pipe(res)
|
fs.createReadStream(fpath).pipe(res)
|
||||||
|
@ -86,13 +88,18 @@ app.get('/*.mp4', function (req, res) {
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
let fname = p.split('/')
|
let fname = p.split('/')
|
||||||
fname = fname.pop()
|
fname = fname.pop()
|
||||||
res.render('stuff/video', {
|
res.render(pageToRender, {
|
||||||
path: p,
|
path: p,
|
||||||
fname,
|
fname,
|
||||||
url
|
url,
|
||||||
|
contentType
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
appGetFileType(app, '/*.mp4', 'video/mp4', 'stuff/video')
|
||||||
|
appGetFileType(app, '/*.mp3', 'audio/mpeg', 'stuff/audio')
|
||||||
|
|
||||||
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
|
|
@ -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>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue