-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathstorage.js
More file actions
94 lines (75 loc) · 2.32 KB
/
storage.js
File metadata and controls
94 lines (75 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import {NativeModules, NativeEventEmitter} from 'react-native';
const FirestackStorage = NativeModules.FirestackStorage;
const FirestackStorageEvt = new NativeEventEmitter(FirestackStorage);
import promisify from '../utils/promisify'
import { Base, ReferenceBase } from './base'
class StorageRef extends ReferenceBase {
constructor(storage, path) {
super(storage.firestack, path);
this.storageUrl = storage.storageUrl;
}
downloadUrl() {
const path = this.pathToString();
return promisify('downloadUrl', FirestackStorage)(this.storageUrl, path);
}
}
export class Storage extends Base {
constructor(firestack, options={}) {
super(firestack, options);
if (this.options.storageBucket) {
this.setStorageUrl(this.options.storageBucket);
}
this.refs = {};
this._addToFirestackInstance(
'uploadFile'
)
}
ref(...path) {
const key = this._pathKey(path);
if (!this.refs[key]) {
const ref = new StorageRef(this, path);
this.refs[key] = ref;
}
return this.refs[key];
}
/**
* Upload a filepath
* @param {string} name The destination for the file
* @param {string} filepath The local path of the file
* @param {object} metadata An object containing metadata
* @return {Promise}
*/
uploadFile(name, filepath, metadata={}, cb) {
let callback = cb;
if (!callback || typeof callback !== 'function') {
callback = (evt) => {}
}
filepath = filepath.replace("file://", "");
const listeners = [];
listeners.push(this._addListener('upload_progress', callback));
listeners.push(this._addListener('upload_paused', callback));
listeners.push(this._addListener('upload_resumed', callback));
return promisify('uploadFile', FirestackStorage)(this.storageUrl, name, filepath, metadata)
.then((res) => {
listeners.forEach(this._removeListener);
return res;
});
}
_addListener(evt, cb) {
return FirestackStorageEvt.addListener(evt, cb);
}
_removeListener(evt) {
return FirestackStorageEvt.removeListener(evt);
}
setStorageUrl(url) {
// return promisify('setStorageUrl', FirestackStorage)(url);
this.storageUrl = `gs://${url}`;
}
_pathKey(...path) {
return path.join('-');
}
get namespace() {
return 'firestack:storage'
}
}
export default Storage