MCPcopy
hub / github.com/docker/docker-agent / isTextByContent

Function isTextByContent

pkg/chat/chat.go:356–379  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
356func 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}

Callers 1

IsTextFileFunction · 0.85

Calls 3

OpenMethod · 0.65
CloseMethod · 0.65
ReadMethod · 0.65

Tested by

no test coverage detected