Last active
July 15, 2019 15:55
-
-
Save lakesare/e8ee6a268bda03ba61bb9739865a3aec to your computer and use it in GitHub Desktop.
TransloaditApi.js - application code
This file contains 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
import TransloaditApi from './TransloaditApi'; | |
const AWS_S3_URL_PREFIX = 'https://memcode.s3.amazonaws.com/'; | |
const onFileDrop = (event) => { | |
const file = event.target.files[0]; | |
// 1. Generate unique AWS S3 path | |
const uniqueAwsS3Path = 'randomFilename.someExtension'; | |
// 2. Create Transloadit assembly | |
TransloaditApi.createAssembly(uniqueAwsS3Path) | |
.then(({ tusUrl, assemblyUrl }) => | |
// 3. Start uploading our file to Transloadit via the TUS endpoint | |
TransloaditApi.startTusUpload(tusUrl, assemblyUrl, file, { | |
onError: (error) => { | |
console.log({ error: error.toString() }); | |
}, | |
onProgress: (bytesUploaded, bytesTotal) => { | |
const percentage = (bytesUploaded / bytesTotal) * 100; | |
console.log({ progress: percentage }); | |
} | |
onSuccess: () => { | |
console.log({ success: 'Yay! Now that upload via TUS from our side is over, we can calm down and just keep on fetching our assembly status!' }); | |
setInterval(() => { | |
// 4. Start checking the status of our assembly | |
TransloaditApi.getAssembly() | |
.then((response) => { | |
if (response.ok && response.ok === 'ASSEMBLY_COMPLETED') { | |
console.log(`YAY. Access the file at ${AWS_S3_URL_PREFIX + uniqueAwsS3Path}!`); | |
} else { | |
console.log('Assembly is either still executing, or failed, look at returned status to check.'); | |
} | |
}) | |
}, 10 * 1000); | |
} | |
}); | |
); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment