frostfs-s3-gw/api/handler/detector.go
Denis Kirillov b00ad03ddc [#549] Move layer logic to handler
Signed-off-by: Denis Kirillov <denis@nspcc.ru>
2022-12-02 12:40:43 +03:00

58 lines
993 B
Go

package handler
import (
"io"
"net/http"
)
type (
detector struct {
io.Reader
err error
data []byte
}
errReader struct {
data []byte
err error
offset int
}
)
const contentTypeDetectSize = 512
func newReader(data []byte, err error) *errReader {
return &errReader{data: data, err: err}
}
func (r *errReader) Read(b []byte) (int, error) {
if r.offset >= len(r.data) {
return 0, io.EOF
}
n := copy(b, r.data[r.offset:])
r.offset += n
if r.offset >= len(r.data) {
return n, r.err
}
return n, nil
}
func newDetector(reader io.Reader) *detector {
return &detector{
data: make([]byte, contentTypeDetectSize),
Reader: reader,
}
}
func (d *detector) Detect() (string, error) {
n, err := d.Reader.Read(d.data)
if err != nil && err != io.EOF {
d.err = err
return "", err
}
d.data = d.data[:n]
return http.DetectContentType(d.data), nil
}
func (d *detector) MultiReader() io.Reader {
return io.MultiReader(newReader(d.data, d.err), d.Reader)
}