Skip to content

Commit

Permalink
blaze: use improved media type parse from serve
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Sep 8, 2023
1 parent 79816c3 commit 8c9e556
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 65 deletions.
2 changes: 1 addition & 1 deletion blaze/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 0 additions & 30 deletions blaze/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package blaze

import (
"errors"
"mime"
"net/url"
"reflect"
"strings"

"github.com/256dpi/fire/coal"
)
Expand All @@ -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)
}
31 changes: 0 additions & 31 deletions blaze/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/256dpi/xo"
"github.com/stretchr/testify/assert"

"github.com/256dpi/fire"
"github.com/256dpi/fire/axe"
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 8c9e556

Please sign in to comment.