Skip to content

Instantly share code, notes, and snippets.

@lakesare
Last active July 15, 2019 15:55
Show Gist options
  • Save lakesare/e8ee6a268bda03ba61bb9739865a3aec to your computer and use it in GitHub Desktop.
Save lakesare/e8ee6a268bda03ba61bb9739865a3aec to your computer and use it in GitHub Desktop.
TransloaditApi.js - application code
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