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

Function DetectMimeType

pkg/chat/chat.go:187–199  ·  view source on GitHub ↗

DetectMimeType returns the MIME type for a file by reading its first 512 bytes and inspecting the content (magic bytes). For text-based files that http.DetectContentType cannot distinguish (e.g. source code vs plain text), it falls back to extension matching. This is the canonical implementation use

(filePath string)

Source from the content-addressed store, hash-verified

185// it falls back to extension matching. This is the canonical implementation
186// used across all packages for consistency.
187func DetectMimeType(filePath string) string {
188 // Content sniffing — reliably detects images, PDF, etc.
189 if ct := detectMimeTypeFromFile(filePath); ct != "application/octet-stream" {
190 return ct
191 }
192
193 // http.DetectContentType returns "application/octet-stream" for text
194 // files it can't classify, so fall back to extension for those.
195 if isTextExtension(strings.ToLower(filepath.Ext(filePath))) {
196 return "text/plain"
197 }
198 return "application/octet-stream"
199}
200
201// detectMimeTypeFromFile reads the first 512 bytes of a file and uses
202// content-based detection (magic bytes) to determine the MIME type.

Callers 9

readImageFileMethod · 0.92
GetOrUploadMethod · 0.92
TestDetectMimeTypeFunction · 0.92
processFileAttachmentMethod · 0.92
processFilePartFunction · 0.85
IsImageFileFunction · 0.85
TestDetectMimeTypeFunction · 0.85

Calls 2

detectMimeTypeFromFileFunction · 0.85
isTextExtensionFunction · 0.85

Tested by 3

TestDetectMimeTypeFunction · 0.74
TestDetectMimeTypeFunction · 0.68