Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions magicmime.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,13 @@ func (d *Decoder) TypeByFile(filename string) (string, error) {
// TypeByBuffer looks up for a blob's mimetype by its contents.
// It uses a magic number database which is described in magic(5).
func (d *Decoder) TypeByBuffer(blob []byte) (string, error) {
bytes := unsafe.Pointer(&blob[0])
out := C.magic_buffer(d.db, bytes, C.size_t(len(blob)))
var bytes unsafe.Pointer
bloblen := len(blob)
if bloblen > 0 {
bytes = unsafe.Pointer(&blob[0])
}

out := C.magic_buffer(d.db, bytes, C.size_t(bloblen))
if out == nil {
return "", errors.New(C.GoString(C.magic_error(d.db)))
}
Expand Down
17 changes: 17 additions & 0 deletions magicmime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ func TestGifBuffer(t *testing.T) {
}
}

func TestEmptyBuffer(t *testing.T) {
if err := Open(MAGIC_MIME_TYPE | MAGIC_SYMLINK | MAGIC_ERROR); err != nil {
t.Fatal(err)
}
defer Close()

var buffer []byte
expected := "application/x-empty"
mimetype, err := TypeByBuffer(buffer)
if err != nil {
panic(err)
}
if mimetype != expected {
t.Errorf("expected %s; got %s.", expected, mimetype)
}
}

func testFile(tb testing.TB, path string, expected string) {
if err := Open(MAGIC_MIME_TYPE | MAGIC_SYMLINK | MAGIC_ERROR); err != nil {
tb.Fatal(err)
Expand Down