Skip to content

Instantly share code, notes, and snippets.

@razbakov
Created August 22, 2019 13:15
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 razbakov/15cf81bc330c31bcc2836ce97a21ec05 to your computer and use it in GitHub Desktop.
Save razbakov/15cf81bc330c31bcc2836ce97a21ec05 to your computer and use it in GitHub Desktop.
import firebase from 'firebase/app'
import 'firebase/storage'
import uuid from 'uuid/v4'
export default function upload (file, metadata, onChange) {
return new Promise((resolve) => {
const ref = 'media/' + uuid()
const uploadTask = firebase
.storage()
.ref()
.child(ref)
.put(file, metadata)
uploadTask.on(
'state_changed',
sp => {
if (onChange) {
onChange(sp)
}
},
null,
() => {
uploadTask.snapshot.ref.getDownloadURL().then(downloadURL => {
resolve({
url: downloadURL,
id: ref,
metadata: metadata,
name: file.name,
size: file.size,
uploadedDate: new Date(),
lastModified: file.lastModified,
description: ''
})
})
}
)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment