-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmimeReader.go
59 lines (52 loc) · 1.16 KB
/
mimeReader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package indexer
import (
"bytes"
"emperror.dev/errors"
"io"
"net/http"
)
const bufSize = 512
type MimeReader struct {
io.Reader
buffer []byte
contentType string
}
func NewMimeReader(r io.Reader) (*MimeReader, error) {
reader := r // bufio.NewReaderSize(r, bufSize)
mr := &MimeReader{
Reader: reader,
buffer: make([]byte, 0, bufSize),
}
return mr, mr.Init()
}
func (mr *MimeReader) Init() error {
var n int64
var err error
n, err = io.CopyN(bytes.NewBuffer(mr.buffer), mr.Reader, int64(bufSize))
// n, err = mr.Reader.Read(mr.buffer)
if err != nil {
if errors.Is(err, io.EOF) {
mr.contentType = "application/octet-stream"
mr.buffer = make([]byte, 0, bufSize)
return nil
}
return errors.Wrap(err, "failed to read head")
}
mr.buffer = mr.buffer[:n]
mr.contentType = http.DetectContentType(mr.buffer)
if n == 0 {
mr.contentType = "application/octet-stream"
}
return nil
}
func (mr *MimeReader) DetectContentType() (string, error) {
return mr.contentType, nil
}
func (mr *MimeReader) Read(p []byte) (n int, err error) {
if len(mr.buffer) > 0 {
n = copy(p, mr.buffer)
mr.buffer = mr.buffer[n:]
return
}
return mr.Reader.Read(p)
}