From f2164490c2f909f28ea71761d39686f819121108 Mon Sep 17 00:00:00 2001 From: alex-lambrides Date: Thu, 9 Jul 2020 16:14:33 -0400 Subject: [PATCH] Implement list to enable remote state storage. --- .../gyro/google/GoogleStorageFileBackend.java | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/main/java/gyro/google/GoogleStorageFileBackend.java b/src/main/java/gyro/google/GoogleStorageFileBackend.java index af9ae7e9..5171d9e7 100644 --- a/src/main/java/gyro/google/GoogleStorageFileBackend.java +++ b/src/main/java/gyro/google/GoogleStorageFileBackend.java @@ -19,15 +19,17 @@ import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; import java.util.stream.Stream; import com.google.api.client.http.ByteArrayContent; import com.google.api.services.storage.Storage; +import com.google.api.services.storage.model.Objects; import com.google.api.services.storage.model.StorageObject; - import com.psddev.dari.util.ObjectUtils; - import gyro.core.FileBackend; +import gyro.core.GyroCore; import gyro.core.GyroException; import gyro.core.Type; import gyro.core.auth.CredentialsSettings; @@ -69,7 +71,34 @@ public String getCredentials() { @Override public Stream list() throws Exception { - return null; + if (this.equals(GyroCore.getStateBackend(getName()))) { + List storageObjects = new ArrayList<>(); + Storage client = client(); + String nextPageToken = null; + int count = 0; + + do { + Objects objects = client.objects() + .list(getBucket()) + .setPrefix(prefixed("")) + .setPageToken(nextPageToken) + .execute(); + + if (objects.getItems() != null) { + storageObjects.addAll(objects.getItems()); + } + + nextPageToken = objects.getNextPageToken(); + count++; + } while (nextPageToken != null || count < 10); + + return storageObjects.stream() + .map(StorageObject::getName) + .filter(f -> f.endsWith(".gyro")) + .map(this::removePrefix); + } + + return Stream.empty(); } @Override @@ -109,4 +138,12 @@ private String prefixed(String file) { return getPrefix() != null ? getPrefix() + '/' + file : file; } + private String removePrefix(String file) { + if (getPrefix() != null && file.startsWith(getPrefix() + "/")) { + return file.substring(getPrefix().length() + 1); + } + + return file; + } + }