-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
185 lines (152 loc) · 4.45 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* mediaserver module for node.js
*
* MIT license, Oguz Bastemur 2014-2018
*/
var fs = require('fs'),
exts = require('./libs/exts'),
pathModule = require('path');
var pipe_extensions = {};
var pipe_extension_id = 0;
var shared = {};
var fileInfo = function (path) {
if (path) {
if (!exports.noCache && shared[path]) {
return shared[path];
}
else {
if (!fs.existsSync(path)) {
return null;
}
var stat = fs.statSync(path);
if (!exports.noCache)
shared[path] = stat.size;
return stat.size;
}
}
return 0;
};
// set this to true for development mode
exports.noCache = false;
exports.mediaTypes = exts;
var getRange = function (req, total) {
var range = [0, total, 0];
var rinfo = req.headers ? req.headers.range : null;
if (rinfo) {
var rloc = rinfo.indexOf('bytes=');
if (rloc >= 0) {
var ranges = rinfo.substr(rloc + 6).split('-');
try {
range[0] = parseInt(ranges[0]);
if (ranges[1] && ranges[1].length) {
range[1] = parseInt(ranges[1]);
range[1] = range[1] < 16 ? 16 : range[1];
}
} catch (e) {}
}
if (range[1] == total)
range[1]--;
range[2] = total;
}
return range;
};
var isString = function (str) {
if (!str) return false;
return (typeof str == 'string' || str instanceof String);
};
exports.pipe = function (req, res, path, type, opt_cb) {
if (!isString(path)) {
throw new TypeError("path must be a string value");
}
var total = fileInfo(path);
if (total == null) {
res.end(path + " not found");
return false;
}
var range = getRange(req, total);
var ext = pathModule.extname(path).toLowerCase();
if (!type && ext && ext.length) {
type = exts[ext];
}
if (type && type.length && type[0] == '.') {
ext = type;
type = exts[type];
}
if (!type || !type.length) {
res.write("Media format not found for " + pathModule.basename(path));
} else {
var file = fs.createReadStream(path, {start: range[0], end: range[1]});
var cleanupFileStream = function() {
file.close();
}
// the event emitted seems to change based on version of node.js
// 'close' is fired as of v6.11.5
res.on('close', cleanupFileStream); // https://stackoverflow.com/a/9021242
res.on('end', cleanupFileStream); // https://stackoverflow.com/a/16897986
res.on('finish', cleanupFileStream); // https://stackoverflow.com/a/14093091 - https://stackoverflow.com/a/38057516
if (!ext.length || !pipe_extensions[ext]) {
var header = {
'Content-Length': range[1],
'Content-Type': type,
'Access-Control-Allow-Origin': req.headers.origin || "*",
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'POST, GET, OPTIONS'
};
if (range[2]) {
header['Accept-Ranges'] = 'bytes';
header['Content-Range'] = 'bytes ' + range[0] + '-' + range[1] + '/' + total;
header['Content-Length'] = range[2];
res.writeHead(206, header);
} else {
res.writeHead(200, header);
}
file.pipe(res);
file.on('close', function () {
res.end(0);
if (opt_cb && typeof opt_cb == 'function') {
opt_cb(path);
}
});
} else {
var _exts = pipe_extensions[ext];
res.writeHead(200,
{
'Content-Type': type,
'Access-Control-Allow-Origin': req.headers.origin || "*",
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
'Access-Control-Allow-Headers': 'POST, GET, OPTIONS'
});
for (var o in _exts) {
_exts[o](file, req, res, function () {
if (!res.__ended) {
res.__ended = true;
res.end(0);
}
});
}
}
return true;
}
return false;
};
exports.on = function (ext, m) {
if (!pipe_extensions[ext]) {
pipe_extensions[ext] = [];
}
m.pipe_extension_id = pipe_extension_id++;
m.pipe_extension = ext;
pipe_extensions[ext].push(m);
};
exports.removeEvent = function (method) {
if (!method || !method.pipe_extension || !method.pipe_extension_id) {
return;
}
if (pipe_extensions[method.pipe_extension]) {
var exts = pipe_extensions[method.pipe_extension];
for (var i = 0, ln = exts.length; i < ln; i++) {
if (exts[i].pipe_extension_id == method.pipe_extension_id) {
pipe_extensions[method.pipe_extension] = exts.splice(i, 1);
}
}
}
};