aboutsummaryrefslogtreecommitdiff
path: root/weed/util
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2020-06-19 22:45:27 -0700
committerChris Lu <chris.lu@gmail.com>2020-06-19 22:45:27 -0700
commite912fd15e32179ca532c23011ce7bc9186586ba4 (patch)
tree4e5065c2b0df4ff4420e434a9c464b88e33072fb /weed/util
parent16fe132a20b859a57ba51cbb8824dbae69513a55 (diff)
downloadseaweedfs-e912fd15e32179ca532c23011ce7bc9186586ba4.tar.xz
seaweedfs-e912fd15e32179ca532c23011ce7bc9186586ba4.zip
renaming
Diffstat (limited to 'weed/util')
-rw-r--r--weed/util/compression.go27
-rw-r--r--weed/util/http_util.go10
2 files changed, 31 insertions, 6 deletions
diff --git a/weed/util/compression.go b/weed/util/compression.go
index 1f778b5d5..f6315ebc2 100644
--- a/weed/util/compression.go
+++ b/weed/util/compression.go
@@ -25,7 +25,25 @@ func GzipData(input []byte) ([]byte, error) {
}
return buf.Bytes(), nil
}
-func UnGzipData(input []byte) ([]byte, error) {
+func UnCompressData(input []byte) ([]byte, error) {
+ if IsGzippedContent(input) {
+ return ungzipData(input)
+ }
+
+}
+
+func ungzipData(input []byte) ([]byte, error) {
+ buf := bytes.NewBuffer(input)
+ r, _ := gzip.NewReader(buf)
+ defer r.Close()
+ output, err := ioutil.ReadAll(r)
+ if err != nil {
+ glog.V(2).Infoln("error uncompressing data:", err)
+ }
+ return output, err
+}
+
+func ungzipData(input []byte) ([]byte, error) {
buf := bytes.NewBuffer(input)
r, _ := gzip.NewReader(buf)
defer r.Close()
@@ -51,6 +69,13 @@ func IsGzippable(ext, mtype string, data []byte) bool {
return isMostlyText
}
+func IsGzippedContent(data []byte) bool {
+ if len(data) < 2 {
+ return false
+ }
+ return data[0] == 31 && data[1] == 139
+}
+
/*
* Default more not to gzip since gzip can be done on client side.
*/func IsGzippableFileType(ext, mtype string) (shouldBeZipped, iAmSure bool) {
diff --git a/weed/util/http_util.go b/weed/util/http_util.go
index 5df79a7be..51748e92a 100644
--- a/weed/util/http_util.go
+++ b/weed/util/http_util.go
@@ -189,11 +189,11 @@ func NormalizeUrl(url string) string {
return "http://" + url
}
-func ReadUrl(fileUrl string, cipherKey []byte, isGzipped bool, isFullChunk bool, offset int64, size int, buf []byte) (int64, error) {
+func ReadUrl(fileUrl string, cipherKey []byte, isContentCompressed bool, isFullChunk bool, offset int64, size int, buf []byte) (int64, error) {
if cipherKey != nil {
var n int
- err := readEncryptedUrl(fileUrl, cipherKey, isGzipped, isFullChunk, offset, size, func(data []byte) {
+ err := readEncryptedUrl(fileUrl, cipherKey, isContentCompressed, isFullChunk, offset, size, func(data []byte) {
n = copy(buf, data)
})
return int64(n), err
@@ -300,7 +300,7 @@ func ReadUrlAsStream(fileUrl string, cipherKey []byte, isContentGzipped bool, is
}
-func readEncryptedUrl(fileUrl string, cipherKey []byte, isContentGzipped bool, isFullChunk bool, offset int64, size int, fn func(data []byte)) error {
+func readEncryptedUrl(fileUrl string, cipherKey []byte, isContentCompressed bool, isFullChunk bool, offset int64, size int, fn func(data []byte)) error {
encryptedData, err := Get(fileUrl)
if err != nil {
return fmt.Errorf("fetch %s: %v", fileUrl, err)
@@ -309,8 +309,8 @@ func readEncryptedUrl(fileUrl string, cipherKey []byte, isContentGzipped bool, i
if err != nil {
return fmt.Errorf("decrypt %s: %v", fileUrl, err)
}
- if isContentGzipped {
- decryptedData, err = UnGzipData(decryptedData)
+ if isContentCompressed {
+ decryptedData, err = UnCompressData(decryptedData)
if err != nil {
return fmt.Errorf("unzip decrypt %s: %v", fileUrl, err)
}