detectMimeTypeFromFile reads the first 512 bytes of a file and uses content-based detection (magic bytes) to determine the MIME type.
(filePath string)
| 201 | // detectMimeTypeFromFile reads the first 512 bytes of a file and uses |
| 202 | // content-based detection (magic bytes) to determine the MIME type. |
| 203 | func detectMimeTypeFromFile(filePath string) string { |
| 204 | f, err := os.Open(filePath) |
| 205 | if err != nil { |
| 206 | return "application/octet-stream" |
| 207 | } |
| 208 | defer f.Close() |
| 209 | |
| 210 | buf := make([]byte, 512) |
| 211 | n, _ := f.Read(buf) |
| 212 | if n == 0 { |
| 213 | return "application/octet-stream" |
| 214 | } |
| 215 | return DetectMimeTypeByContent(buf[:n]) |
| 216 | } |
| 217 | |
| 218 | // IsImageMimeType returns true if the MIME type is a supported image type. |
| 219 | func IsImageMimeType(mimeType string) bool { |
no test coverage detected