Skip to content

Commit d74e5f1

Browse files
committed
Updates to include downloading (closes #61)
1 parent 423e733 commit d74e5f1

File tree

5 files changed

+61
-18
lines changed

5 files changed

+61
-18
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ android/gradlew.bat
5555
android/gradle/
5656
.idea
5757
.idea
58+
coverage

Diff for: ios/Firestack/FirestackStorage.m

+30
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ @implementation FirestackStorage
1515

1616
RCT_EXPORT_MODULE(FirestackStorage);
1717

18+
// Run on a different thread
19+
- (dispatch_queue_t)methodQueue
20+
{
21+
return dispatch_queue_create("io.fullstack.firestack.storage", DISPATCH_QUEUE_SERIAL);
22+
}
23+
1824
RCT_EXPORT_METHOD(downloadUrl: (NSString *) storageUrl
1925
path:(NSString *) path
2026
callback:(RCTResponseSenderBlock) callback)
@@ -225,6 +231,7 @@ - (void) addUploadObservers:(FIRStorageUploadTask *) uploadTask
225231
[downloadTask observeStatus:FIRStorageTaskStatusFailure handler:^(FIRStorageTaskSnapshot *snapshot) {
226232
if (snapshot.error != nil) {
227233
NSDictionary *errProps = [[NSMutableDictionary alloc] init];
234+
NSLog(@"Error in download: %@", snapshot.error);
228235

229236
switch (snapshot.error.code) {
230237
case FIRStorageErrorCodeObjectNotFound:
@@ -249,6 +256,29 @@ - (void) addUploadObservers:(FIRStorageUploadTask *) uploadTask
249256
}}];
250257
}
251258

259+
// This is just too good not to use, but I don't want to take credit for
260+
// this work from RNFS
261+
// https://github.com/johanneslumpe/react-native-fs/blob/master/RNFSManager.m
262+
- (NSString *)getPathForDirectory:(int)directory
263+
{
264+
NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
265+
return [paths firstObject];
266+
}
267+
268+
- (NSDictionary *)constantsToExport
269+
{
270+
return @{
271+
@"MAIN_BUNDLE_PATH": [[NSBundle mainBundle] bundlePath],
272+
@"CACHES_DIRECTORY_PATH": [self getPathForDirectory:NSCachesDirectory],
273+
@"DOCUMENT_DIRECTORY_PATH": [self getPathForDirectory:NSDocumentDirectory],
274+
@"EXTERNAL_DIRECTORY_PATH": [NSNull null],
275+
@"EXTERNAL_STORAGE_DIRECTORY_PATH": [NSNull null],
276+
@"TEMP_DIRECTORY_PATH": NSTemporaryDirectory(),
277+
@"LIBRARY_DIRECTORY_PATH": [self getPathForDirectory:NSLibraryDirectory],
278+
@"FILETYPE_REGULAR": NSFileTypeRegular,
279+
@"FILETYPE_DIRECTORY": NSFileTypeDirectory
280+
};
281+
}
252282

253283
// Not sure how to get away from this... yet
254284
- (NSArray<NSString *> *)supportedEvents {

Diff for: lib/firestack.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export class Firestack extends Singleton {
5959
if (!this.configurePromise) {
6060
const firestackOptions = Object.assign({}, this.options, opts);
6161

62-
log.info('Calling native configureWithOptions')
6362
this.configurePromise = promisify('configureWithOptions', FirestackModule)(firestackOptions)
6463
.then((configuredProperties) => {
6564
log.info('Native configureWithOptions success', configuredProperties);
@@ -117,18 +116,6 @@ export class Firestack extends Singleton {
117116
if (!this._cloudMessaging) { this._cloudMessaging = new CloudMessaging(this); }
118117
return this._cloudMessaging;
119118
}
120-
// Storage
121-
//
122-
// /**
123-
// * Configure the library to store the storage url
124-
// * @param {string} url A string of your firebase storage url
125-
// * @return {Promise}
126-
// */
127-
// setStorageUrl(url) {
128-
// return promisify('setStorageUrl')(url);
129-
// }
130-
131-
132119

133120
// other
134121
get ServerValue() {
@@ -166,6 +153,13 @@ export class Firestack extends Singleton {
166153
return this._store;
167154
}
168155

156+
get constants() {
157+
if (!this._constants) {
158+
this._constants = Object.assign({}, Storage.constants)
159+
}
160+
return this._constants;
161+
}
162+
169163
/**
170164
* Set the redux store helper
171165
*/

Diff for: lib/modules/base.js

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export class Base {
2828
return logs[this.namespace];
2929
}
3030

31+
_addConstantExports(constants) {
32+
Object.keys(constants).forEach(name => {
33+
FirestackModule[name] = constants[name];
34+
});
35+
}
36+
3137
_addToFirestackInstance(...methods) {
3238
methods.forEach(name => {
3339
this.firestack[name] = this[name].bind(this);

Diff for: lib/modules/storage.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ class StorageRef extends ReferenceBase {
3737
const path = this.pathToString();
3838
return promisify('downloadFile', FirestackStorage)(this.storage.storageUrl, path, downloadPath)
3939
.then((res) => {
40+
console.log('res --->', res);
4041
listeners.forEach(this.storage._removeListener);
4142
return res;
42-
});
43+
})
44+
.catch(err => {
45+
console.log('Got an error ->', err);
46+
})
4347
}
4448
}
4549

@@ -52,10 +56,6 @@ export class Storage extends Base {
5256
}
5357

5458
this.refs = {};
55-
56-
this._addToFirestackInstance(
57-
'uploadFile'
58-
)
5959
}
6060

6161
ref(...path) {
@@ -110,6 +110,18 @@ export class Storage extends Base {
110110
return path.join('-');
111111
}
112112

113+
static constants = {
114+
'MAIN_BUNDLE_PATH': FirestackStorage.MAIN_BUNDLE_PATH,
115+
'CACHES_DIRECTORY_PATH': FirestackStorage.CACHES_DIRECTORY_PATH,
116+
'DOCUMENT_DIRECTORY_PATH': FirestackStorage.DOCUMENT_DIRECTORY_PATH,
117+
'EXTERNAL_DIRECTORY_PATH': FirestackStorage.EXTERNAL_DIRECTORY_PATH,
118+
'EXTERNAL_STORAGE_DIRECTORY_PATH': FirestackStorage.EXTERNAL_STORAGE_DIRECTORY_PATH,
119+
'TEMP_DIRECTORY_PATH': FirestackStorage.TEMP_DIRECTORY_PATH,
120+
'LIBRARY_DIRECTORY_PATH': FirestackStorage.LIBRARY_DIRECTORY_PATH,
121+
'FILETYPE_REGULAR': FirestackStorage.FILETYPE_REGULAR,
122+
'FILETYPE_DIRECTORY': FirestackStorage.FILETYPE_DIRECTORY
123+
};
124+
113125
get namespace() {
114126
return 'firestack:storage'
115127
}

0 commit comments

Comments
 (0)