Skip to content

Commit

Permalink
Correctly parse key if prefix is not a directory (peak#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonmezonur committed Feb 13, 2020
1 parent c142829 commit 76e7ce0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion s3url/s3url.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,15 @@ func parseNonBatch(prefix string, key string) string {
if key == prefix || !strings.HasPrefix(key, prefix) {
return key
}
parsedKey := strings.TrimPrefix(key, prefix)
parsedKey := strings.TrimSuffix(key, s3Separator)
if loc := strings.LastIndex(parsedKey, s3Separator); loc < len(prefix) {
if loc < 0 {
return key
}
parsedKey = key[loc:]
return strings.TrimPrefix(parsedKey, s3Separator)
}
parsedKey = strings.TrimPrefix(key, prefix)
parsedKey = strings.TrimPrefix(parsedKey, s3Separator)
index := strings.Index(parsedKey, s3Separator) + 1
if index <= 0 || index >= len(parsedKey) {
Expand Down
12 changes: 12 additions & 0 deletions s3url/s3url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ func Test_parseNonBatch(t *testing.T) {
key: "a/b/asset.txt",
want: "asset.txt",
},
{
name: "parse_key_and_return_current_asset_if_prefix_is_not_dir",
prefix: "a/b/ab",
key: "a/b/abc.txt",
want: "abc.txt",
},
{
name: "parse_key_and_return_current_dir_if_prefix_is_not_dir",
prefix: "test",
key: "testdir/",
want: "testdir/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 76e7ce0

Please sign in to comment.