Skip to content

Commit

Permalink
feat: no-op safe chars
Browse files Browse the repository at this point in the history
  • Loading branch information
cshum committed Mar 7, 2024
1 parent 6d6e196 commit ba53c44
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/awsconfig/awsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func WithAWS(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
s3ForcePathStyle = fs.Bool("s3-force-path-style", false,
"S3 force the request to use path-style addressing s3.amazonaws.com/bucket/key, instead of bucket.s3.amazonaws.com/key")
s3SafeChars = fs.String("s3-safe-chars", "",
"S3 safe characters to be excluded from image key escape")
"S3 safe characters to be excluded from image key escape. Set -- for no-op")

s3LoaderBucket = fs.String("s3-loader-bucket", "",
"S3 Bucket for S3 Loader. Enable S3 Loader only if this value present")
Expand Down
2 changes: 1 addition & 1 deletion config/fileconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func withFileSystem(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
var (
fileSafeChars = fs.String("file-safe-chars", "",
"File safe characters to be excluded from image key escape")
"File safe characters to be excluded from image key escape. Set -- for no-op")
fileLoaderBaseDir = fs.String("file-loader-base-dir", "",
"Base directory for File Loader. Enable File Loader only if this value present")
fileLoaderPathPrefix = fs.String("file-loader-path-prefix", "",
Expand Down
2 changes: 1 addition & 1 deletion config/gcloudconfig/gcloudconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func WithGCloud(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
var (
gcloudSafeChars = fs.String("gcloud-safe-chars", "",
"Google Cloud safe characters to be excluded from image key escape")
"Google Cloud safe characters to be excluded from image key escape. Set -- for no-op")

gcloudLoaderBucket = fs.String("gcloud-loader-bucket", "",
"Bucket name for Google Cloud Storage Loader. Enable Google Cloud Loader only if this value present")
Expand Down
16 changes: 14 additions & 2 deletions imagorpath/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ type SafeChars interface {

var defaultSafeChars = NewSafeChars("")

// NewSafeChars create SafeChars from predefined set of string
// NewSafeChars create SafeChars from predefined set of string, or "--" for no-op
func NewSafeChars(safechars string) SafeChars {
if safechars == "--" {
return NewNoopSafeChars()
}
s := &safeChars{safeChars: map[byte]bool{}}
for _, c := range safechars {
s.safeChars[byte(c)] = true
Expand All @@ -25,13 +28,22 @@ func NewSafeChars(safechars string) SafeChars {
return s
}

// NewNoopSafeChars create no-op SafeChars
func NewNoopSafeChars() SafeChars {
return &safeChars{noop: true}
}

type safeChars struct {
hasCustom bool
noop bool
safeChars map[byte]bool
}

// ShouldEscape implements SafeChars interface
func (s *safeChars) ShouldEscape(c byte) bool {
func (s safeChars) ShouldEscape(c byte) bool {
if s.noop {
return false
}
// alphanum
if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
return false
Expand Down
4 changes: 4 additions & 0 deletions imagorpath/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ func TestNormalize(t *testing.T) {
)

assert.Equal(t, "a+", Normalize("a ", nil))

assert.Equal(t, "a-%2B", Normalize("a-+", NewSafeChars("-")))
assert.Equal(t, "a-+", Normalize("a-+", NewNoopSafeChars()))
assert.Equal(t, "a-+", Normalize("a-+", NewSafeChars("--")))
}

func TestHMACSigner(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions storage/filestorage/filestorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ func TestFileStore_Path(t *testing.T) {
safeChars: "{}",
expectedOk: true,
},
{
name: "no-op safe chars",
baseDir: "/home/imagor",
image: "/foo/b{:}ar",
expected: "/home/imagor/foo/b{:}ar",
safeChars: "--",
expectedOk: true,
},
{
name: "path under with base uri",
baseDir: "/home/imagor",
Expand Down
7 changes: 7 additions & 0 deletions storage/gcloudstorage/gcloudstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func TestGCloudStorage_Path(t *testing.T) {
safeChars: "{}",
expectedOk: true,
},
{
name: "no-op safe chars",
image: "/foo/b{:}ar",
expectedPath: "foo/b{:}ar",
safeChars: "--",
expectedOk: true,
},
{
name: "path under with base uri",
baseDir: "home/imagor",
Expand Down
8 changes: 6 additions & 2 deletions storage/s3storage/s3storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ func New(sess *session.Session, bucket string, options ...Option) *S3Storage {
for _, option := range options {
option(s)
}
s.safeChars = imagorpath.NewSafeChars("!\"()*" + s.SafeChars)
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html#object-key-guidelines-safe-characters
if s.SafeChars == "--" {
s.safeChars = imagorpath.NewNoopSafeChars()
} else {
s.safeChars = imagorpath.NewSafeChars("!\"()*" + s.SafeChars)
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html#object-key-guidelines-safe-characters
}

return s
}
Expand Down
9 changes: 9 additions & 0 deletions storage/s3storage/s3storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func TestS3Store_Path(t *testing.T) {
safeChars: "{}",
expectedOk: true,
},
{
name: "no-op safe chars",
bucket: "mybucket",
image: "/foo/b{:}\"ar",
expectedBucket: "mybucket",
expectedPath: "/foo/b{:}\"ar",
safeChars: "--",
expectedOk: true,
},
{
name: "path under with base uri",
bucket: "mybucket",
Expand Down

0 comments on commit ba53c44

Please sign in to comment.