1
1
/* @flow */
2
2
import { NativeModules , NativeEventEmitter } from 'react-native' ;
3
3
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' ;
6
7
7
8
const FirestackStorage = NativeModules . FirestackStorage ;
8
9
const FirestackStorageEvt = new NativeEventEmitter ( FirestackStorage ) ;
9
10
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
-
55
11
type StorageOptionsType = {
56
12
storageBucket ?: ?string ,
57
13
} ;
14
+
58
15
export default class Storage extends Base {
59
- constructor ( firestack : Object , options :StorageOptionsType = { } ) {
16
+ constructor ( firestack : Object , options : StorageOptionsType = { } ) {
60
17
super ( firestack , options ) ;
61
18
62
19
if ( this . options . storageBucket ) {
@@ -69,14 +26,13 @@ export default class Storage extends Base {
69
26
ref ( ...path : Array < string > ) : StorageRef {
70
27
const key = this . _pathKey ( ...path ) ;
71
28
if ( ! this . refs [ key ] ) {
72
- const ref = new StorageRef ( this , path ) ;
73
- this . refs [ key ] = ref ;
29
+ this . refs [ key ] = new StorageRef ( this , path ) ;
74
30
}
75
31
return this . refs [ key ] ;
76
32
}
77
33
78
34
/**
79
- * Upload a filepath
35
+ * Upload a file path
80
36
* @param {string } name The destination for the file
81
37
* @param {string } filePath The local path of the file
82
38
* @param {object } metadata An object containing metadata
@@ -97,7 +53,7 @@ export default class Storage extends Base {
97
53
listeners . forEach ( listener => listener . remove ( ) ) ;
98
54
return res ;
99
55
} )
100
- . catch ( err => {
56
+ . catch ( ( err ) => {
101
57
this . log . error ( 'Error uploading file ' , name , ' to ' , _filePath , '. Error: ' , err ) ;
102
58
throw err ;
103
59
} ) ;
@@ -108,8 +64,7 @@ export default class Storage extends Base {
108
64
}
109
65
110
66
_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 ) ;
113
68
}
114
69
115
70
setStorageUrl ( url : string ) : void {
@@ -134,7 +89,7 @@ export default class Storage extends Base {
134
89
} ;
135
90
136
91
get namespace ( ) : string {
137
- return 'firestack :storage '
92
+ return 'firestack:storage' ;
138
93
}
139
94
}
140
95
0 commit comments