diff options
author | clsr <clsr@clsr.net> | 2016-11-15 16:57:51 +0100 |
---|---|---|
committer | clsr <clsr@clsr.net> | 2016-11-15 19:28:55 +0100 |
commit | d3536e6741351fb13a9f6a327637bc2a4619fea4 (patch) | |
tree | f9cf707ed2b3daf6bea2d7a07f0c1ce9efbe5c23 /storage/magic.go | |
parent | 1e55e61786eaedd59c067f4306fb8427dc92ef52 (diff) | |
download | gomf-d3536e6741351fb13a9f6a327637bc2a4619fea4.tar.gz gomf-d3536e6741351fb13a9f6a327637bc2a4619fea4.zip |
Move storage to its own packagev0.5.0
Diffstat (limited to 'storage/magic.go')
-rw-r--r-- | storage/magic.go | 31 |
1 files changed, 31 insertions, 0 deletions
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 <stdlib.h> +// #include <magic.h> +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 +} |