Search code examples
node.jsaxiossalesforceform-data

Uploaded audio file to salesforce ContentVersion getting corrupted


I need to upload a Zoom recording as ContentVersion to salesforce using the recording file donwload_url.

What I've done so far is this:

// Get download_url field from Zoom file returned by api 
const downloadFileUrl = audioFile.download_url;

const token = await generateToken();

// Call Zoom file download
const recordingResponse = await axios.get(`${downloadFileUrl}?access_token=${token.access_token}`);

// Generate form data
const formData = new FormData();

const metadata = {
    Title: file.id,
    ContentLocation: 's',
    PathOnClient: 'audio.m4a',
    OwnerId: ownerId ?? process.env.DEFAULT_FILE_OWNER_ID,
    ReasonForChange: 'Testing',
};

formData.append('entity_content', JSON.stringify(metadata), {
    contentType: 'application/json'
});

const file = recordingResponse.data;

formData.append('VersionData', file, {
    filename: metadata.PathOnClient,
    contentType: 'application/octet-stream'
});

const conn = await salesforceClient.getClient();

const axiosConfig = {
    headers: {
        'Authorization': `Bearer ${conn.accessToken}`,
        'Content-type': 'multipart/form-data',
    }
};
    
// Post to Salesforce   
await axios.post(`${process.env.SALESFORCE_LOGIN_URL}/services/data/v55.0/sobjects/ContentVersion`, formData, axiosConfig).catch((error) => console.log(error));

The file is uploaded successfully, and I see it in the list of files in Salesforce, but the file is corrupted and cannot be played.

The data that comes from the zoom api when posting to 'download_url' is some kind of utf-16 string. My guess would be that the encoding format is failing because of that when submitting to salesforce.

Does anybody know how this can be solved?


Solution

  • I found the solution to the problem by grabbing the file as a 'arraybuffer':

    const recordingResponse = await axios.get(`${downloadFileUrl}?access_token=${token.access_token}`,
    {
        responseType: 'arraybuffer'
    });
    

    And then passing the file as buffer:

    formData.append('VersionData', Buffer.from(downloadedResponse.data), {
        filename: metadata.PathOnClient,
        contentType: 'application/octet-stream'
    });