Skip to content

Commit 2654764

Browse files
Propagate error for ListAll (#713)
1 parent 65dcc2d commit 2654764

File tree

10 files changed

+124
-21
lines changed

10 files changed

+124
-21
lines changed

firebase-storage/CHANGELOG.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
# Unreleased (17.1.0)
1+
# 19.0.1
2+
- [fixed] `listAll()` now propagates the error messages if the List operation
3+
was denied by a Security Rule.
4+
5+
# 19.0.0
6+
- [changed] Added missing nullability annotations for better Kotlin interop.
7+
- [internal] Removed ``@PublicApi` annotations as they are no longer enforced
8+
and have no semantic meaning.
9+
10+
# 18.1.0
211
- [feature] Added `StorageReference.list()` and `StorageReference.listAll()`,
312
which allows developers to list the files and folders under the given
413
StorageReference.
514
- [changed] Added validation to `StorageReference.getDownloadUrl()` and
615
`StorageReference.getMetadata()` to return an error if the reference is the
716
root of the bucket.
8-
- [changed] Added missing nullability annotations for better Kotlin interop.
9-
- [internal] Removed ``@PublicApi` annotations as they are no longer enforced
10-
and have no semantic meaning.
1117

1218
# 17.0.0
1319
- [internal] Updated the SDK initialization process and removed usages of

firebase-storage/src/main/java/com/google/firebase/storage/StorageReference.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -626,15 +626,20 @@ public Task<ListResult> listAll() {
626626
new Continuation<ListResult, Task<Void>>() {
627627
@Override
628628
public Task<Void> then(@NonNull Task<ListResult> currentPage) {
629-
ListResult result = currentPage.getResult();
630-
prefixes.addAll(result.getPrefixes());
631-
items.addAll(result.getItems());
632-
633-
if (result.getPageToken() != null) {
634-
Task<ListResult> nextPage = listHelper(/* maxResults= */ null, result.getPageToken());
635-
nextPage.continueWithTask(executor, this);
629+
if (currentPage.isSuccessful()) {
630+
ListResult result = currentPage.getResult();
631+
prefixes.addAll(result.getPrefixes());
632+
items.addAll(result.getItems());
633+
634+
if (result.getPageToken() != null) {
635+
Task<ListResult> nextPage =
636+
listHelper(/* maxResults= */ null, result.getPageToken());
637+
nextPage.continueWithTask(executor, this);
638+
} else {
639+
pendingResult.setResult(new ListResult(prefixes, items, /* pageToken= */ null));
640+
}
636641
} else {
637-
pendingResult.setResult(new ListResult(prefixes, items, /* pageToken= */ null));
642+
pendingResult.setException(currentPage.getException());
638643
}
639644

640645
return Tasks.forResult(null);

firebase-storage/src/test/java/com/google/firebase/storage/ListTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ public void listResultsWithMultiplePages() throws InterruptedException {
103103
TestUtil.verifyTaskStateChanges("listMultiplePages", task.getResult().toString());
104104
}
105105

106+
@Test
107+
public void listFailure() throws InterruptedException {
108+
MockConnectionFactory factory =
109+
NetworkLayerMock.ensureNetworkMock("listSinglePageFailed", false);
110+
Task<StringBuilder> task = TestCommandHelper.listFiles(/* pageSize= */ 10, /* pageCount= */ 1);
111+
112+
TestUtil.await(task);
113+
114+
factory.verifyOldMock();
115+
TestUtil.verifyTaskStateChanges("listSinglePageFailed", task.getResult().toString());
116+
}
117+
106118
@Test
107119
public void listAll() throws InterruptedException {
108120
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("listAll", true);
@@ -113,4 +125,15 @@ public void listAll() throws InterruptedException {
113125
factory.verifyOldMock();
114126
TestUtil.verifyTaskStateChanges("listAll", task.getResult().toString());
115127
}
128+
129+
@Test
130+
public void listAllWithFailure() throws InterruptedException {
131+
MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("listAllFailed", false);
132+
Task<StringBuilder> task = TestCommandHelper.listAllFiles();
133+
134+
TestUtil.await(task);
135+
136+
factory.verifyOldMock();
137+
TestUtil.verifyTaskStateChanges("listAllFailed", task.getResult().toString());
138+
}
116139
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
<new>
3+
Url:https://firebasestorage.googleapis.com/v0/b/fooey.appspot.com/o?prefix=largeDirectory%2F&delimiter=%2F
4+
setRequestMethod:GET
5+
setRequestProperty:X-Firebase-Storage-Version,Android/[No Gmscore]
6+
setRequestProperty:x-firebase-gmpid,fooey
7+
setRequestProperty:Content-Length,0
8+
setUseCaches:false
9+
setDoInput:true
10+
getResponseCode:403
11+
getHeaderFields:
12+
null:[HTTP/1.1 403 Forbidden]
13+
Access-Control-Allow-Origin:[*]
14+
Access-Control-Expose-Headers:[Content-Range, X-Firebase-Storage-XSRF]
15+
Alt-Svc:[quic=":443"; ma=2592000; v="46,43,39"]
16+
Cache-Control:[private, max-age=0]
17+
Content-Length:[106]
18+
Content-Type:[application/json; charset=UTF-8]
19+
Date:[Fri, 16 Aug 2019 17:51:19 GMT]
20+
Expires:[Fri, 16 Aug 2019 17:51:19 GMT]
21+
Server:[UploadServer]
22+
X-Android-Received-Millis:[1565977885114]
23+
X-Android-Response-Source:[NETWORK 403]
24+
X-Android-Selected-Protocol:[http/1.1]
25+
X-Android-Sent-Millis:[1565977884943]
26+
X-Content-Type-Options:[nosniff]
27+
X-GUploader-UploadID:[AEnB2Up1Fu4ZwtUsQe7mXrzuQ_S2KZvRwUttfOYcrQsZm4kuJcUJFvMzTRxkmydO8ErBV1bI_Ku_B7xqFBp5r4bt-hEu4a2JBQ]
28+
getErrorStream:{ "error": { "code": 403, "message": "Permission denied. Could not perform this operation" }}
29+
disconnect:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
listAll:
3+
onComplete:Success=false
4+
done.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
<new>
3+
Url:https://firebasestorage.googleapis.com/v0/b/fooey.appspot.com/o?prefix=smallDirectory%2F&delimiter=%2F&maxResults=10
4+
setRequestMethod:GET
5+
setRequestProperty:X-Firebase-Storage-Version,Android/[No Gmscore]
6+
setRequestProperty:x-firebase-gmpid,fooey
7+
setRequestProperty:Content-Length,0
8+
setUseCaches:false
9+
setDoInput:true
10+
getResponseCode:403
11+
getHeaderFields:
12+
null:[HTTP/1.1 403 Forbidden]
13+
Access-Control-Allow-Origin:[*]
14+
Access-Control-Expose-Headers:[Content-Range, X-Firebase-Storage-XSRF]
15+
Alt-Svc:[quic=":443"; ma=2592000; v="46,43,39"]
16+
Cache-Control:[private, max-age=0]
17+
Content-Length:[106]
18+
Content-Type:[application/json; charset=UTF-8]
19+
Date:[Fri, 16 Aug 2019 17:51:19 GMT]
20+
Expires:[Fri, 16 Aug 2019 17:51:19 GMT]
21+
Server:[UploadServer]
22+
X-Android-Received-Millis:[1565977885114]
23+
X-Android-Response-Source:[NETWORK 403]
24+
X-Android-Selected-Protocol:[http/1.1]
25+
X-Android-Sent-Millis:[1565977884943]
26+
X-Content-Type-Options:[nosniff]
27+
X-GUploader-UploadID:[AEnB2Up1Fu4ZwtUsQe7mXrzuQ_S2KZvRwUttfOYcrQsZm4kuJcUJFvMzTRxkmydO8ErBV1bI_Ku_B7xqFBp5r4bt-hEu4a2JBQ]
28+
getErrorStream:{ "error": { "code": 403, "message": "Permission denied. Could not perform this operation" }}
29+
disconnect:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
list:
3+
onComplete:Success=false
4+
done.

firebase-storage/src/testUtil/java/com/google/firebase/storage/TestCommandHelper.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ public static Task<StringBuilder> listFiles(int pageSize, int pageCount) {
242242
public void onComplete(@NonNull Task<ListResult> task) {
243243
++pagesReceived[0];
244244

245-
ListResult listResult = task.getResult();
246-
247245
builder.append("\nlist:");
248246
builder.append("\n onComplete:Success=").append(task.isSuccessful());
249247

250248
if (task.isSuccessful()) {
249+
ListResult listResult = task.getResult();
250+
251251
builder.append("\n Received Prefixes:");
252252
for (StorageReference prefix : listResult.getPrefixes()) {
253253
builder.append("\n ").append(prefix.getPath());
@@ -257,12 +257,14 @@ public void onComplete(@NonNull Task<ListResult> task) {
257257
builder.append("\n ").append(item.getPath());
258258
}
259259
builder.append("\n Page Token:").append(listResult.getPageToken());
260-
}
261260

262-
if (pagesReceived[0] == pageCount) {
263-
result.setResult(builder);
261+
if (pagesReceived[0] == pageCount) {
262+
result.setResult(builder);
263+
} else {
264+
reference.list(pageSize, listResult.getPageToken()).addOnCompleteListener(this);
265+
}
264266
} else {
265-
reference.list(pageSize, listResult.getPageToken()).addOnCompleteListener(this);
267+
result.setResult(builder);
266268
}
267269
}
268270
});
@@ -280,12 +282,11 @@ public static Task<StringBuilder> listAllFiles() {
280282
listFiles.addOnCompleteListener(
281283
executor,
282284
task -> {
283-
ListResult listResult = task.getResult();
284-
285285
builder.append("\nlistAll:");
286286
builder.append("\n onComplete:Success=").append(task.isSuccessful());
287287

288288
if (task.isSuccessful()) {
289+
ListResult listResult = task.getResult();
289290
builder.append("\n Received Prefixes:");
290291
for (StorageReference prefix : listResult.getPrefixes()) {
291292
builder.append("\n ").append(prefix.getPath());

firebase-storage/test-app/src/main/java/com/example/storage/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.android.gms.tasks.Continuation;
4141
import com.google.android.gms.tasks.Task;
4242
import com.google.android.gms.tasks.TaskCompletionSource;
43+
import com.google.firebase.FirebaseApp;
4344
import com.google.firebase.auth.FirebaseAuth;
4445
import com.google.firebase.storage.FirebaseStorage;
4546
import com.google.firebase.storage.TestCommandHelper;
@@ -104,6 +105,7 @@ protected void onCreate(Bundle savedInstanceState) {
104105
super.onCreate(savedInstanceState);
105106
setContentView(R.layout.activity_main);
106107

108+
FirebaseApp.initializeApp(getApplicationContext());
107109
FirebaseAuth.getInstance().signInAnonymously();
108110

109111
Button clickButton = findViewById(R.id.streamDownload);

firebase-storage/test-app/test-app.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ dependencies {
6767
// androidTest integration tests.
6868
// ==========================================================================
6969
ext.packageName = "com.example.storage"
70-
apply from: '../../gradle/googleServices.gradle'
70+
apply from: '../../gradle/googleServices.gradle'

0 commit comments

Comments
 (0)