f8a45b14d9
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
28 lines
443 B
Go
28 lines
443 B
Go
package fs
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
// MimeType returns the mime type of a file, empty string if unknown.
|
|
func MimeType(filename string) string {
|
|
handle, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
defer handle.Close()
|
|
|
|
// Only the first 512 bytes are used to sniff the content type.
|
|
buffer := make([]byte, 512)
|
|
|
|
_, err = handle.Read(buffer)
|
|
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
return http.DetectContentType(buffer)
|
|
}
|