Skip to content

Commit 9435b1b

Browse files
committed
misc storage
1 parent ee7a63c commit 9435b1b

File tree

2 files changed

+62
-55
lines changed

2 files changed

+62
-55
lines changed
Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,19 @@
11
/* @flow */
22
import { 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

78
const FirestackStorage = NativeModules.FirestackStorage;
89
const 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-
5511
type StorageOptionsType = {
5612
storageBucket?: ?string,
5713
};
14+
5815
export 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

lib/modules/storage/reference.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* @flow */
2+
import { NativeModules } from 'react-native';
3+
4+
import { promisify, noop } from '../../utils';
5+
import { ReferenceBase } from './../base';
6+
import Storage from './';
7+
8+
const FirestackStorage = NativeModules.FirestackStorage;
9+
10+
export default class StorageReference extends ReferenceBase {
11+
constructor(storage: Storage, path: Array<string>) {
12+
super(storage.firestack, path);
13+
this.storage = storage;
14+
}
15+
16+
downloadUrl(): Promise<Object> {
17+
const path = this.pathToString();
18+
this.log.debug('downloadUrl(', path, ')');
19+
return promisify('downloadUrl', FirestackStorage)(this.storage.storageUrl, path)
20+
.catch((err) => {
21+
this.log.error('Error downloading URL for ', path, '. Error: ', err);
22+
throw err;
23+
});
24+
}
25+
26+
/**
27+
* Downloads a reference to the device
28+
* @param {String} downloadPath Where to store the file
29+
* @param listener
30+
* @return {Promise}
31+
*/
32+
download(downloadPath: string, listener: Function = noop): Promise<Object> {
33+
const path = this.pathToString();
34+
this.log.debug('download(', path, ') -> ', downloadPath);
35+
const listeners = [
36+
this.storage._addListener('download_progress', listener),
37+
this.storage._addListener('download_paused', listener),
38+
this.storage._addListener('download_resumed', listener),
39+
];
40+
41+
return promisify('downloadFile', FirestackStorage)(this.storage.storageUrl, path, downloadPath)
42+
.then((res) => {
43+
this.log.debug('res --->', res);
44+
listeners.forEach(l => l.remove());
45+
return res;
46+
})
47+
.catch((err) => {
48+
this.log.error('Error downloading ', path, ' to ', downloadPath, '. Error: ', err);
49+
throw err;
50+
});
51+
}
52+
}

0 commit comments

Comments
 (0)