Created
August 22, 2019 13:15
-
-
Save razbakov/15cf81bc330c31bcc2836ce97a21ec05 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 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