2020-01-12 14:00:56 +01:00
|
|
|
package fs
|
2019-12-15 17:19:16 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-04-07 10:42:42 +02:00
|
|
|
const (
|
|
|
|
MimeTypeJpeg = "image/jpeg"
|
|
|
|
)
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
// MimeType returns the mime type of a file, empty string if unknown.
|
2019-12-15 17:19:16 +01:00
|
|
|
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)
|
|
|
|
}
|