Created
May 15, 2023 21:54
-
-
Save cfjedimaster/9d40ac7b42c12ab758596481f28e66fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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