isTextByContent reads the first 8KB of a file and checks if it looks like text. A file is considered text if it's valid UTF-8 and contains no null bytes.
(filePath string)
| 354 | // isTextByContent reads the first 8KB of a file and checks if it looks like text. |
| 355 | // A file is considered text if it's valid UTF-8 and contains no null bytes. |
| 356 | func isTextByContent(filePath string) bool { |
| 357 | f, err := os.Open(filePath) |
| 358 | if err != nil { |
| 359 | return false |
| 360 | } |
| 361 | defer f.Close() |
| 362 | |
| 363 | buf := make([]byte, 8*1024) |
| 364 | n, _ := f.Read(buf) |
| 365 | if n == 0 { |
| 366 | // Empty files are treated as text |
| 367 | return true |
| 368 | } |
| 369 | |
| 370 | data := buf[:n] |
| 371 | |
| 372 | // Check for null bytes (strong binary indicator) |
| 373 | if slices.Contains(data, 0) { |
| 374 | return false |
| 375 | } |
| 376 | |
| 377 | // Check if the content is valid UTF-8 |
| 378 | return utf8.Valid(data) |
| 379 | } |
no test coverage detected