diff --git a/download/download.go b/download/download.go index bc03f8448e..9c611bb647 100644 --- a/download/download.go +++ b/download/download.go @@ -315,7 +315,16 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download defer m.Timer(metrics.RegoLoadBundles).Stop() baseURL := path.Join(d.client.Config().URL, d.path) - loader := bundle.NewTarballLoaderWithBaseURL(io.TeeReader(resp.Body, &buf), baseURL) + cnt := &count{} + r := io.TeeReader(resp.Body, cnt) + + var loader bundle.DirectoryLoader + if d.persist { + tee := io.TeeReader(r, &buf) + loader = bundle.NewTarballLoaderWithBaseURL(tee, baseURL) + } else { + loader = bundle.NewTarballLoaderWithBaseURL(r, baseURL) + } etag := resp.Header.Get("ETag") @@ -356,7 +365,7 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download raw: &buf, etag: etag, longPoll: isLongPollSupported(resp.Header), - size: buf.Len(), + size: cnt.Bytes(), }, nil } @@ -383,6 +392,20 @@ func (d *Downloader) download(ctx context.Context, m metrics.Metrics) (*download } } +type count struct { + total int +} + +func (c *count) Write(p []byte) (n int, err error) { + n = len(p) + c.total += n + return +} + +func (c *count) Bytes() int { + return c.total +} + func isLongPollSupported(header http.Header) bool { return header.Get("Content-Type") == "application/vnd.openpolicyagent.bundles" }