0

I am currently attempting to get an image upload component built in Zapier but am having issues getting it to work. I have to be able to download an image, and then POST it to a new endpoint without storing it locally. Currently, the closest I've been able to do is get an IncomingMessage to POST, but I know that's not right.

Does anyone have any advice?

let FormData = require('form-data');
let http = require('https');

const makeDownloadStream = (url) => {
  new Promise((resolve, reject) => {
    http.request(url, resolve).on('error', reject).end();
  });
}

const makeUploadStream = (z, bundle, options) => {
    var imageRequest = options;
    const promise = z.request(imageRequest);
    return promise.then((response) => {
        return response.data;
    });
}

const addAttachment = async (z, bundle) => {
    /*var request = {
        'url': bundle.inputData.attachment
    };
    const promiseAt = z.request(request);
    return promiseAt.then((stream) => {*/
        const form = new FormData();
        var data = `{"type": "records", "attributes": {"form_id": ${bundle.inputData.form_id}}}`
        const stream = await makeDownloadStream(bundle.inputData.attachment);
        form.append(`field_${bundle.inputData.field_id}`, stream);
        form.append('data', data);
        var request = {
            'url': bundle.inputData.url,
            'method': 'PUT',
            'headers': {
                'Content-Type': `multipart/form-data; boundary=${form.getBoundary()}`
            },
            'body': form
        };
        const response = await makeUploadStream(z, bundle, request);
        return response;
    //});
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

2

I figured it out myself. For anyone needing to upload an image on Zapier, here it is:

let FormData = require('form-data');

const makeDownloadStream = (z, bundle) => {
    var imageRequest = {
        'url': bundle.inputData.attachment,
        'method': 'GET',
        'raw': true
    };
    const promise = z.request(imageRequest);
    return promise.then(async (response) => {
        var buffer = await response.buffer();
        return {
            'content-type': response.headers.get('content-type'),
            'content': buffer,
            'filename': response.headers.get('content-disposition').replace('attachment; filename="', '').replace('"', '')
        }
    });
}

const addAttachment = async (z, bundle) => {
    const form = new FormData();
    const content = await makeDownloadStream(z, bundle);
    form.append(`field_${bundle.inputData.field_id}`, Buffer.from(content.content.toString('binary'), 'binary'), {
        filename: content.filename
    });
    const request = {
        'url': `${bundle.inputData.url}/api/records/${bundle.inputData.record_id}`,
        'method': 'PUT',
        'headers': {
            'Content-Type': `multipart/form-data; boundary=${form.getBoundary()}`,
            'Content-Length': form.getLengthSync()
        },
        'body': form
    };
    const promise = z.request(request);
    return promise.then((response) => {
        return response.data;
    });
}