Skip to content

Instantly share code, notes, and snippets.

@watanabeyu
Created November 9, 2018 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watanabeyu/bcfdc7c801754533ac0cb3af1dd31f44 to your computer and use it in GitHub Desktop.
Save watanabeyu/bcfdc7c801754533ac0cb3af1dd31f44 to your computer and use it in GitHub Desktop.
import uuid from 'uuid';
import * as firebase from 'firebase';
import 'firebase/firestore';
firebase.initializeApp(config);
firebase.firestore().settings({ timestampsInSnapshots: true });
const uploadFileAsync = async (uri) => {
const ext = uri.split('.').slice(-1)[0];
const path = `file/${this.uid}/${uuid.v4()}.${ext}`;
return new Promise(async (resolve, reject) => {
const blob = await fetch(uri).then(response => response.blob());
const ref = firebase.storage().ref(path);
const unsubscribe = ref.put(blob).on('state_changed',
(state) => { },
(err) => {
unsubscribe();
reject(err);
},
async () => {
unsubscribe();
const url = await ref.getDownloadURL();
resolve(url);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment