-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamer.js
More file actions
77 lines (65 loc) · 1.79 KB
/
Copy pathstreamer.js
File metadata and controls
77 lines (65 loc) · 1.79 KB
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
var fs = require('fs'),
util = require('util'),
events = require('events');
function Stream (path) {
var _stats,
_path = path,
_that = this;
events.EventEmitter.call(this);
fs.stat(_path, function (err, stats) {
if (err) throw err;
_stats = stats;
_that.emit('ready');
});
this.get_stats = function () {
return _stats;
};
this.get_path = function () {
return _path;
};
}
util.inherits(Stream, events.EventEmitter);
Stream.prototype.send = function (req, res) {
var range = req.headers.range;
var total = this.get_stats().size;
var parts = range.replace(/bytes=/, "").split("-"),
partialstart = parts[0],
partialend = parts[1],
start = parseInt(partialstart, 10),
end = partialend ? parseInt(partialend, 10) : total - 1,
chunksize = (end - start);
fs.open(this.get_path(), 'r', function (err, fd) {
if (err) throw err;
fs.read(fd, new Buffer(chunksize), 0, chunksize, start, function (err, bytesRead, buffer) {
res.writeHead(206, {
"Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": 'audio/mpeg'
});
res.end(buffer.slice(start, end), "binary");
});
});
};
exports.stream = (function () {
var count = 0,
_streams = {};
return function (path, req, res) {
var session_id = 0;
console.log(req.session);
if (!req.session.id) {
req.session.id = count++;
}
session_id = req.session.id;
var id = session_id + ':' + path;
if (!_streams[id]) {
_streams[id] = new Stream(path);
_streams[id].once('ready', function () {
_streams[id].send(req, res);
});
}
else {
_streams[id].send(req, res);
}
};
}());