Skip to content

Commit

Permalink
fix(core): implement filePathsByPrefix for search by file name in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
brian-mulier-p committed Nov 27, 2023
1 parent 56b2b2c commit d9cf2d9
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/main/java/io/kestra/storage/gcs/GcsStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.channels.ReadableByteChannel;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand Down Expand Up @@ -86,18 +87,23 @@ public InputStream get(String tenantId, URI uri) throws IOException {
}
}

@Override
public List<String> filePathsByPrefix(String tenantId, URI prefix) {
String path = getPath(tenantId, prefix);
return blobsForPrefix(path, true)
.map(BlobInfo::getName)
// keep only files
.filter(blobPath -> !blobPath.endsWith("/"))
.map(blobPath -> blobPath.substring(path.length()))
.toList();
}

@Override
public List<FileAttributes> list(String tenantId, URI uri) throws IOException {
String path = getPath(tenantId, uri);
String prefix = (path.endsWith("/")) ? path : path + "/";
Page<Blob> blobs = this.storage.list(config.bucket, Storage.BlobListOption.prefix(prefix),
Storage.BlobListOption.currentDirectory());
List<FileAttributes> list = blobs.streamAll()
.filter(blob -> {
String key = blob.getName().substring(prefix.length());
// Remove recursive result and requested dir
return !key.isEmpty() && !Objects.equals(key, prefix) && new File(key).getParent() == null;
})

List<FileAttributes> list = blobsForPrefix(prefix, false)
.map(throwFunction(this::getGcsFileAttributes))
.toList();
if(list.isEmpty()) {
Expand All @@ -107,6 +113,22 @@ public List<FileAttributes> list(String tenantId, URI uri) throws IOException {
return list;
}

private Stream<Blob> blobsForPrefix(String prefix, boolean recursive) {
Storage.BlobListOption[] blobListOptions = Stream.concat(
Stream.of(Storage.BlobListOption.prefix(prefix)),
recursive ? Stream.empty() : Stream.of(Storage.BlobListOption.currentDirectory())
).toArray(Storage.BlobListOption[]::new);
Page<Blob> blobs = this.storage.list(config.bucket, blobListOptions);
return blobs.streamAll()
.filter(blob -> {
String key = blob.getName().substring(prefix.length());
// Remove recursive result and requested dir
return !key.isEmpty()
&& !Objects.equals(key, prefix)
&& (recursive || new File(key).getParent() == null);
});
}

@Override
public boolean exists(String tenantId, URI uri) {
try {
Expand Down

0 comments on commit d9cf2d9

Please sign in to comment.