From 8296b21883279301f1144d10847c93e6d04cc083 Mon Sep 17 00:00:00 2001 From: Niklas Date: Fri, 27 Aug 2021 22:03:19 +0200 Subject: [PATCH] deleteDir recursion fix --- internal/dataloader.go | 28 +++++++++++++++++++++++++++- internal/mutations.go | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/internal/dataloader.go b/internal/dataloader.go index 1f0b9dd..6461b27 100644 --- a/internal/dataloader.go +++ b/internal/dataloader.go @@ -32,12 +32,33 @@ func listObjectsBatch(c context.Context, k dataloader.Keys) []*dataloader.Result return results } +// listObjectsRecursiveBatch just like listObjectsBatch but with recursive set to true +func listObjectsRecursiveBatch(c context.Context, k dataloader.Keys) []*dataloader.Result { + log.Debug("listObjectsRecursiveBatch: ", k.Keys()) + var results []*dataloader.Result + + s3Client, ok := c.Value("s3Client").(*minio.Client) + + if !ok { + return handleLoaderError(k, fmt.Errorf("Failed to get s3Client from context")) + } + + for _, v := range k { + results = append(results, &dataloader.Result{ + Data: listObjects(s3Client, bucketName, v.String(), true), + Error: nil, + }) + } + + return results +} + // listObjects helper func for listObjectsBatch func listObjects(s3Client *minio.Client, bukitName, path string, recursive bool) []minio.ObjectInfo { log.Debug("S3 call 'ListObjects': ", path) objectCh := s3Client.ListObjects(context.Background(), bukitName, minio.ListObjectsOptions{ Prefix: path, - Recursive: false, + Recursive: recursive, }) result := make([]minio.ObjectInfo, 0) @@ -210,6 +231,11 @@ func createDataloader(config AppConfig) map[string]*dataloader.Loader { dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)), ) + loaderMap["listObjectsRecursive"] = dataloader.NewBatchedLoader( + listObjectsRecursiveBatch, + dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)), + ) + loaderMap["getDirs"] = dataloader.NewBatchedLoader( getDirsBatch, dataloader.WithCache(newCache(config.CacheTTL, config.CacheCleanup)), diff --git a/internal/mutations.go b/internal/mutations.go index 12d4446..eed1183 100644 --- a/internal/mutations.go +++ b/internal/mutations.go @@ -152,7 +152,7 @@ func deleteDirectory(ctx context.Context, path string) error { } // Get all files inside the directory - thunk := loader["listObjects"].Load(ctx, dataloader.StringKey(path)) + thunk := loader["listObjectsRecursive"].Load(ctx, dataloader.StringKey(path)) result, err := thunk()