From 75e107da0c631dcaf0598850e1866a18cd85008a Mon Sep 17 00:00:00 2001 From: Thomas Dupas Date: Wed, 20 Apr 2022 16:38:30 +0200 Subject: [PATCH] allow inverted sorting if needed --- README.md | 1 + internal/config/config.go | 6 +++ internal/controllers/reversed-s3-objects.go | 47 +++++++++++++++++++ .../controllers/reversed-s3-objects_test.go | 35 ++++++++++++++ internal/controllers/s3.go | 6 ++- 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 internal/controllers/reversed-s3-objects.go create mode 100644 internal/controllers/reversed-s3-objects_test.go diff --git a/README.md b/README.md index d730fa6..ccd4654 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ MAX_IDLE_CONNECTIONS | Allowed number of idle connections to the S3 storage IDLE_CONNECTION_TIMEOUT | Allowed timeout to the S3 storage. | | 10 DISABLE_COMPRESSION | If true will pass encoded content through as-is. | | true INSECURE_TLS | If true it will skip cert checks | | false +REVERSE_SORTING | If true it will sort in descending order | | false ### 2. Run the application diff --git a/internal/config/config.go b/internal/config/config.go index 6e80c32..8594fbd 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -46,6 +46,7 @@ type config struct { // nolint DisableCompression bool // DISABLE_COMPRESSION InsecureTLS bool // Disables TLS validation on request endpoints. JwtSecretKey string // JWT_SECRET_KEY + ReverseSorting bool // REVERSE_SORTING } // Setup configurations with environment variables @@ -98,6 +99,10 @@ func Setup() { if b, err := strconv.ParseBool(os.Getenv("INSECURE_TLS")); err == nil { insecureTLS = b } + reverseSorting := false + if b, err := strconv.ParseBool(os.Getenv("REVERSE_SORTING")); err == nil { + reverseSorting = b + } Config = &config{ AwsRegion: region, AwsAPIEndpoint: os.Getenv("AWS_API_ENDPOINT"), @@ -128,6 +133,7 @@ func Setup() { DisableCompression: disableCompression, InsecureTLS: insecureTLS, JwtSecretKey: os.Getenv("JWT_SECRET_KEY"), + ReverseSorting: reverseSorting, } // Proxy log.Printf("[config] Proxy to %v", Config.S3Bucket) diff --git a/internal/controllers/reversed-s3-objects.go b/internal/controllers/reversed-s3-objects.go new file mode 100644 index 0000000..88cfeb1 --- /dev/null +++ b/internal/controllers/reversed-s3-objects.go @@ -0,0 +1,47 @@ +package controllers + +import ( + "strings" + "unicode" +) + +type reversedS3objects []string + +func (s reversedS3objects) Len() int { + return len(s) +} +func (s reversedS3objects) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} +func (s reversedS3objects) Less(i, j int) bool { + if strings.Contains(s[i], "/") { + if !strings.Contains(s[j], "/") { + return true + } + } else { + if strings.Contains(s[j], "/") { + return false + } + } + irs := []rune(s[i]) + jrs := []rune(s[j]) + + max := len(irs) + if max > len(jrs) { + max = len(jrs) + } + for idx := 0; idx < max; idx++ { + ir := irs[idx] + jr := jrs[idx] + irl := unicode.ToLower(ir) + jrl := unicode.ToLower(jr) + + if irl != jrl { + return irl > jrl + } + if ir != jr { + return ir > jr + } + } + return false +} diff --git a/internal/controllers/reversed-s3-objects_test.go b/internal/controllers/reversed-s3-objects_test.go new file mode 100644 index 0000000..be40567 --- /dev/null +++ b/internal/controllers/reversed-s3-objects_test.go @@ -0,0 +1,35 @@ +package controllers + +import ( + "sort" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestSortByReversedS3objects1(t *testing.T) { + expected := []string{"3", "2", "1"} + + actual := []string{"3", "1", "2"} + sort.Sort(reversedS3objects{(actual)) + + assert.Equal(t, expected, actual) +} + +func TestSortByReversedS3objects2(t *testing.T) { + expected := []string{"/20", "/10", "/101"} + + actual := []string{"/20", "/101", "/10"} + sort.Sort(reversedS3objects(actual)) + + assert.Equal(t, expected, actual) +} + +func TestSortByReversedS3objects3(t *testing.T) { + expected := []string{"/200/10", "/101/1", "/10/2"} + + actual := []string{"/200/10", "/10/2", "/101/1"} + sort.Sort(reversedS3objects(actual)) + + assert.Equal(t, expected, actual) +} diff --git a/internal/controllers/s3.go b/internal/controllers/s3.go index 7243e7c..0b9084e 100644 --- a/internal/controllers/s3.go +++ b/internal/controllers/s3.go @@ -194,7 +194,11 @@ func convertToMaps(s3output *s3.ListObjectsOutput, prefix string) ([]string, map for file := range candidates { files = append(files, file) } - sort.Sort(s3objects(files)) + if config.Config.ReverseSorting { + sort.Sort(reversedS3objects(files)) + } else { + sort.Sort(s3objects(files)) + } return files, updatedAt }