1 | var assert = require('assert');
|
2 | var fs = require('fs');
|
3 | var mime = require('mime');
|
4 | var util = require('util');
|
5 |
|
6 | var MAX_FILE_SIZE_BYTES = 15 * 1024 * 1024;
|
7 | var MAX_FILE_CHUNK_BYTES = 5 * 1024 * 1024;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var FileUploader = function (params, twit) {
|
21 | assert(params)
|
22 | assert(params.file_path, 'Must specify `file_path` to upload a file. Got: ' + params.file_path + '.')
|
23 | var self = this;
|
24 | self._file_path = params.file_path;
|
25 | self._twit = twit;
|
26 | self._isUploading = false;
|
27 | self._isFileStreamEnded = false;
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | FileUploader.prototype.upload = function (cb) {
|
36 | var self = this;
|
37 |
|
38 |
|
39 | self._initMedia(function (err, bodyObj, resp) {
|
40 | if (err) {
|
41 | cb(err);
|
42 | return;
|
43 | } else {
|
44 | var mediaTmpId = bodyObj.media_id_string;
|
45 | var chunkNumber = 0;
|
46 | var mediaFile = fs.createReadStream(self._file_path, { highWatermark: MAX_FILE_CHUNK_BYTES });
|
47 |
|
48 | mediaFile.on('data', function (chunk) {
|
49 |
|
50 |
|
51 | mediaFile.pause();
|
52 | self._isUploading = true;
|
53 |
|
54 | self._appendMedia(mediaTmpId, chunk.toString('base64'), chunkNumber, function (err, bodyObj, resp) {
|
55 | self._isUploading = false;
|
56 | if (err) {
|
57 | cb(err);
|
58 | } else {
|
59 | if (self._isUploadComplete()) {
|
60 |
|
61 | self._finalizeMedia(mediaTmpId, cb);
|
62 | } else {
|
63 |
|
64 | chunkNumber++;
|
65 | mediaFile.resume();
|
66 | }
|
67 | }
|
68 | });
|
69 | });
|
70 |
|
71 | mediaFile.on('end', function () {
|
72 |
|
73 | self._isFileStreamEnded = true;
|
74 | if (self._isUploadComplete()) {
|
75 | self._finalizeMedia(mediaTmpId, cb);
|
76 | }
|
77 | });
|
78 | }
|
79 | })
|
80 | }
|
81 |
|
82 | FileUploader.prototype._isUploadComplete = function () {
|
83 | return !this._isUploading && this._isFileStreamEnded;
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | FileUploader.prototype._finalizeMedia = function(media_id, cb) {
|
93 | var self = this;
|
94 | self._twit.post('media/upload', {
|
95 | command: 'FINALIZE',
|
96 | media_id: media_id
|
97 | }, cb);
|
98 | }
|
99 |
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | FileUploader.prototype._appendMedia = function(media_id_string, chunk_part, segment_index, cb) {
|
110 | var self = this;
|
111 | self._twit.post('media/upload', {
|
112 | command: 'APPEND',
|
113 | media_id: media_id_string.toString(),
|
114 | segment_index: segment_index,
|
115 | media: chunk_part,
|
116 | }, cb);
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | FileUploader.prototype._initMedia = function (cb) {
|
125 | var self = this;
|
126 | var mediaType = mime.lookup(self._file_path);
|
127 | var mediaFileSizeBytes = fs.statSync(self._file_path).size;
|
128 |
|
129 |
|
130 |
|
131 | if (mediaFileSizeBytes < MAX_FILE_SIZE_BYTES) {
|
132 | self._twit.post('media/upload', {
|
133 | 'command': 'INIT',
|
134 | 'media_type': mediaType,
|
135 | 'total_bytes': mediaFileSizeBytes
|
136 | }, cb);
|
137 | } else {
|
138 | var errMsg = util.format('This file is too large. Max size is %dB. Got: %dB.', MAX_FILE_SIZE_BYTES, mediaFileSizeBytes);
|
139 | cb(new Error(errMsg));
|
140 | }
|
141 | }
|
142 |
|
143 | module.exports = FileUploader
|