Skip to content

Commit

Permalink
Return inode/x-empty mimetype for empty inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-vasile committed Mar 26, 2019
1 parent 2c4d53c commit 01e1e30
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mime.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

// Detect returns the mime type and extension of the provided byte slice.
func Detect(in []byte) (mime, extension string) {
if len(in) == 0 {
return "inode/x-empty", ""
}
n := Root.match(in, Root)
return n.Mime(), n.Extension()
}
Expand Down
6 changes: 6 additions & 0 deletions mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ func TestFaultyInput(t *testing.T) {
}
}

func TestEmptyInput(t *testing.T) {
if m, _ := Detect([]byte{}); m != "inode/x-empty" {
t.Errorf("failed to detect empty file")
}
}

// `foobar` func matches inputs starting with the string "foobar"
// `foobarNode` is the node holding the mimetype and extension to be returned
// when the `foobar` func returns true for an input
Expand Down

0 comments on commit 01e1e30

Please sign in to comment.