-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
113 lines (107 loc) · 3.18 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
var fs = require('fs');
var request = require('request');
var EventEmitter = require('events').EventEmitter;
var mime = require('mime');
var util = require('util');
function resumableUpload() {
this.byteCount = 0; //init variables
this.tokens = {};
this.filepath = '';
this.metadata = {};
this.retry = -1;
this.host = 'www.googleapis.com';
this.api = '/upload/youtube/v3/videos';
};
util.inherits(resumableUpload, EventEmitter);
//Init the upload by POSTing google for an upload URL (saved to self.location)
resumableUpload.prototype.upload = function() {
var self = this;
var options = {
url: 'https://' + self.host + self.api + '?uploadType=resumable&part=snippet,status,contentDetails',
headers: {
'Host': self.host,
'Authorization': 'Bearer ' + self.tokens.access_token,
'Content-Length': new Buffer(JSON.stringify(self.metadata)).length,
'Content-Type': 'application/json',
'X-Upload-Content-Length': fs.statSync(self.filepath).size,
'X-Upload-Content-Type': mime.lookup(self.filepath)
},
body: JSON.stringify(self.metadata)
};
//Send request and start upload if success
request.post(options, function(err, res, body) {
if (err || !res.headers.location) {
self.emit('error', new Error(err));
self.emit('progress', 'Retrying ...');
if ((self.retry > 0) || (self.retry <= -1)) {
self.retry--;
self.upload(); // retry
} else {
return;
}
}
self.location = res.headers.location;
self.send();
});
}
//Pipes uploadPipe to self.location (Google's Location header)
resumableUpload.prototype.send = function() {
var self = this;
var options = {
url: self.location, //self.location becomes the Google-provided URL to PUT to
headers: {
'Authorization': 'Bearer ' + self.tokens.access_token,
'Content-Length': fs.statSync(self.filepath).size - self.byteCount,
'Content-Type': mime.lookup(self.filepath)
}
};
try {
//creates file stream, pipes it to self.location
var uploadPipe = fs.createReadStream(self.filepath, {
start: self.byteCount,
end: fs.statSync(self.filepath).size
});
} catch (e) {
self.emit('error', new Error(e));
return;
}
var health = setInterval(function(){
self.getProgress(function(err, res, body) {
if (!err && typeof res.headers.range !== 'undefined') {
self.emit('progress', res.headers.range.substring(8));
}
});
}, 5000);
uploadPipe.pipe(request.put(options, function(error, response, body) {
clearInterval(health);
if (!error) {
self.emit('success', body);
return;
}
self.emit('error', new Error(error));
if ((self.retry > 0) || (self.retry <= -1)) {
self.retry--;
self.getProgress(function(err, res, b) {
if (typeof res.headers.range !== 'undefined') {
self.byteCount = res.headers.range.substring(8); //parse response
} else {
self.byteCount = 0;
}
self.send();
});
}
}));
}
resumableUpload.prototype.getProgress = function(handler) {
var self = this;
var options = {
url: self.location,
headers: {
'Authorization': 'Bearer ' + self.tokens.access_token,
'Content-Length': 0,
'Content-Range': 'bytes */' + fs.statSync(self.filepath).size
}
};
request.put(options, handler);
}
module.exports = resumableUpload;