photoprism/pkg/fs/mime.go
Michael Mayer f8a45b14d9 Backend: Move reusable packages to pkg/
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-01-12 14:00:56 +01:00

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)
}