Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Add download url support
Browse files Browse the repository at this point in the history
  • Loading branch information
kroikie committed Apr 2, 2018
1 parent 11c78c3 commit 7336c01
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,33 @@ public void onMethodCall(MethodCall call, final Result result) {
case "StorageReference#delete":
delete(call, result);
break;
case "StorageReference#getDownloadUrl":
getDownloadUrl(call, result);
break;
default:
result.notImplemented();
break;
}
}

private void getDownloadUrl(MethodCall call, final Result result) {
String path = call.argument("path");
StorageReference ref = firebaseStorage.getReference().child(path);
ref.getDownloadUrl().addOnSuccessListener(
new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
result.success(uri.toString());
}
}
).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
result.error("download_error", e.getMessage(), null);
}
});
}

private void delete(MethodCall call, final Result result) {
String path = call.argument("path");
StorageReference ref = firebaseStorage.getReference().child(path);
Expand Down
14 changes: 14 additions & 0 deletions packages/firebase_storage/ios/Classes/FirebaseStoragePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
[self putFile:call result:result];
} else if ([@"StorageReference#getData" isEqualToString:call.method]) {
[self getData:call result:result];
} else if ([@"StorageReference#getDownloadUrl" isEqualToString:call.method]) {
[self getDownloadUrl:call result:result];
} else if ([@"StorageReference#delete" isEqualToString:call.method]) {
[self delete:call result:result];
} else {
Expand Down Expand Up @@ -90,6 +92,18 @@ - (void)getData:(FlutterMethodCall *)call result:(FlutterResult)result {
}];
}

- (void) getDownloadUrl:(FlutterMethodCall *)call result:(FlutterResult)result {
NSString *path = call.arguments[@"path"];
FIRStorageReference *ref = [[FIRStorage storage].reference child:path];
[ref downloadURLWithCompletion:^(NSURL *URL, NSError *error){
if (error != nil) {
result(error.flutterError);
} else {
result(URL.absoluteString);
}
}];
}

- (void) delete:(FlutterMethodCall *)call result:(FlutterResult)result {
NSString *path = call.arguments[@"path"];
FIRStorageReference *ref = [[FIRStorage storage].reference child:path];
Expand Down
7 changes: 6 additions & 1 deletion packages/firebase_storage/lib/firebase_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class StorageReference {
);
}

Future<String> getDownloadURL() {
return FirebaseStorage._channel.invokeMethod("StorageReference#getDownloadUrl",
<String, String>{'path': _pathComponents.join("/")});
}

Future<void> delete() {
return FirebaseStorage._channel.invokeMethod("StorageReference#delete",
<String, String>{'path': _pathComponents.join("/")});
Expand All @@ -65,7 +70,7 @@ class StorageUploadTask {
new Completer<UploadTaskSnapshot>();
Future<UploadTaskSnapshot> get future => _completer.future;

Future<Null> _start() async {
Future<void> _start() async {
final String downloadUrl = await FirebaseStorage._channel.invokeMethod(
"StorageReference#putFile",
<String, String>{
Expand Down

0 comments on commit 7336c01

Please sign in to comment.