Skip to content
This repository has been archived by the owner on Feb 13, 2018. It is now read-only.

Commit

Permalink
Add chunked upload for all media types, refs #41
Browse files Browse the repository at this point in the history
  • Loading branch information
reneraab committed Aug 18, 2016
1 parent 743bc46 commit 4fe670b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ Twitter.prototype.uploadMedia = function(params, accessToken, accessTokenSecret,
* @param callback
*/
Twitter.prototype.uploadVideo = function (params, accessToken, accessTokenSecret, callback) {
this.uploadMediaChunked(params, "video/mp4", accessToken, accessTokenSecret, callback);
};

Twitter.prototype.uploadMediaChunked = function (params, media_type, accessToken, accessTokenSecret, callback) {
var bufferLength = 1000000;
var theBuffer = new Buffer(bufferLength);
var offset = 0;
Expand All @@ -319,10 +323,10 @@ Twitter.prototype.uploadVideo = function (params, accessToken, accessTokenSecret
};

fs.stat(params.media, function (err, stats) {
var formData, finalizeVideo, options;
var formData, finalizeMedia, options;
formData = {
command: "INIT",
media_type: 'video/mp4',
media_type: media_type,
total_bytes: stats.size
};
options = {
Expand All @@ -331,7 +335,7 @@ Twitter.prototype.uploadVideo = function (params, accessToken, accessTokenSecret
formData: formData
};

finalizeVideo = function (media_id) {
finalizeMedia = function (media_id) {
return function (err, response, body) {

finished++;
Expand Down Expand Up @@ -372,7 +376,7 @@ Twitter.prototype.uploadVideo = function (params, accessToken, accessTokenSecret
segment_index: segment_index,
media_data: data.toString('base64')
};
request.post(options, finalizeVideo(media_id));
request.post(options, finalizeMedia(media_id));
offset += bufferLength;
segment_index++
}
Expand Down

0 comments on commit 4fe670b

Please sign in to comment.