11/* @flow */
22import { NativeModules , NativeEventEmitter } from 'react-native' ;
33
4- import { promisify , noop } from '../utils' ;
5- import { Base , ReferenceBase } from './base' ;
4+ import { Base } from './../base' ;
5+ import StorageRef from './reference' ;
6+ import { promisify , noop } from './../../utils' ;
67
78const FirestackStorage = NativeModules . FirestackStorage ;
89const FirestackStorageEvt = new NativeEventEmitter ( FirestackStorage ) ;
910
10- class StorageRef extends ReferenceBase {
11- constructor ( storage , path ) {
12- super ( storage . firestack , path ) ;
13-
14- this . storage = storage ;
15- }
16-
17- downloadUrl ( ) : Promise < Object > {
18- const path = this . pathToString ( ) ;
19- this . log . debug ( 'downloadUrl(' , path , ')' ) ;
20- return promisify ( 'downloadUrl' , FirestackStorage ) ( this . storage . storageUrl , path )
21- . catch ( err => {
22- this . log . error ( 'Error downloading URL for ' , path , '. Error: ' , err ) ;
23- throw err ;
24- } ) ;
25- }
26-
27- /**
28- * Downloads a reference to the device
29- * @param {String } downloadPath Where to store the file
30- * @param listener
31- * @return {Promise }
32- */
33- download ( downloadPath : string , listener : Function = noop ) : Promise < Object > {
34- const path = this . pathToString ( ) ;
35- this . log . debug ( 'download(' , path , ') -> ' , downloadPath ) ;
36- const listeners = [
37- this . storage . _addListener ( 'download_progress' , listener ) ,
38- this . storage . _addListener ( 'download_paused' , listener ) ,
39- this . storage . _addListener ( 'download_resumed' , listener ) ,
40- ] ;
41-
42- return promisify ( 'downloadFile' , FirestackStorage ) ( this . storage . storageUrl , path , downloadPath )
43- . then ( ( res ) => {
44- this . log . debug ( 'res --->' , res ) ;
45- listeners . forEach ( listener => listener . remove ( ) ) ;
46- return res ;
47- } )
48- . catch ( err => {
49- this . log . error ( 'Error downloading ' , path , ' to ' , downloadPath , '. Error: ' , err ) ;
50- throw err ;
51- } ) ;
52- }
53- }
54-
5511type StorageOptionsType = {
5612 storageBucket ?: ?string ,
5713} ;
14+
5815export default class Storage extends Base {
59- constructor ( firestack : Object , options :StorageOptionsType = { } ) {
16+ constructor ( firestack : Object , options : StorageOptionsType = { } ) {
6017 super ( firestack , options ) ;
6118
6219 if ( this . options . storageBucket ) {
@@ -69,14 +26,13 @@ export default class Storage extends Base {
6926 ref ( ...path : Array < string > ) : StorageRef {
7027 const key = this . _pathKey ( ...path ) ;
7128 if ( ! this . refs [ key ] ) {
72- const ref = new StorageRef ( this , path ) ;
73- this . refs [ key ] = ref ;
29+ this . refs [ key ] = new StorageRef ( this , path ) ;
7430 }
7531 return this . refs [ key ] ;
7632 }
7733
7834 /**
79- * Upload a filepath
35+ * Upload a file path
8036 * @param {string } name The destination for the file
8137 * @param {string } filePath The local path of the file
8238 * @param {object } metadata An object containing metadata
@@ -97,7 +53,7 @@ export default class Storage extends Base {
9753 listeners . forEach ( listener => listener . remove ( ) ) ;
9854 return res ;
9955 } )
100- . catch ( err => {
56+ . catch ( ( err ) => {
10157 this . log . error ( 'Error uploading file ' , name , ' to ' , _filePath , '. Error: ' , err ) ;
10258 throw err ;
10359 } ) ;
@@ -108,8 +64,7 @@ export default class Storage extends Base {
10864 }
10965
11066 _addListener ( evt : string , cb : ( evt : Object ) => Object ) : { remove : ( ) => void } {
111- let listener = FirestackStorageEvt . addListener ( evt , cb ) ;
112- return listener ;
67+ return FirestackStorageEvt . addListener ( evt , cb ) ;
11368 }
11469
11570 setStorageUrl ( url : string ) : void {
@@ -134,7 +89,7 @@ export default class Storage extends Base {
13489 } ;
13590
13691 get namespace ( ) : string {
137- return 'firestack :storage '
92+ return 'firestack:storage' ;
13893 }
13994}
14095
0 commit comments