1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var url_module = require('url');
|
20 | var fs_module = require('fs');
|
21 | var https_module = require('https');
|
22 | var http_module = require('http');
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var FileStreamer = module.exports = function FileStreamer(file_path, upload_endpoint) {
|
31 | if (!file_path) {
|
32 | throw new Error('You must provide a file path');
|
33 | }
|
34 |
|
35 | if (!upload_endpoint) {
|
36 | throw new Error('You must provide an upload endpoint');
|
37 | }
|
38 |
|
39 | this._endpoint = url_module.parse(upload_endpoint);
|
40 | this._path = file_path;
|
41 | }
|
42 |
|
43 | FileStreamer.prototype._endpoint = null;
|
44 | FileStreamer.prototype._path = null;
|
45 | FileStreamer.prototype._fd = null;
|
46 | FileStreamer.prototype._file_size = 0;
|
47 | FileStreamer.prototype.sequential = true;
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | FileStreamer.prototype._user_ready = function () {
|
53 | this.ready = function (fn) {
|
54 | fn();
|
55 | }
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | FileStreamer.prototype._ready = function () {
|
65 | var _self = this;
|
66 |
|
67 |
|
68 | this._getNewStart(function (err, start) {
|
69 | if (err) {
|
70 |
|
71 | _self._closeFile();
|
72 | return _self._error(err);
|
73 | }
|
74 |
|
75 | if (start >= _self._file_size) {
|
76 |
|
77 | _self._closeFile()
|
78 | _self._user_ready();
|
79 | } else {
|
80 |
|
81 | _self._streamChunk(start);
|
82 | }
|
83 | });
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | FileStreamer.prototype.ready = function (fn) {
|
92 | this._user_ready = fn;
|
93 | };
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | FileStreamer.prototype._error = function (error) {
|
102 | this.error = function (fn) {
|
103 | fn(error);
|
104 | }
|
105 | };
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | FileStreamer.prototype.error = function (fn) {
|
113 | this._error = fn;
|
114 | };
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | FileStreamer.prototype.upload = function () {
|
120 | var _self = this;
|
121 |
|
122 | fs_module.stat(_self._path, function (stat_err, stats) {
|
123 | if (stat_err) {
|
124 | return _self._error(stat_err);
|
125 | }
|
126 |
|
127 | _self._file_size = stats.size;
|
128 |
|
129 | fs_module.open(_self._path, 'r', function(open_err, fd) {
|
130 | if (open_err) {
|
131 | return this._error(open_err);
|
132 | }
|
133 |
|
134 | _self._fd = fd;
|
135 | _self._streamChunk(0);
|
136 | });
|
137 | });
|
138 | };
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | FileStreamer.prototype._streamChunk = function (start) {
|
146 | var _self = this;
|
147 | _self._putFile(start, function (put_err, code, headers) {
|
148 |
|
149 |
|
150 | if (put_err && code) {
|
151 | _self._closeFile();
|
152 | return _self._error(put_err);
|
153 | }
|
154 |
|
155 | _self._ready();
|
156 | });
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | FileStreamer.prototype._putFile = function (start, callback) {
|
166 | var _self = this;
|
167 |
|
168 | var file = fs_module.createReadStream(_self._path, {
|
169 | start : start
|
170 | });
|
171 |
|
172 | file.on('error', function (err) {
|
173 | callback(err);
|
174 | });
|
175 |
|
176 | var headers = {
|
177 | 'Content-Length' : _self._file_size,
|
178 | 'Content-Type' : 'video/mp4'
|
179 | };
|
180 |
|
181 | headers['Content-Range'] = 'bytes ' + start + '-' + _self._file_size + '/' + _self._file_size;
|
182 |
|
183 | var req = _self._upload_endpoint_request({
|
184 | method : 'PUT',
|
185 | headers : headers
|
186 | }, callback);
|
187 |
|
188 | file.pipe(req);
|
189 | }
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | FileStreamer.prototype._closeFile = function () {
|
195 | if (this._fd) {
|
196 | fs_module.close(this._fd);
|
197 | this._fd = null;
|
198 | }
|
199 | };
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | FileStreamer.prototype._getNewStart = function (next) {
|
206 | var _self = this;
|
207 |
|
208 | this._upload_endpoint_request({
|
209 | method : 'PUT',
|
210 | headers : {
|
211 | 'Content-Range' : 'bytes */*',
|
212 | 'Content-Type' : 'application/octet-stream'
|
213 | }
|
214 | }, function (err, status, headers) {
|
215 | if (err) {
|
216 | return next(err);
|
217 | }
|
218 |
|
219 | if (status === 308) {
|
220 | return next(null, parseInt(headers.range.split('-')[1]));
|
221 | } else {
|
222 | return next(new Error('Invalid http status returned from range query: [' + status + ']'));
|
223 | }
|
224 | }).end();
|
225 | };
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | FileStreamer.prototype._upload_endpoint_request = function (options, callback) {
|
234 | var request_options = {
|
235 | protocol : this._endpoint.protocol,
|
236 | host : this._endpoint.hostname,
|
237 | port : this._endpoint.port,
|
238 | query : this._endpoint.query,
|
239 | headers : options.headers,
|
240 | path : this._endpoint.path,
|
241 | method : options.method
|
242 | };
|
243 |
|
244 | var client = request_options.protocol === 'https:' ? https_module : http_module;
|
245 | var req = client.request(request_options);
|
246 |
|
247 | req.on('response', function (res) {
|
248 | res.setEncoding('utf8');
|
249 |
|
250 | var buffer = '';
|
251 | res.on('readable', function () {
|
252 | buffer += res.read();
|
253 | });
|
254 |
|
255 | if (res.statusCode > 399) {
|
256 |
|
257 | res.on('end', function () {
|
258 | callback(new Error('[' + buffer + ']'), res.statusCode, res.headers);
|
259 | });
|
260 | } else {
|
261 |
|
262 | res.on('end', function () {
|
263 | callback(null, res.statusCode, res.headers);
|
264 | });
|
265 | }
|
266 | });
|
267 |
|
268 |
|
269 | req.on('error', function(e) {
|
270 | callback(e);
|
271 | });
|
272 |
|
273 | return req;
|
274 | }; |
\ | No newline at end of file |