Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
Created August 18, 2023 17:41
Show Gist options
  • Save jackcoldrick90/213af44a88e239c6ef24e0d1a1dba889 to your computer and use it in GitHub Desktop.
Save jackcoldrick90/213af44a88e239c6ef24e0d1a1dba889 to your computer and use it in GitHub Desktop.
Use this action inside of a contact workflow. It can be used to make a file uploaded via form submission publicly accessible (but not indexable).
const axios = require('axios'); // Used for HTTP requests
axios.defaults.headers.common['Authorization'] = `Bearer ${process.env.HUBSPOTTOKEN}`; // Include access token in all request from Axios
// Retrieve the file redirect URL from form submission
function getRedirectUrl(attachment) {
let requestUrl = attachment;
return new Promise((resolve) => {
axios.get(requestUrl).then(res => {
resolve(getSubstring(res.request.res.responseUrl, 'form-uploads', '?'));
}).catch(err => { console.log(err.message) })
})
}
// Get the file path from the redirect URL
function getSubstring(str, start, end) {
let char1 = str.indexOf(start);
let char2 = str.lastIndexOf(end);
return str.substring(char1, char2);
}
// Get the actual file URL
function getFileURL(redirectUrl) {
let requestUrl = 'https://api.hubapi.com/files/v3/files/search?path=' + redirectUrl;
return new Promise((resolve) => {
axios.get(requestUrl).then(res => {
//Update file visbility to public
axios.patch('https://api.hubapi.com/files/v3/files/' + res.data.results[0].id, {
"access": "PUBLIC_NOT_INDEXABLE" // "PRIVATE" is default, "PUBLIC_NOT_INDEXABLE" or "PUBLIC_INDEXABLE"
}).then((response) => {
resolve(response.data.url);
}).catch((error) => {
console.error(error);
});
}).catch(err => { console.log(err.message) })
})
}
exports.main = async (event, callback) => {
const attachment = event.inputFields['attachment']; // form upload property
let redirectUrl = await getRedirectUrl(attachment);
let url = await getFileURL(redirectUrl);
callback({
outputFields: {
fileUrl: url
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment