Converting filetypes to desired fixed image format
A developer can use tweekit to add to their popular KYC (Know Your Customer) application where all kinds of files get uploaded and there’s constant problems enabling instant conversion of any incoming filetypes to a desired fixed image format
Before starting, always include the
tweekit_bundle.js
-
Rest Version
-
Widget Version
rest_api_example.js
const fetch = require('node-fetch');
const fs = require('fs');
const headers = {
"apikey": "{Example Key}",
"apisecret": "{Example Secret}",
"Content-Type": "application/json;charset=UTF-8"
};
const baseUrl = 'https://www.tweekit.io/tweekit/api'
async function changeType({ docID, type }) {
const format = type
const fetchUrl = `${baseUrl}/image/${docID}`;
const options ={
headers ,
method: 'POST',
body: JSON.stringify({
fmt: format
})
};
const response = await fetch(fetchUrl,options);
response.body.pipe(fs.createWriteStream(`${Date.now()}-test.${format}`));
}
async function upload({ file, filename }) {
formData.append("name", filename);
formData.append("file", fs.createReadStream(file));
const fetchUrl = `${baseUrl}/image/upload`;
const response = await fetch(fetchUrl, opts);
const docID = response.headers.get('x-tweekit-docid')
return { docID, response }
}
function convertFile(fileItem, newType) {
const { file, name, id } = fileItem
const filename = name
const { docID } = upload({ file, filename })
changeType({ docID, type: newType })
}
async function main() {
const fileList = await fetch('sample_filelist_url'. {
method: 'GET'
})
const newType = 'pdf'
fileList.map((fileItem) => {
convertFile(fileItem, newType)
})
}