From 4fc4fa6209e23a67cb649afc4408c357ef964399 Mon Sep 17 00:00:00 2001 From: t-takahashi Date: Fri, 19 Jul 2024 06:31:39 +0900 Subject: [PATCH] add --- core/amazon/awssdk/listobjects_v2.go | 24 ++++++++++++++-- core/amazon/awssdk/listobjects_v2_test.go | 34 +++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/core/amazon/awssdk/listobjects_v2.go b/core/amazon/awssdk/listobjects_v2.go index 0a7c737..fe58b04 100644 --- a/core/amazon/awssdk/listobjects_v2.go +++ b/core/amazon/awssdk/listobjects_v2.go @@ -42,14 +42,20 @@ func ListObjectsV2(w http.ResponseWriter, r *http.Request, uploadDirName string) http.Error(w, "Failed to list objects", http.StatusInternalServerError) return } + slog.Info("Items are below", "files", items) - slog.Info("Files are below", "files", items) + prefix := r.URL.Query().Get("prefix") + delimiter := r.URL.Query().Get("delimiter") + filteredItems := FilterWithPrefix(prefix, delimiter, items) - isTruncated, items := IsTruncated(items) + slog.Info("FilteredItems are below", "files", filteredItems) + + isTruncated, truncatedItems := IsTruncated(filteredItems) + slog.Info("TruncatedItems are below", "files", truncatedItems) response := ListObjectsResult{ Name: dir, - Items: items, + Items: truncatedItems, IsTruncated: isTruncated, } @@ -78,6 +84,18 @@ func ListObjects(rootDir string, uploadDirName string) ([]Item, error) { return items, err } +func FilterWithPrefix(prefix string, delimiter string, items []Item) []Item { + var filteredItems []Item + + p := strings.Replace(prefix, delimiter, "/", -1) + for _, item := range items { + if strings.HasPrefix(item.Key, p) { + filteredItems = append(filteredItems, item) + } + } + return filteredItems +} + func IsTruncated(items []Item) (bool, []Item) { if len(items) > 1000 { return true, items[:1000] diff --git a/core/amazon/awssdk/listobjects_v2_test.go b/core/amazon/awssdk/listobjects_v2_test.go index f232b80..566ce48 100644 --- a/core/amazon/awssdk/listobjects_v2_test.go +++ b/core/amazon/awssdk/listobjects_v2_test.go @@ -52,6 +52,40 @@ func TestListObjects(t *testing.T) { } } +func TestFilterWithPrefix(t *testing.T) { + items := []Item{ + {Key: "file1.txt", Size: 1}, + {Key: "dir1/file1.txt", Size: 1}, + {Key: "dir1/file2.txt", Size: 1}, + {Key: "dir1/pic1.png", Size: 1}, + {Key: "dir1/pic2.png", Size: 1}, + {Key: "dir1/dir2/file1.txt", Size: 1}, + {Key: "dir1/dir2/file2.txt", Size: 1}, + {Key: "dir1/dir2/file3.txt", Size: 1}, + } + + expected := []Item{{Key: "file1.txt", Size: 1}} + assert.Equal(t, FilterWithPrefix("file", "%2F", items), expected) + + expected = []Item{{Key: "dir1/file1.txt", Size: 1}, + {Key: "dir1/file2.txt", Size: 1}, + {Key: "dir1/pic1.png", Size: 1}, + {Key: "dir1/pic2.png", Size: 1}, + {Key: "dir1/dir2/file1.txt", Size: 1}, + {Key: "dir1/dir2/file2.txt", Size: 1}, + {Key: "dir1/dir2/file3.txt", Size: 1}} + assert.Equal(t, FilterWithPrefix("dir1", "%2F", items), expected) + + expected = []Item{{Key: "dir1/file1.txt", Size: 1}, + {Key: "dir1/file2.txt", Size: 1}} + assert.Equal(t, FilterWithPrefix("dir1/fi", "%2F", items), expected) + + expected = []Item{{Key: "dir1/dir2/file1.txt", Size: 1}, + {Key: "dir1/dir2/file2.txt", Size: 1}, + {Key: "dir1/dir2/file3.txt", Size: 1}} + assert.Equal(t, FilterWithPrefix("dir1/dir2", "%2F", items), expected) +} + func TestIsTruncated(t *testing.T) { items1 := make([]Item, 1) items999 := make([]Item, 999)