Skip to content

Commit

Permalink
Fix unsupported content type error
Browse files Browse the repository at this point in the history
  • Loading branch information
F0rzend committed Jul 20, 2022
1 parent 930fda4 commit 8e7cbb2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
14 changes: 12 additions & 2 deletions copier/stream_copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,21 @@ func DetectExtension(r *http.Response) (string, error) {
}

func extensionFromContentType(contentType string) (string, error) {
extension, err := mime.ExtensionsByType(contentType)
if !isSupportedContentType(contentType) {
return "", nil
}

extensions, err := mime.ExtensionsByType(contentType)
if err != nil {
return "", err
}
return extension[0], nil

return extensions[0], nil
}

func isSupportedContentType(contentType string) bool {
_, _, err := mime.ParseMediaType(contentType)
return err == nil
}

func extensionFromBody(body io.Reader) (string, error) {
Expand Down
12 changes: 12 additions & 0 deletions copier/stream_copier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ func TestDetectExtension(t *testing.T) {
body: "",
expected: ".mp3",
},
{
name: "wrong header",
header: "i'm wrong!",
body: "",
expected: "",
},
{
name: "extension by not found",
header: "i'm/doesn't exist",
body: "",
expected: "",
},
{
name: "by body",
header: "",
Expand Down

0 comments on commit 8e7cbb2

Please sign in to comment.