twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

rest_chunked_upload.js (2555B)


      1 var assert = require('assert');
      2 var fs = require('fs');
      3 var mime = require('mime');
      4 var path = require('path');
      5 
      6 var config = require('../config1');
      7 var Twit = require('../lib/twitter');
      8 
      9 describe('twit.postMediaChunked', function () {
     10   it('Posting media via twit.postMediaChunked works with .mp4', function (done) {
     11     var twit = new Twit(config);
     12     var mediaFilePath = path.join(__dirname, './video/station.mp4');
     13     twit.postMediaChunked({ file_path: mediaFilePath }, function (err, bodyObj, resp) {
     14       exports.checkUploadMedia(err, bodyObj, resp)
     15       done()
     16     })
     17   })
     18 
     19   it('POST media/upload via manual commands works with .mp4', function (done) {
     20     var mediaFilePath = path.join(__dirname, './video/station.mp4');
     21     var mediaType = mime.lookup(mediaFilePath);
     22     var mediaFileSizeBytes = fs.statSync(mediaFilePath).size;
     23 
     24     var twit = new Twit(config);
     25     twit.post('media/upload', {
     26       'command': 'INIT',
     27       'media_type': mediaType,
     28       'total_bytes': mediaFileSizeBytes
     29     }, function (err, bodyObj, resp) {
     30       assert(!err, err);
     31       var mediaIdStr = bodyObj.media_id_string;
     32 
     33       var isStreamingFile = true;
     34       var isUploading = false;
     35       var segmentIndex = 0;
     36       var fStream = fs.createReadStream(mediaFilePath, { highWaterMark: 5 * 1024 * 1024 });
     37 
     38       var _finalizeMedia = function (mediaIdStr, cb) {
     39         twit.post('media/upload', {
     40           'command': 'FINALIZE',
     41           'media_id': mediaIdStr
     42         }, cb)
     43       }
     44 
     45       var _checkFinalizeResp = function (err, bodyObj, resp) {
     46         exports.checkUploadMedia(err, bodyObj, resp)
     47         done();
     48       }
     49 
     50       fStream.on('data', function (buff) {
     51         fStream.pause();
     52         isStreamingFile = false;
     53         isUploading = true;
     54 
     55         twit.post('media/upload', {
     56           'command': 'APPEND',
     57           'media_id': mediaIdStr,
     58           'segment_index': segmentIndex,
     59           'media': buff.toString('base64'),
     60         }, function (err, bodyObj, resp) {
     61           assert(!err, err);
     62           isUploading = false;
     63 
     64           if (!isStreamingFile) {
     65             _finalizeMedia(mediaIdStr, _checkFinalizeResp);
     66           }
     67         });
     68       });
     69 
     70       fStream.on('end', function () {
     71         isStreamingFile = false;
     72 
     73         if (!isUploading) {
     74           _finalizeMedia(mediaIdStr, _checkFinalizeResp);
     75         }
     76       });
     77     });
     78   })
     79 })
     80 
     81 exports.checkUploadMedia = function (err, bodyObj, resp) {
     82   assert(!err, err)
     83 
     84   assert(bodyObj)
     85   assert(bodyObj.media_id)
     86   assert(bodyObj.media_id_string)
     87   assert(bodyObj.size)
     88 }