Skip to content

Instantly share code, notes, and snippets.

@anomaly44
Last active March 23, 2017 10:54
Show Gist options
  • Save anomaly44/d3d816b4c69f521cdbeb9ee52b78d49f to your computer and use it in GitHub Desktop.
Save anomaly44/d3d816b4c69f521cdbeb9ee52b78d49f to your computer and use it in GitHub Desktop.
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);
});
}
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