Skip to content

Commit 7c593fa

Browse files
committed
Added StorageRef to track download urls on iOS
1 parent e404fa4 commit 7c593fa

File tree

4 files changed

+73
-27
lines changed

4 files changed

+73
-27
lines changed

ios/Firestack/FirestackStorage.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,29 @@ @implementation FirestackStorage
1515

1616
RCT_EXPORT_MODULE(FirestackStorage);
1717

18+
RCT_EXPORT_METHOD(downloadUrl: (NSString *) path
19+
callback:(RCTResponseSenderBlock) callback)
20+
{
21+
FIRStorageReference *storageRef = [[FIRStorage storage] referenceWithPath:path];
22+
[storageRef downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
23+
if (error != nil) {
24+
NSDictionary *evt = @{
25+
@"status": @"error",
26+
@"path": path,
27+
@"msg": [error debugDescription]
28+
};
29+
callback(@[evt]);
30+
} else {
31+
NSDictionary *resp = @{
32+
@"status": @"success",
33+
@"path": path,
34+
@"url": URL
35+
};
36+
callback(@[[NSNull null], resp]);
37+
}
38+
}];
39+
}
40+
1841
RCT_EXPORT_METHOD(uploadFile: (NSString *) urlStr
1942
name: (NSString *) name
2043
path:(NSString *)path

lib/modules/base.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,23 @@ export class Base {
7272
}
7373
});
7474
}
75+
}
76+
77+
export class ReferenceBase extends Base {
78+
constructor(firestack, path) {
79+
super(firestack);
80+
this.path = Array.isArray(path) ?
81+
path :
82+
(typeof path == 'string' ?
83+
[path] : []);
84+
}
85+
86+
pathToString() {
87+
let path = this.path;
88+
let pathStr = (path.length > 0 ? path.join('/') : '/');
89+
if (pathStr[0] != '/') {
90+
pathStr = `/${pathStr}`
91+
}
92+
return pathStr;
93+
}
7594
}

lib/modules/database.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const FirestackDatabase = NativeModules.FirestackDatabase;
66
const FirestackDatabaseEvt = new NativeEventEmitter(FirestackDatabase);
77

88
import promisify from '../promisify'
9-
import { Base } from './base'
9+
import { Base, ReferenceBase } from './base'
1010

1111
let dbSubscriptions = {};
1212

@@ -114,16 +114,11 @@ class DatabaseQuery {
114114

115115
// https://firebase.google.com/docs/reference/js/firebase.database.Reference
116116
const separator = '/';
117-
class DatabaseRef extends Base {
117+
class DatabaseRef extends ReferenceBase {
118118
constructor(db, path) {
119-
super(db.firestack);
119+
super(db.firestack, path);
120120

121121
this.db = db;
122-
this.path = Array.isArray(path) ?
123-
path :
124-
(typeof path == 'string' ?
125-
[path] : []);
126-
127122
this.query = new DatabaseQuery(this);
128123

129124
// Aliases
@@ -255,24 +250,6 @@ class DatabaseRef extends Base {
255250

256251
dbPath() {
257252
let path = this.path;
258-
// let key, value;
259-
// if (arr && arr.length > 0) {
260-
// key = arr[0];
261-
// value = arr[1];
262-
// // A key and a value were passed, to parse, if necessary
263-
// if (arr.length == 1) {
264-
// if (key && typeof(key) == 'string') {
265-
// path.push(key);
266-
// }
267-
// } else {
268-
// if (key && typeof(key) == 'string' && value) {
269-
// path.push(key);
270-
// } else {
271-
// value = key;
272-
// }
273-
// }
274-
// }
275-
276253
let pathStr = (path.length > 0 ? path.join('/') : '/');
277254
if (pathStr[0] != '/') {
278255
pathStr = `/${pathStr}`

lib/modules/storage.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,19 @@ const FirestackStorage = NativeModules.FirestackStorage;
44
const FirestackStorageEvt = new NativeEventEmitter(FirestackStorage);
55

66
import promisify from '../promisify'
7-
import { Base } from './base'
7+
import { Base, ReferenceBase } from './base'
8+
9+
class StorageRef extends ReferenceBase {
10+
constructor(storage, path) {
11+
super(storage.firestack, path);
12+
}
13+
14+
downloadUrl() {
15+
console.log('downloadUrl called', this.path);
16+
const path = this.pathToString();
17+
return promisify('downloadUrl', FirestackStorage)(path);
18+
}
19+
}
820

921
export class Storage extends Base {
1022
constructor(firestack, options={}) {
@@ -14,10 +26,21 @@ export class Storage extends Base {
1426
this.setStorageUrl(this.options.storageBucket);
1527
}
1628

29+
this.refs = {};
30+
1731
this._addToFirestackInstance(
1832
'uploadFile'
1933
)
2034
}
35+
36+
ref(...path) {
37+
const key = this._pathKey(path);
38+
if (!this.refs[key]) {
39+
const ref = new StorageRef(this, path);
40+
this.refs[key] = ref;
41+
}
42+
return this.refs[key];
43+
}
2144

2245
/**
2346
* Upload a filepath
@@ -58,6 +81,10 @@ export class Storage extends Base {
5881
this.storageUrl = `gs://${url}`;
5982
}
6083

84+
_pathKey(...path) {
85+
return path.join('-');
86+
}
87+
6188
get namespace() {
6289
return 'firestack:storage'
6390
}

0 commit comments

Comments
 (0)