Converting filetypes to desired fixed image format

Converting filetypes to desired fixed image format with Tweekit

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


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) }) }

index.html <html lang="en"> <head> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta http-equiv="X-UA-Compatible" content="ie=edge"/> <title>Converting FileTypes</title> </head> <body> <!-- Creating headshots for all employee photos made easy--> <div id="canvas0"></div> <script src="tweekit-widget.min.js"></script> <script src="script.js"></script> </body> </html>


async function convertFileType(file, newFormat) { const tweekit = new Tweekit("#canvas0", { appId: "{ your app ID }" }) // upload image to server tweekit.upload(file) // set new image size tweekit.format = newFormat const croppedImgFile = await tweekit.result() return URL.createObjectURL(croppedImgFile) }