I recently discovered rakyll’s new Go library magicmime
on GitHub. In simple words, this library provides MIME type detection for files
or buffers in Go. For example, if you have a file uploaded to your application,
you might want to figure out its MIME type (e.g. if it is an image or a video file).
This project is a demo of how to do C bindings in Go: It uses
libmagic(3)
to make calls directly to this native library. You
might want to go ahead and check the source code, it is rather
easy to read. Here is an example though:
import "github.com/rakyll/magicmime"
...
mime, _ := magicmime.TypeByFile("files/some.jpg")
// mime will be "image/jpeg" here
A real-world use of this I can think of could be setting mime types of blobs
uploaded to cloud storage services. For example, current version of
Windows Azure Client Library for Go does not implement auto-detection
of mime types and Azure API defaults to saving blobs as application/octet-stream
, which leads
to weird issues e.g downloading images as files instead of
viewing them in browsers. This can be fixed using magicmime.
I got a tiny commit in this project, hopefully you’ll see me contributing to more [1] [2] Go projects and come up with new ones.
Leave your thoughts