This repository was archived by the owner on Dec 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathupload.js
More file actions
73 lines (62 loc) · 2.23 KB
/
upload.js
File metadata and controls
73 lines (62 loc) · 2.23 KB
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
// this file is necessary because the normal twit upload code is extremely broken
const fs = require("fs");
const upload = (filePath, client) => {
return new Promise((resolve, reject) => {
fs.stat(filePath, (error, stats) => {
if (error) reject(error);
client.post("media/upload", {
"command": "INIT",
"media_type": "video/mp4",
"total_bytes": stats.size
}, (error, bodyObj) => {
if (error) reject(error);
const mediaIdStr = bodyObj.media_id_string;
let isStreamingFile = true;
let isUploading = false;
let segmentIndex = 0;
const fStream = fs.createReadStream(filePath, {
highWaterMark: 5 * 1024 * 1024
});
const _finalizeMedia = function(mediaIdStr, cb) {
client.post("media/upload", {
"command": "FINALIZE",
"media_id": mediaIdStr
}, cb);
};
const _checkFinalizeResp = function(err, bodyObj, resp) {
checkUploadMedia(err, bodyObj, resp);
resolve({ data: bodyObj, response: resp });
};
const checkUploadMedia = function(err, bodyObj) {
if (err) reject(err);
if (!bodyObj) reject("Error with initial upload");
if (!bodyObj.media_id) reject("Error with finalized upload");
if (!bodyObj.media_id_string) reject("Error with finalized upload");
if (!bodyObj.size) reject("Error with finalized upload");
};
fStream.on("data", function(buff) {
isUploading = true;
client.post("media/upload", {
"command": "APPEND",
"media_id": mediaIdStr,
"segment_index": segmentIndex,
"media": buff.toString("base64"),
}, function(error, bodyObj, resp) { // eslint-disable-line no-unused-vars
if (error) reject();
segmentIndex += 1;
if (!isStreamingFile) {
_finalizeMedia(mediaIdStr, _checkFinalizeResp);
}
});
});
fStream.on("end", function() {
isStreamingFile = false;
if (!isUploading) {
_finalizeMedia(mediaIdStr, _checkFinalizeResp);
}
});
});
});
});
};
module.exports = upload;