diff --git a/components/freshdesk/actions/download-attachment/download-attachment.mjs b/components/freshdesk/actions/download-attachment/download-attachment.mjs new file mode 100644 index 0000000000000..e6b78803b51b5 --- /dev/null +++ b/components/freshdesk/actions/download-attachment/download-attachment.mjs @@ -0,0 +1,73 @@ +import freshdesk from "../../freshdesk.app.mjs"; +import { axios } from "@pipedream/platform"; +import fs from "fs"; + +export default { + key: "freshdesk-download-attachment", + name: "Download Attachment", + description: "Download an attachment from a ticket. [See the documentation](https://developers.freshdesk.com/api/#view_a_ticket)", + version: "0.0.1", + type: "action", + props: { + freshdesk, + ticketId: { + propDefinition: [ + freshdesk, + "ticketId", + ], + }, + attachmentId: { + type: "integer", + label: "Attachment ID", + description: "The ID of the attachment to download", + async options() { + const attachments = await this.listTicketAttachments(); + return attachments.map(({ + id, name, + }) => ({ + value: id, + label: name, + })); + }, + }, + syncDir: { + type: "dir", + accessMode: "write", + sync: true, + }, + }, + methods: { + async listTicketAttachments(opts = {}) { + const { attachments } = await this.freshdesk.getTicket({ + ticketId: this.ticketId, + ...opts, + }); + return attachments; + }, + }, + async run({ $ }) { + const attachments = await this.listTicketAttachments({ + $, + }); + const attachment = attachments.find(({ id }) => id === this.attachmentId); + + const response = await axios($, { + url: attachment.attachment_url, + responseType: "arraybuffer", + }); + + const buffer = Buffer.from(response); + const downloadedFilepath = `/tmp/${attachment.name}`; + fs.writeFileSync(downloadedFilepath, buffer); + + const filedata = [ + attachment.name, + downloadedFilepath, + ]; + + return { + filedata, + attachment, + }; + }, +}; diff --git a/components/freshdesk/package.json b/components/freshdesk/package.json index a37abb1407f9a..6189aa3119732 100644 --- a/components/freshdesk/package.json +++ b/components/freshdesk/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/freshdesk", - "version": "0.4.0", + "version": "0.5.0", "description": "Pipedream Freshdesk Components", "main": "freshdesk.app.mjs", "keywords": [