|
| 1 | +import { getFileStreamAndMetadata } from "@pipedream/platform"; |
| 2 | +import hootsuite from "../../hootsuite.app.mjs"; |
| 3 | + |
| 4 | +export default { |
| 5 | + key: "hootsuite-create-media-upload-job", |
| 6 | + name: "Create Media Upload Job", |
| 7 | + description: "Creates a new Media Upload Job on your Hootsuite account. [See the documentation](https://apidocs.hootsuite.com/docs/api/index.html#operation/createMedia)", |
| 8 | + version: "0.0.1", |
| 9 | + type: "action", |
| 10 | + props: { |
| 11 | + hootsuite, |
| 12 | + file: { |
| 13 | + type: "string", |
| 14 | + label: "File Path or URL", |
| 15 | + description: "The path or URL to the image file.", |
| 16 | + }, |
| 17 | + syncDir: { |
| 18 | + type: "dir", |
| 19 | + accessMode: "read", |
| 20 | + sync: true, |
| 21 | + }, |
| 22 | + }, |
| 23 | + methods: { |
| 24 | + initializeUpload(opts = {}) { |
| 25 | + return this.hootsuite._makeRequest({ |
| 26 | + method: "POST", |
| 27 | + path: "/media", |
| 28 | + ...opts, |
| 29 | + }); |
| 30 | + }, |
| 31 | + streamToBuffer(stream) { |
| 32 | + return new Promise((resolve, reject) => { |
| 33 | + const chunks = []; |
| 34 | + stream.on("data", (chunk) => chunks.push(chunk)); |
| 35 | + stream.on("end", () => resolve(Buffer.concat(chunks))); |
| 36 | + stream.on("error", reject); |
| 37 | + }); |
| 38 | + }, |
| 39 | + uploadImage(url, fileBinary, headers) { |
| 40 | + return this.hootsuite._makeRequest({ |
| 41 | + url, |
| 42 | + method: "PUT", |
| 43 | + maxBodyLength: Infinity, |
| 44 | + data: Buffer.from(fileBinary, "binary"), |
| 45 | + noHeaders: true, |
| 46 | + headers, |
| 47 | + }); |
| 48 | + }, |
| 49 | + }, |
| 50 | + async run({ $ }) { |
| 51 | + const { |
| 52 | + stream, metadata, |
| 53 | + } = await getFileStreamAndMetadata(this.file); |
| 54 | + |
| 55 | + const { |
| 56 | + data: { |
| 57 | + uploadUrl, id, |
| 58 | + }, |
| 59 | + } = await this.initializeUpload({ |
| 60 | + data: { |
| 61 | + sizeBytes: metadata.size, |
| 62 | + mimeType: metadata.contentType, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + const fileBinary = await this.streamToBuffer(stream); |
| 67 | + |
| 68 | + await this.uploadImage(uploadUrl, fileBinary, { |
| 69 | + "Content-Type": metadata.contentType, |
| 70 | + }); |
| 71 | + |
| 72 | + $.export("$summary", `Successfully created media upload job with ID: ${id}`); |
| 73 | + return { |
| 74 | + fileId: id, |
| 75 | + }; |
| 76 | + }, |
| 77 | +}; |
0 commit comments