MIMETypeFromReader takes a reader, sniffs the beginning of it, and returns the mime (if sniffed, else "") and a new reader that's the concatenation of the bytes sniffed and the remaining reader.
(r io.Reader)
| 197 | // that's the concatenation of the bytes sniffed and the remaining |
| 198 | // reader. |
| 199 | func MIMETypeFromReader(r io.Reader) (mime string, reader io.Reader) { |
| 200 | var buf bytes.Buffer |
| 201 | _, err := io.Copy(&buf, io.LimitReader(r, 1024)) |
| 202 | mime = MIMEType(buf.Bytes()) |
| 203 | if err != nil { |
| 204 | return mime, io.MultiReader(&buf, errReader{err}) |
| 205 | } |
| 206 | return mime, io.MultiReader(&buf, r) |
| 207 | } |
| 208 | |
| 209 | // MIMETypeFromReaderAt takes a ReaderAt, sniffs the beginning of it, |
| 210 | // and returns the MIME type if sniffed, else the empty string. |