Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Created May 15, 2023 21:54
Show Gist options
  • Select an option

  • Save cfjedimaster/9d40ac7b42c12ab758596481f28e66fd to your computer and use it in GitHub Desktop.

Select an option

Save cfjedimaster/9d40ac7b42c12ab758596481f28e66fd to your computer and use it in GitHub Desktop.
async function getUploadData(mediaType, clientId, token) {
let body = {
'mediaType': mediaType
};
body = JSON.stringify(body);
let req = await fetch(REST_API+'/assets', {
method:'post',
headers: {
'X-API-Key':clientId,
'Authorization':`Bearer ${token}`,
'Content-Type':'application/json'
},
body: body
});
let data = await req.json();
return data;
}
async function uploadFile(url, filePath, mediaType) {
let stream = fs.createReadStream(filePath);
let stats = fs.statSync(filePath);
let fileSizeInBytes = stats.size;
let upload = await fetch(url, {
method:'PUT',
redirect:'follow',
headers: {
'Content-Type':mediaType,
'Content-Length':fileSizeInBytes
},
body:stream
});
if(upload.status === 200) return;
else {
throw('Bad result, handle later.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment