|
| 1 | +import freshdesk from "../../freshdesk.app.mjs"; |
| 2 | +import { axios } from "@pipedream/platform"; |
| 3 | +import fs from "fs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + key: "freshdesk-download-attachment", |
| 7 | + name: "Download Attachment", |
| 8 | + description: "Download an attachment from a ticket. [See the documentation](https://developers.freshdesk.com/api/#view_a_ticket)", |
| 9 | + version: "0.0.1", |
| 10 | + type: "action", |
| 11 | + props: { |
| 12 | + freshdesk, |
| 13 | + ticketId: { |
| 14 | + propDefinition: [ |
| 15 | + freshdesk, |
| 16 | + "ticketId", |
| 17 | + ], |
| 18 | + }, |
| 19 | + attachmentId: { |
| 20 | + type: "integer", |
| 21 | + label: "Attachment ID", |
| 22 | + description: "The ID of the attachment to download", |
| 23 | + async options() { |
| 24 | + const attachments = await this.listTicketAttachments(); |
| 25 | + return attachments.map(({ |
| 26 | + id, name, |
| 27 | + }) => ({ |
| 28 | + value: id, |
| 29 | + label: name, |
| 30 | + })); |
| 31 | + }, |
| 32 | + }, |
| 33 | + syncDir: { |
| 34 | + type: "dir", |
| 35 | + accessMode: "write", |
| 36 | + sync: true, |
| 37 | + }, |
| 38 | + }, |
| 39 | + methods: { |
| 40 | + async listTicketAttachments(opts = {}) { |
| 41 | + const { attachments } = await this.freshdesk.getTicket({ |
| 42 | + ticketId: this.ticketId, |
| 43 | + ...opts, |
| 44 | + }); |
| 45 | + return attachments; |
| 46 | + }, |
| 47 | + }, |
| 48 | + async run({ $ }) { |
| 49 | + const attachments = await this.listTicketAttachments({ |
| 50 | + $, |
| 51 | + }); |
| 52 | + const attachment = attachments.find(({ id }) => id === this.attachmentId); |
| 53 | + |
| 54 | + const resppnse = await axios($, { |
| 55 | + url: attachment.attachment_url, |
| 56 | + responseType: "arraybuffer", |
| 57 | + }); |
| 58 | + |
| 59 | + const rawcontent = resppnse.toString("base64"); |
| 60 | + const buffer = Buffer.from(rawcontent, "base64"); |
| 61 | + const downloadedFilepath = `/tmp/${attachment.name}`; |
| 62 | + fs.writeFileSync(downloadedFilepath, buffer); |
| 63 | + |
| 64 | + const filedata = [ |
| 65 | + attachment.name, |
| 66 | + downloadedFilepath, |
| 67 | + ]; |
| 68 | + |
| 69 | + return { |
| 70 | + filedata, |
| 71 | + attachment, |
| 72 | + }; |
| 73 | + }, |
| 74 | +}; |
0 commit comments