From d3536e6741351fb13a9f6a327637bc2a4619fea4 Mon Sep 17 00:00:00 2001 From: clsr Date: Tue, 15 Nov 2016 16:57:51 +0100 Subject: Move storage to its own package --- storage/magic.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 storage/magic.go (limited to 'storage/magic.go') diff --git a/storage/magic.go b/storage/magic.go new file mode 100644 index 0000000..16f655d --- /dev/null +++ b/storage/magic.go @@ -0,0 +1,31 @@ +package storage + +// #cgo LDFLAGS: -lmagic +// #include +// #include +import "C" +import "errors" +import "unsafe" + +var magic C.magic_t + +func init() { + magic = C.magic_open(C.MAGIC_MIME_TYPE | C.MAGIC_SYMLINK | C.MAGIC_ERROR) + if magic == nil { + panic("unable to initialize libmagic") + } + if C.magic_load(magic, nil) != 0 { + C.magic_close(magic) + panic("unable to load libmagic database: " + C.GoString(C.magic_error(magic))) + } +} + +func GetMimeType(fname string) (string, error) { + cfname := C.CString(fname) + defer C.free(unsafe.Pointer(cfname)) + mime := C.magic_file(magic, cfname) + if mime == nil { + return "", errors.New(C.GoString(C.magic_error(magic))) + } + return C.GoString(mime), nil +} -- cgit