@pipedream/twitter 0.3.10 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,13 @@
1
1
  import twitter from "../../twitter.app.mjs";
2
2
  import fs from "fs";
3
3
  import mime from "mime";
4
+ import FormData from "form-data";
4
5
 
5
6
  export default {
6
7
  key: "twitter-upload-media",
7
8
  name: "Upload Media",
8
9
  description: "Upload images or other media types to Twitter. Returns a `media_id_string` to be used with the Create Tweet action. [See the docs here](https://developer.twitter.com/en/docs/tutorials/uploading-media)",
9
- version: "0.0.2",
10
+ version: "0.0.3",
10
11
  type: "action",
11
12
  props: {
12
13
  twitter,
@@ -35,11 +36,38 @@ export default {
35
36
  ],
36
37
  },
37
38
  },
39
+ methods: {
40
+ async append($, fileSize, mediaId) {
41
+ const bufferLength = 1000000;
42
+ const theBuffer = new Buffer(bufferLength);
43
+ let offset = 0;
44
+ let segment = 0;
45
+ const fd = fs.openSync(this.file, "r");
46
+ while (offset < fileSize) {
47
+ const bytesRead = fs.readSync(fd, theBuffer, 0, bufferLength, null);
48
+ const mediaData = bytesRead < bufferLength
49
+ ? theBuffer.slice(0, bytesRead)
50
+ : theBuffer;
51
+ const data = new FormData();
52
+ data.append("command", "APPEND");
53
+ data.append("media_id", mediaId);
54
+ data.append("media_data", mediaData.toString("base64"));
55
+ data.append("segment_index", segment);
56
+ await this.twitter.uploadMedia({
57
+ data,
58
+ headers: {
59
+ "Content-Type": `multipart/form-data; boundary=${data._boundary}`,
60
+ },
61
+ $,
62
+ });
63
+ segment++;
64
+ offset += bufferLength;
65
+ }
66
+ fs.close(fd);
67
+ },
68
+ },
38
69
  async run({ $ }) {
39
70
  // INIT
40
- const fileContent = fs.readFileSync(this.file, {
41
- encoding: "base64",
42
- });
43
71
  const fileSize = (fs.statSync(this.file)).size;
44
72
  const params = {
45
73
  command: "INIT",
@@ -56,27 +84,7 @@ export default {
56
84
  });
57
85
 
58
86
  // APPEND
59
- const size = 9000;
60
- const numSegments = fileContent.length / size;
61
- let segment = 0;
62
- let startIndex = 0;
63
- let endIndex = startIndex + size;
64
- while (segment <= numSegments) {
65
- const chunk = fileContent.substring(startIndex, endIndex);
66
- const params = {
67
- command: "APPEND",
68
- media_id: mediaId,
69
- media_data: chunk,
70
- segment_index: segment,
71
- };
72
- await this.twitter.uploadMedia({
73
- params,
74
- $,
75
- });
76
- segment++;
77
- startIndex += size;
78
- endIndex += size;
79
- }
87
+ await this.append($, fileSize, mediaId);
80
88
 
81
89
  // FINALIZE
82
90
  let response;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pipedream/twitter",
3
- "version": "0.3.10",
3
+ "version": "0.3.11",
4
4
  "description": "Pipedream Twitter Components",
5
5
  "main": "twitter.app.mjs",
6
6
  "keywords": [
@@ -13,6 +13,7 @@
13
13
  "@pipedream/platform": "^1.2.0",
14
14
  "async-retry": "^1.3.3",
15
15
  "axios": "^0.21.1",
16
+ "form-data": "^4.0.0",
16
17
  "lodash": "^4.17.21",
17
18
  "luxon": "^3.2.0",
18
19
  "mime": "^3.0.0",