Created
November 9, 2018 02:21
-
-
Save watanabeyu/bcfdc7c801754533ac0cb3af1dd31f44 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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