-
Notifications
You must be signed in to change notification settings - Fork 33
/
video-file-server.js
68 lines (57 loc) · 2.13 KB
/
video-file-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const express = require('express');
const path = require('path');
const fs = require('fs');
var router = express.Router();
const storage = require('./storage.json');
router.get('/api/*.:ext', (req, res, next) => {
// const location = req.params.location;
// const year = req.params.year;
// const month = req.params.month;
// const day = req.params.day;
// const filename = req.params.filename;
const ext = req.params.ext;
const parts = `${req.params['0']}.${ext}`.split('/').filter(x => x.length > 0);
// https://stackoverflow.com/a/24977085/10159640
var filepath = path.join(storage.rootpath, ...parts);
fs.stat(filepath, function (err, stats) {
if (err) {
if (err.code === 'ENOENT') {
console.log('file not found', err);
// 404 Error if file not found
return res.status(404).send();
}
console.log('file stat error', err);
return res.send(err);
}
var range = req.headers.range;
if (!range) {
// 416 Wrong range
console.log('no req range header')
return res.status(416).send();
}
var videoSize = stats.size;
// Parse Range
// Example: "bytes=32324-"
const CHUNK_SIZE = 10 ** 6; // 1MB
const start = Number(range.replace(/\D/g, ""));
const end = Math.min(start + CHUNK_SIZE, videoSize - 1);
// Create headers
const contentLength = end - start + 1;
const headers = {
"Content-Range": `bytes ${start}-${end}/${videoSize}`,
"Accept-Ranges": "bytes",
"Content-Length": contentLength,
"Content-Type": `video/${ext}`,
};
// HTTP Status 206 for Partial Content
res.writeHead(206, headers);
var stream = fs.createReadStream(filepath, { start: start, end: end })
.on("open", function () {
stream.pipe(res);
}).on("error", function (err) {
console.log('stream error', err)
res.send(err);
});
});
})
module.exports = router;