Last active
March 23, 2017 10:54
-
-
Save anomaly44/d3d816b4c69f521cdbeb9ee52b78d49f to your computer and use it in GitHub Desktop.
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
export function putToS3(file, signedRequest) { | |
return new Promise((resolve, reject) => { | |
const xhr = new XMLHttpRequest(); | |
xhr.open('PUT', signedRequest); | |
xhr.setRequestHeader('x-amz-acl', 'public-read'); | |
xhr.onload = () => { | |
if (xhr.status === 200) { | |
resolve('ok'); | |
} | |
}; | |
xhr.onerror = () => { | |
reject('Could not upload file.'); | |
}; | |
xhr.send(file); | |
}); | |
} |
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
export function* uploadImages(images, signedUrls) { | |
try { | |
yield put(uploadingImages('test')); | |
if (images.size !== signedUrls.length) { | |
throw new Error('images and signed requests nog consistent'); | |
} | |
const uploadImages = images.map((image, index) => { | |
const blob = dataURItoBlob(image); | |
return putToS3(blob, signedUrls[index].signed_request); | |
}); | |
yield Promise.all(uploadImages); | |
yield put(uploadedImages()); | |
} catch (err) { | |
console.error(err); | |
yield put(uploadingImagesError('err')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment