Kaynağa Gözat

Allow compression at the transport level. (#595)

* Allow compression at the transport level.

* Do not enable compression but do decompress compressed content-encodings.

* Fix import in download.go to use std compress/go.

* Fix issue with err shadowing.

* Don't overwrite resBody on download of original image.

Set content length to 0 if data was compressed.
Ewan Higgs 4 yıl önce
ebeveyn
işleme
c7fcb9b94f
1 değiştirilmiş dosya ile 17 ekleme ve 1 silme
  1. 17 1
      download.go

+ 17 - 1
download.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"compress/gzip"
 	"context"
 	"crypto/tls"
 	"fmt"
@@ -200,7 +201,22 @@ func downloadImage(ctx context.Context) (context.Context, context.CancelFunc, er
 		return ctx, func() {}, err
 	}
 
-	imgdata, err := readAndCheckImage(res.Body, int(res.ContentLength))
+	body := res.Body
+	contentLength := int(res.ContentLength)
+
+	if res.Header.Get("Content-Encoding") == "gzip" {
+		gzipBody, errGzip := gzip.NewReader(res.Body)
+		if gzipBody != nil {
+			defer gzipBody.Close()
+		}
+		if errGzip != nil {
+			return ctx, func() {}, err
+		}
+		body = gzipBody
+		contentLength = 0
+	}
+
+	imgdata, err := readAndCheckImage(body, contentLength)
 	if err != nil {
 		return ctx, func() {}, err
 	}