From 31d3e55489653ca1ec28e2a09f20dc9db4dc8f04 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Fri, 27 Nov 2020 15:33:31 +0300 Subject: [PATCH] Add content detector Signed-off-by: Evgeniy Kulikov --- api/layer/detector.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 api/layer/detector.go diff --git a/api/layer/detector.go b/api/layer/detector.go new file mode 100644 index 00000000..3a9b7d85 --- /dev/null +++ b/api/layer/detector.go @@ -0,0 +1,26 @@ +package layer + +import ( + "io" + "net/http" + "sync" +) + +type detector struct { + io.Reader + sync.Once + + contentType string +} + +func newDetector(r io.Reader) *detector { + return &detector{Reader: r} +} + +func (d *detector) Read(data []byte) (int, error) { + d.Do(func() { + d.contentType = http.DetectContentType(data) + }) + + return d.Reader.Read(data) +}