diff --git a/blaze/bucket.go b/blaze/bucket.go index ea3b2c68..452ba0a4 100644 --- a/blaze/bucket.go +++ b/blaze/bucket.go @@ -289,7 +289,7 @@ func (b *Bucket) uploadBody(ctx *fire.Context, mediaType string) ([]string, erro // parse content disposition if ctx.HTTPRequest.Header.Get("Content-Disposition") != "" { - disposition, params, err := parseContentDisposition(ctx.HTTPRequest.Header.Get("Content-Disposition")) + disposition, params, err := serve.ParseMediaType(ctx.HTTPRequest.Header.Get("Content-Disposition")) if err != nil { return nil, err } diff --git a/blaze/utils.go b/blaze/utils.go index 0d6218be..84224389 100644 --- a/blaze/utils.go +++ b/blaze/utils.go @@ -1,11 +1,7 @@ package blaze import ( - "errors" - "mime" - "net/url" "reflect" - "strings" "github.com/256dpi/fire/coal" ) @@ -27,29 +23,3 @@ func collectFields(model coal.Model) []string { return list } - -func parseContentDisposition(str string) (string, map[string]string, error) { - // attempt to parse string - typ, params, err := mime.ParseMediaType(str) - if err == nil || !errors.Is(err, mime.ErrInvalidMediaParameter) { - return typ, params, err - } - - /* error may be due to unescaped characters */ - - // Chrome will generate the following Content-Disposition header for a file - // named "file_$%!&*()[]{}^+=#@`,;'-_"`.mp4": - // "attachment; filename*=utf-8''file_$%25!&*()%5B%5D%7B%7D%5E+=#@%60,;'-_%22%60.mp4" - - // therefore the following characters need pre-encoding without "=", "*" and ";" to please the currently - // implemented decoding algorithm in Go - special := []string{"$", "!", "&", "(", ")", "+", "#", "@", ",", "-", "_"} - var args = make([]string, 0, len(special)*2) - for _, s := range special { - args = append(args, s, url.QueryEscape(s)) - } - rep := strings.NewReplacer(args...) - str = rep.Replace(str) - - return mime.ParseMediaType(str) -} diff --git a/blaze/utils_test.go b/blaze/utils_test.go index d4102e2f..6e61a25d 100644 --- a/blaze/utils_test.go +++ b/blaze/utils_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/256dpi/xo" - "github.com/stretchr/testify/assert" "github.com/256dpi/fire" "github.com/256dpi/fire/axe" @@ -64,33 +63,3 @@ func withTester(t *testing.T, fn func(*testing.T, *fire.Tester)) { fn(t, tester) }) } - -func TestParseContentDisposition(t *testing.T) { - typ, params, err := parseContentDisposition("foo") - assert.NoError(t, err) - assert.Equal(t, "foo", typ) - assert.Empty(t, params) - - _, _, err = parseContentDisposition("foo; bar") - assert.Error(t, err) - - typ, params, err = parseContentDisposition("foo; bar=baz") - assert.NoError(t, err) - assert.Equal(t, "foo", typ) - assert.Equal(t, map[string]string{"bar": "baz"}, params) - - typ, params, err = parseContentDisposition("foo; bar*=utf-8''baz") - assert.NoError(t, err) - assert.Equal(t, "foo", typ) - assert.Equal(t, map[string]string{"bar": "baz"}, params) - - typ, params, err = parseContentDisposition("foo; bar*=utf-8''A%20B") - assert.NoError(t, err) - assert.Equal(t, "foo", typ) - assert.Equal(t, map[string]string{"bar": "A B"}, params) - - typ, params, err = parseContentDisposition("foo; bar*=utf-8''file_$!&()+#@,-_.mp4") - assert.NoError(t, err) - assert.Equal(t, "foo", typ) - assert.Equal(t, map[string]string{"bar": "file_$!&()+#@,-_.mp4"}, params) -} diff --git a/go.mod b/go.mod index f5530722..f5f74d2c 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/256dpi/jsonapi/v2 v2.3.1 github.com/256dpi/lungo v0.3.4 github.com/256dpi/oauth2/v2 v2.2.0 - github.com/256dpi/serve v0.8.1 + github.com/256dpi/serve v0.8.2 github.com/256dpi/xo v0.4.4 github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef github.com/golang-jwt/jwt/v4 v4.5.0 diff --git a/go.sum b/go.sum index 293e0a61..4639f6a0 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,8 @@ github.com/256dpi/lungo v0.3.4 h1:7HsYytH+LPizorlLv2HIf/XQOoY/VdY1OzyJuwGynkw= github.com/256dpi/lungo v0.3.4/go.mod h1:28RROJ4SMldg+T7IOmp9q9Gr04zET3SQMJtuZyz2cR4= github.com/256dpi/oauth2/v2 v2.2.0 h1:j8XRN3t6sLmOgINNb2JNlBBqDbuvuReuBMRH8b0lWYA= github.com/256dpi/oauth2/v2 v2.2.0/go.mod h1:Sr+K9gSyEntfoZF3IFVoCecBgZbfuG8x5NnFinlKM3k= -github.com/256dpi/serve v0.8.1 h1:WEkPLVzVgd704Q2HpQEezysqFKoY66Ap/7wI7w1JETs= -github.com/256dpi/serve v0.8.1/go.mod h1:pZW8PLew3q20Ex+jje4/9jvc5haDE208sx088nqnZ4g= +github.com/256dpi/serve v0.8.2 h1:zy2E5cPSrl0uwC5loRt0qWXI+WyAkt4Bz8ShTBsBuPk= +github.com/256dpi/serve v0.8.2/go.mod h1:pZW8PLew3q20Ex+jje4/9jvc5haDE208sx088nqnZ4g= github.com/256dpi/xo v0.4.4 h1:xBBsWH9NEtrJGvbA2YvkGqMApGSelYXlazCK1eNhBFA= github.com/256dpi/xo v0.4.4/go.mod h1:SdPysogT8/TJdVKx8JCx8ZTGNVVrP5Eyk/a8w1saDhw= github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGLmAjMPwCCCo7Jf0W6f9slllCkkv7vyc1yOSg=