Skip to content

Commit ba53c44

Browse files
committed
feat: no-op safe chars
1 parent 6d6e196 commit ba53c44

File tree

9 files changed

+51
-7
lines changed

9 files changed

+51
-7
lines changed

config/awsconfig/awsconfig.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func WithAWS(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
6161
s3ForcePathStyle = fs.Bool("s3-force-path-style", false,
6262
"S3 force the request to use path-style addressing s3.amazonaws.com/bucket/key, instead of bucket.s3.amazonaws.com/key")
6363
s3SafeChars = fs.String("s3-safe-chars", "",
64-
"S3 safe characters to be excluded from image key escape")
64+
"S3 safe characters to be excluded from image key escape. Set -- for no-op")
6565

6666
s3LoaderBucket = fs.String("s3-loader-bucket", "",
6767
"S3 Bucket for S3 Loader. Enable S3 Loader only if this value present")

config/fileconfig.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func withFileSystem(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
1212
var (
1313
fileSafeChars = fs.String("file-safe-chars", "",
14-
"File safe characters to be excluded from image key escape")
14+
"File safe characters to be excluded from image key escape. Set -- for no-op")
1515
fileLoaderBaseDir = fs.String("file-loader-base-dir", "",
1616
"Base directory for File Loader. Enable File Loader only if this value present")
1717
fileLoaderPathPrefix = fs.String("file-loader-path-prefix", "",

config/gcloudconfig/gcloudconfig.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func WithGCloud(fs *flag.FlagSet, cb func() (*zap.Logger, bool)) imagor.Option {
1414
var (
1515
gcloudSafeChars = fs.String("gcloud-safe-chars", "",
16-
"Google Cloud safe characters to be excluded from image key escape")
16+
"Google Cloud safe characters to be excluded from image key escape. Set -- for no-op")
1717

1818
gcloudLoaderBucket = fs.String("gcloud-loader-bucket", "",
1919
"Bucket name for Google Cloud Storage Loader. Enable Google Cloud Loader only if this value present")

imagorpath/normalize.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ type SafeChars interface {
1515

1616
var defaultSafeChars = NewSafeChars("")
1717

18-
// NewSafeChars create SafeChars from predefined set of string
18+
// NewSafeChars create SafeChars from predefined set of string, or "--" for no-op
1919
func NewSafeChars(safechars string) SafeChars {
20+
if safechars == "--" {
21+
return NewNoopSafeChars()
22+
}
2023
s := &safeChars{safeChars: map[byte]bool{}}
2124
for _, c := range safechars {
2225
s.safeChars[byte(c)] = true
@@ -25,13 +28,22 @@ func NewSafeChars(safechars string) SafeChars {
2528
return s
2629
}
2730

31+
// NewNoopSafeChars create no-op SafeChars
32+
func NewNoopSafeChars() SafeChars {
33+
return &safeChars{noop: true}
34+
}
35+
2836
type safeChars struct {
2937
hasCustom bool
38+
noop bool
3039
safeChars map[byte]bool
3140
}
3241

3342
// ShouldEscape implements SafeChars interface
34-
func (s *safeChars) ShouldEscape(c byte) bool {
43+
func (s safeChars) ShouldEscape(c byte) bool {
44+
if s.noop {
45+
return false
46+
}
3547
// alphanum
3648
if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
3749
return false

imagorpath/params_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ func TestNormalize(t *testing.T) {
411411
)
412412

413413
assert.Equal(t, "a+", Normalize("a ", nil))
414+
415+
assert.Equal(t, "a-%2B", Normalize("a-+", NewSafeChars("-")))
416+
assert.Equal(t, "a-+", Normalize("a-+", NewNoopSafeChars()))
417+
assert.Equal(t, "a-+", Normalize("a-+", NewSafeChars("--")))
414418
}
415419

416420
func TestHMACSigner(t *testing.T) {

storage/filestorage/filestorage_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ func TestFileStore_Path(t *testing.T) {
3838
safeChars: "{}",
3939
expectedOk: true,
4040
},
41+
{
42+
name: "no-op safe chars",
43+
baseDir: "/home/imagor",
44+
image: "/foo/b{:}ar",
45+
expected: "/home/imagor/foo/b{:}ar",
46+
safeChars: "--",
47+
expectedOk: true,
48+
},
4149
{
4250
name: "path under with base uri",
4351
baseDir: "/home/imagor",

storage/gcloudstorage/gcloudstorage_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ func TestGCloudStorage_Path(t *testing.T) {
4343
safeChars: "{}",
4444
expectedOk: true,
4545
},
46+
{
47+
name: "no-op safe chars",
48+
image: "/foo/b{:}ar",
49+
expectedPath: "foo/b{:}ar",
50+
safeChars: "--",
51+
expectedOk: true,
52+
},
4653
{
4754
name: "path under with base uri",
4855
baseDir: "home/imagor",

storage/s3storage/s3storage.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ func New(sess *session.Session, bucket string, options ...Option) *S3Storage {
5454
for _, option := range options {
5555
option(s)
5656
}
57-
s.safeChars = imagorpath.NewSafeChars("!\"()*" + s.SafeChars)
58-
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html#object-key-guidelines-safe-characters
57+
if s.SafeChars == "--" {
58+
s.safeChars = imagorpath.NewNoopSafeChars()
59+
} else {
60+
s.safeChars = imagorpath.NewSafeChars("!\"()*" + s.SafeChars)
61+
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html#object-key-guidelines-safe-characters
62+
}
5963

6064
return s
6165
}

storage/s3storage/s3storage_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ func TestS3Store_Path(t *testing.T) {
5454
safeChars: "{}",
5555
expectedOk: true,
5656
},
57+
{
58+
name: "no-op safe chars",
59+
bucket: "mybucket",
60+
image: "/foo/b{:}\"ar",
61+
expectedBucket: "mybucket",
62+
expectedPath: "/foo/b{:}\"ar",
63+
safeChars: "--",
64+
expectedOk: true,
65+
},
5766
{
5867
name: "path under with base uri",
5968
bucket: "mybucket",

0 commit comments

Comments
 (0)