-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
90 lines (76 loc) · 2.42 KB
/
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const express = require('express')
const fs = require('fs')
const path = require('path')
const app = express()
const ThumbnailGenerator = require('video-thumbnail-generator').default;
app.use(express.static(path.join(__dirname, 'public')))
var template_course = path.join(__dirname + '/courseplay.html');
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.htm'))
})
app.get('/list', function (req, res) {
const directoryPath = path.join(__dirname, '/assets');
var data = {};
fs.readdirSync(directoryPath).forEach(file => {
const tg = new ThumbnailGenerator({
sourcePath: path.join(directoryPath, file),
thumbnailPath: path.join(path.join(__dirname, 'public'), 'thumbnail'),
});
tt = tg.generateOneByPercentCb(90, (err, result) => {
console.log(result);
});
if(file!="comments")
data[file] = null;
});
fs.readdirSync(path.join(path.join(__dirname, 'public'), 'thumbnail')).forEach(file => {
let f = file.split('-');
data[f[0]+".mp4"] = file;
});
console.log(data);
res.send(JSON.stringify(data));
})
app.get('/video/:name', function(req, res) {
//res.sendFile(template_course);
const path = 'assets/' + req.params.name;
console.log(path);
const stat = fs.statSync(path)
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
if(start >= fileSize) {
res.status(416).send('Requested range not satisfiable\n'+start+' >= '+fileSize);
return
}
const chunksize = (end-start)+1
const file = fs.createReadStream(path, {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(path).pipe(res)
}
})
app.get('/comments/:name', function (req, res) {
var f = req.params.name;
let rawdata = fs.readFileSync(`assets/comments/${f}.json`);
let student = JSON.parse(rawdata);
res.send(JSON.stringify(student));
})
app.listen(3000, function () {
console.log('Listening on port 3000!')
})