photoprism/internal/file/mime.go
Michael Mayer e43983d579 Backend: Refactor thumbnail package #157
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
2020-01-06 14:32:15 +01:00

29 lines
473 B
Go

package file
import (
"net/http"
"os"
)
func MimeType(filename string) string {
handle, err := os.Open(filename)
if err != nil {
log.Error(err.Error())
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 {
log.Errorf("could not read file to determine mime type: %s", filename)
return ""
}
return http.DetectContentType(buffer)
}