Hey guys, built a custom module to upload images to CloudFlare but I am having issues with setting the image path from the application and looking for some help.
See below the Custom Module JS & HJSON
const axios = require('axios'); // you will need to install this
const { toSystemPath } = require('../../../lib/core/path');
const fs = require('fs')
const FormData = require('form-data');
exports.cfUpload = async function (options) {
const srcImgPath = options.path; //image path on the server
let path = toSystemPath(this.parseRequired(this.parse(options.path), 'string', 'fs.exists: path is required.'))
let srcPath = fs.createReadStream(path);
var data = new FormData();
//THIS CURRENTLY SENDS TO API BUT NO DATA OR IMAGE SEEMS TO BE PRESENT
// NEED TO SET ENCODING?
data.append('file', path);
//THIS CURRENTLY WORKS BELOW
// data.append('file', fs.createReadStream('/Users/robbydiederich/Kodiak Dropbox/PRODUCT CUSTOMIZER APP - Project Assets Folder/Example Logos - Image Uploads/With Background/test8.jpg'));
var config = {
method: 'post',
url: 'https://api.cloudflare.com/client/v4/accounts/45cea078ebef87fccfbc40e7e0d86331/images/v1',
headers: {
'X-Auth-Key': 'API KEY',
'X-Auth-Email': 'EMAIL',
...data.getHeaders()
},
data: data
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
}
Community Page
Last updated:
Last updated: