photoprism/pkg/fs/mime.go
Michael Mayer 138dabd0c8 Metadata: Use mime type to determine file format and exif parser #391
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-07-19 16:39:43 +02:00

35 lines
575 B
Go

package fs
import (
"net/http"
"os"
)
const (
MimeTypeJpeg = "image/jpeg"
MimeTypePng = "image/png"
MimeTypeGif = "image/gif"
MimeTypeBitmap = "image/bmp"
)
// 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)
}