Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow inverted sorting if needed #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions internal/controllers/reversed-s3-objects.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions internal/controllers/reversed-s3-objects_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 5 additions & 1 deletion internal/controllers/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down