Skip to content

Commit 72d3f1a

Browse files
committed
Completed downloadUrl for ios/android
1 parent 7c593fa commit 72d3f1a

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

android/src/main/java/io/fullstack/firestack/FirestackStorage.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
import com.google.android.gms.tasks.OnCompleteListener;
2626
import com.google.android.gms.tasks.OnFailureListener;
2727
import com.google.android.gms.tasks.OnSuccessListener;
28+
import com.google.android.gms.tasks.Task;
2829

2930
import com.google.firebase.storage.OnProgressListener;
3031
import com.google.firebase.storage.OnPausedListener;
3132

32-
import com.google.android.gms.tasks.Task;
3333
import com.google.firebase.FirebaseApp;
34+
3435
import com.google.firebase.storage.FirebaseStorage;
3536
import com.google.firebase.storage.UploadTask;
3637

@@ -58,6 +59,38 @@ public String getName() {
5859
return TAG;
5960
}
6061

62+
@ReactMethod
63+
public void downloadUrl(final String storageUrl,
64+
final String path,
65+
final Callback callback) {
66+
FirebaseStorage storage = FirebaseStorage.getInstance();
67+
StorageReference storageRef = storage.getReferenceFromUrl(storageUrl);
68+
StorageReference fileRef = storageRef.child(path);
69+
70+
Task<Uri> downloadTask = storageRef.getDownloadUrl();
71+
downloadTask.addOnSuccessListener(new OnSuccessListener<Uri>() {
72+
@Override
73+
public void onSuccess(Uri uri) {
74+
WritableMap res = Arguments.createMap();
75+
res.putString("status", "success");
76+
res.putString("path", uri.getPath());
77+
res.putString("url", uri.toString());
78+
callback.invoke(null, res);
79+
}
80+
}).addOnFailureListener(new OnFailureListener() {
81+
@Override
82+
public void onFailure(@NonNull Exception ex) {
83+
Log.e(TAG, "Failed to download file " + exception.getMessage());
84+
85+
WritableMap err = Arguments.createMap();
86+
err.putString("status", "error");
87+
err.putString("description", exception.getLocalizedMessage());
88+
89+
callback.invoke(err);
90+
}
91+
})
92+
}
93+
6194
// STORAGE
6295
@ReactMethod
6396
public void uploadFile(final String urlStr, final String name, final String filepath, final ReadableMap metadata, final Callback callback) {

ios/Firestack/Firestack.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ - (void)dealloc
172172

173173
// self->_configured = YES;
174174
// }
175-
callback(@[[NSNull null]]);
175+
callback(@[[NSNull null], props]);
176176
}
177177
@catch (NSException *exception) {
178178
NSLog(@"Exception occurred while configuring: %@", exception);

ios/Firestack/FirestackStorage.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ @implementation FirestackStorage
1515

1616
RCT_EXPORT_MODULE(FirestackStorage);
1717

18-
RCT_EXPORT_METHOD(downloadUrl: (NSString *) path
18+
RCT_EXPORT_METHOD(downloadUrl: (NSString *) storageUrl
19+
path:(NSString *) path
1920
callback:(RCTResponseSenderBlock) callback)
2021
{
21-
FIRStorageReference *storageRef = [[FIRStorage storage] referenceWithPath:path];
22-
[storageRef downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
22+
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:storageUrl];
23+
FIRStorageReference *fileRef = [storageRef child:path];
24+
[fileRef downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
2325
if (error != nil) {
2426
NSDictionary *evt = @{
2527
@"status": @"error",
@@ -30,8 +32,8 @@ @implementation FirestackStorage
3032
} else {
3133
NSDictionary *resp = @{
3234
@"status": @"success",
33-
@"path": path,
34-
@"url": URL
35+
@"url": [URL absoluteString],
36+
@"path": [URL path]
3537
};
3638
callback(@[[NSNull null], resp]);
3739
}

lib/firestack.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ export class Firestack extends Singleton {
6262

6363
log.info('Calling native configureWithOptions')
6464
this.configurePromise = promisify('configureWithOptions', FirestackModule)(firestackOptions)
65-
.then((...args) => {
66-
log.info('Native configureWithOptions success', args);
65+
.then((configuredProperties) => {
66+
log.info('Native configureWithOptions success', configuredProperties);
6767
this.configured = true;
68+
this.firestackOptions = configuredProperties;
6869
return args;
6970
}).catch((err) => {
7071
log.error('Native error occurred while calling configure', err);

lib/modules/storage.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import { Base, ReferenceBase } from './base'
99
class StorageRef extends ReferenceBase {
1010
constructor(storage, path) {
1111
super(storage.firestack, path);
12+
13+
this.storageUrl = storage.storageUrl;
1214
}
1315

1416
downloadUrl() {
15-
console.log('downloadUrl called', this.path);
1617
const path = this.pathToString();
17-
return promisify('downloadUrl', FirestackStorage)(path);
18+
return promisify('downloadUrl', FirestackStorage)(this.storageUrl, path);
1819
}
1920
}
2021

0 commit comments

Comments
 (0)