Skip to content

Commit

Permalink
feat: Support recursive listing into Files API #337
Browse files Browse the repository at this point in the history
  • Loading branch information
astsiapanay committed Jun 6, 2024
1 parent 39ba0c6 commit f89199e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ private String getContentType() {
@Override
protected Future<?> handle(ResourceDescription resource) {
BlobStorage storage = proxy.getStorage();
boolean recursive = Boolean.parseBoolean(context.getRequest().getParam("recursive", "false"));
String token = context.getRequest().getParam("token");
int limit = Integer.parseInt(context.getRequest().getParam("limit", "100"));
return proxy.getVertx().executeBlocking(() -> {
try {
MetadataBase metadata = storage.listMetadata(resource);
MetadataBase metadata = storage.listMetadata(resource, token, limit, recursive);
if (metadata != null) {
proxy.getAccessService().filterForbidden(context, resource, metadata);
context.respond(HttpStatus.OK, getContentType(), metadata);
Expand Down
40 changes: 19 additions & 21 deletions src/main/java/com/epam/aidial/core/storage/BlobStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,8 @@ public boolean copy(String fromPath, String toPath) {
/**
* List all files/folder metadata for a given resource
*/
public MetadataBase listMetadata(ResourceDescription resource) {
String storageLocation = getStorageLocation(resource.getAbsoluteFilePath());
ListContainerOptions options = buildListContainerOptions(storageLocation);
public MetadataBase listMetadata(ResourceDescription resource, String afterMarker, int maxResults, boolean recursive) {
ListContainerOptions options = buildListContainerOptions(resource.getAbsoluteFilePath(), maxResults, recursive, afterMarker);
PageSet<? extends StorageMetadata> list = blobStore.list(this.bucketName, options);
List<MetadataBase> filesMetadata = list.stream().map(meta -> buildFileMetadata(resource, meta)).toList();

Expand All @@ -220,20 +219,7 @@ public MetadataBase listMetadata(ResourceDescription resource) {
}

public PageSet<? extends StorageMetadata> list(String absoluteFilePath, String afterMarker, int maxResults, boolean recursive) {
String storageLocation = getStorageLocation(absoluteFilePath);
ListContainerOptions options = new ListContainerOptions()
.prefix(storageLocation)
.maxResults(maxResults);

if (recursive) {
options.recursive();
} else {
options.delimiter(BlobStorageUtil.PATH_SEPARATOR);
}

if (afterMarker != null) {
options.afterMarker(afterMarker);
}
ListContainerOptions options = buildListContainerOptions(absoluteFilePath, maxResults, recursive, afterMarker);

PageSet<? extends StorageMetadata> originalSet = blobStore.list(bucketName, options);
if (prefix == null) {
Expand Down Expand Up @@ -264,10 +250,22 @@ public void close() {
storeContext.close();
}

private static ListContainerOptions buildListContainerOptions(String prefix) {
return new ListContainerOptions()
.prefix(prefix)
.delimiter(BlobStorageUtil.PATH_SEPARATOR);
private ListContainerOptions buildListContainerOptions(String absoluteFilePath, int maxResults, boolean recursive, String afterMarker) {
String storageLocation = getStorageLocation(absoluteFilePath);
ListContainerOptions options = new ListContainerOptions()
.prefix(storageLocation)
.maxResults(maxResults);

if (recursive) {
options.recursive();
} else {
options.delimiter(BlobStorageUtil.PATH_SEPARATOR);
}

if (afterMarker != null) {
options.afterMarker(afterMarker);
}
return options;
}

private MetadataBase buildFileMetadata(ResourceDescription resource, StorageMetadata metadata) {
Expand Down

0 comments on commit f89199e

Please sign in to comment.