MCPcopy
hub / github.com/perkeep/perkeep / runFileCmd

Function runFileCmd

internal/magic/magic_test.go:109–127  ·  view source on GitHub ↗

runFileCmd runs the file utility and returns the mime-type and true or an empty string and false on any error. It also mimics the MIMEType behaviour.

(data []byte)

Source from the content-addressed store, hash-verified

107// or an empty string and false on any error. It also mimics the MIMEType
108// behaviour.
109func runFileCmd(data []byte) (string, bool) {
110 cmd := exec.Command("file", "--mime-type", "-")
111 cmd.Stdin = bytes.NewReader(data)
112 var out bytes.Buffer
113 cmd.Stdout = &out
114 if err := cmd.Run(); err != nil {
115 return "", false
116 }
117 outString := out.String()
118 idx := strings.LastIndex(outString, " ")
119 if idx == -1 {
120 return "", false
121 }
122 mime := outString[idx+1 : len(outString)-1]
123 if mime != "application/octet-stream" && mime != "text/plain" {
124 return mime, true
125 }
126 return "", true
127}
128
129func TestMIMETypeFromReader(t *testing.T) {
130 someErr := errors.New("some error")

Callers 1

TestMagicFunction · 0.85

Calls 3

CommandMethod · 0.65
RunMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected