fix(video): поддержка Range на dev-сервере + Cues в начале webm (seek в плеере)
This commit is contained in:
1 parent
3d5fbe45ee
commit
8c34d3fa97
3 files changed
+33
-4
No files matched your search
+30
-4
@@ -237,11 +237,37 @@ function serveStatic(req, res) {
|
||||
}
|
||||
const ext = path.extname(filePath).toLowerCase();
|
||||
const mime = MIME[ext] || "application/octet-stream";
|
||||
fs.readFile(filePath, (e2, data) => {
|
||||
if (e2) { res.writeHead(500); res.end("Error"); return; }
|
||||
res.writeHead(200, { "Content-Type": mime });
|
||||
res.end(data);
|
||||
const total = stat.size;
|
||||
const range = req.headers.range;
|
||||
|
||||
// Поддержка Range-запросов — нужна браузеру для перемотки видео/аудио
|
||||
if (range) {
|
||||
const m = /^bytes=(\d*)-(\d*)$/.exec(range);
|
||||
if (m) {
|
||||
let start = m[1] ? parseInt(m[1], 10) : 0;
|
||||
let end = m[2] ? parseInt(m[2], 10) : total - 1;
|
||||
if (isNaN(start) || isNaN(end) || start > end || end >= total) {
|
||||
res.writeHead(416, { "Content-Range": `bytes */${total}` });
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
res.writeHead(206, {
|
||||
"Content-Type": mime,
|
||||
"Content-Range": `bytes ${start}-${end}/${total}`,
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": end - start + 1,
|
||||
});
|
||||
fs.createReadStream(filePath, { start, end }).pipe(res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
res.writeHead(200, {
|
||||
"Content-Type": mime,
|
||||
"Accept-Ranges": "bytes",
|
||||
"Content-Length": total,
|
||||
});
|
||||
fs.createReadStream(filePath).pipe(res);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user