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)
| 107 | // or an empty string and false on any error. It also mimics the MIMEType |
| 108 | // behaviour. |
| 109 | func 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 | |
| 129 | func TestMIMETypeFromReader(t *testing.T) { |
| 130 | someErr := errors.New("some error") |