-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
118 lines (107 loc) · 3.3 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Created by alex on 15.12.2016.
*/
'use strict';
const express = require('express');
const path = require('path');
const fs = require('graceful-fs');
const shortid = require('shortid');
const multer = require('multer');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.set('STORAGE_DIR', path.resolve(__dirname, 'storage'));
app.set('PORT', 8000);
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const category = req.body.category;
let dest = app.get('STORAGE_DIR');
if (category) {
dest = path.resolve(app.get('STORAGE_DIR'), category);
if (!fs.existsSync(dest)) fs.mkdirSync(dest)
}
cb(null, dest)
},
filename: function (req, file, cb) {
cb(null, shortid.generate() + path.extname(file.originalname))
}
});
const upload = multer({storage});
app.use(cors());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(bodyParser.raw());
app.post('/upload', upload.single('video'),
function (req, res, next) {
try {
res.json({id: path.relative(app.get('STORAGE_DIR'), req.file.path).replace(/\\+/g, '/')});
} catch (e) {
next(e);
}
}
);
app.param('category', function (req, res, next, category) {
req.category = category;
next();
});
app.param('id', function (req, res, next, id) {
if (req.category) {
id = req.category + "/" + id;
}
var file = path.resolve(app.get('STORAGE_DIR'), id);
fs.stat(file, function (err, stats) {
if (err) {
if (err.code === 'ENOENT') {
// 404 Error if file not found
return res.sendStatus(404);
}
res.end(err);
}
req.file = {
id,
path: file,
stats
}
next()
})
});
app.get('/view/:id', function (req, res, next) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(`<video src="/stream/${req.file.id}" controls></video>`);
});
app.get('/view/:category/:id', function (req, res, next) {
res.writeHead(200, {"Content-Type": "text/html"});
res.end(`<video src="/stream/${req.file.id}" controls></video>`);
});
app.get('/stream/:id', stream);
app.get('/stream/:category/:id', stream);
app.use(function (err, req, res, next) {
console.log(err);
res.status(500).json({message: err.message});
});
module.exports = app;
function stream(req, res, next) {
var range = req.headers.range || "bytes=0-";
if (!range) {
// 416 Wrong range
return res.sendStatus(416);
}
var positions = range.replace(/bytes=/, "").split("-");
var start = parseInt(positions[0], 10);
var total = req.file.stats.size;
var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
var chunksize = (end - start) + 1;
res.writeHead(206, {
"Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": "video/mp4"
});
var stream = fs.createReadStream(req.file.path, {start: start, end: end})
.on("open", function () {
stream.pipe(res);
})
.on("error", function (err) {
next(err);
});
}