Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol committed Jul 18, 2024
1 parent c617f73 commit 4fc4fa6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
24 changes: 21 additions & 3 deletions core/amazon/awssdk/listobjects_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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]
Expand Down
34 changes: 34 additions & 0 deletions core/amazon/awssdk/listobjects_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4fc4fa6

Please sign in to comment.