1
0
Fork 0
mirror of https://github.com/chrislusf/seaweedfs synced 2024-07-02 15:16:51 +02:00

filer: avoid encryption and compression at the same time

fix https://github.com/chrislusf/seaweedfs/issues/1828
This commit is contained in:
Chris Lu 2021-02-22 12:22:49 -08:00
parent 62191b08ea
commit 44bdfb2d15

View file

@ -130,7 +130,8 @@ func doUploadData(uploadUrl string, filename string, cipher bool, data []byte, i
// gzip if possible // gzip if possible
// this could be double copying // this could be double copying
clearDataLen = len(data) clearDataLen = len(data)
if shouldGzipNow { clearData := data
if shouldGzipNow && !cipher {
compressed, compressErr := util.GzipData(data) compressed, compressErr := util.GzipData(data)
// fmt.Printf("data is compressed from %d ==> %d\n", len(data), len(compressed)) // fmt.Printf("data is compressed from %d ==> %d\n", len(data), len(compressed))
if compressErr == nil { if compressErr == nil {
@ -139,7 +140,7 @@ func doUploadData(uploadUrl string, filename string, cipher bool, data []byte, i
} }
} else if isInputCompressed { } else if isInputCompressed {
// just to get the clear data length // just to get the clear data length
clearData, err := util.DecompressData(data) clearData, err = util.DecompressData(data)
if err == nil { if err == nil {
clearDataLen = len(clearData) clearDataLen = len(clearData)
} }
@ -150,7 +151,7 @@ func doUploadData(uploadUrl string, filename string, cipher bool, data []byte, i
// encrypt // encrypt
cipherKey := util.GenCipherKey() cipherKey := util.GenCipherKey()
encryptedData, encryptionErr := util.Encrypt(data, cipherKey) encryptedData, encryptionErr := util.Encrypt(clearData, cipherKey)
if encryptionErr != nil { if encryptionErr != nil {
err = fmt.Errorf("encrypt input: %v", encryptionErr) err = fmt.Errorf("encrypt input: %v", encryptionErr)
return return
@ -161,26 +162,26 @@ func doUploadData(uploadUrl string, filename string, cipher bool, data []byte, i
_, err = w.Write(encryptedData) _, err = w.Write(encryptedData)
return return
}, "", false, len(encryptedData), "", nil, jwt) }, "", false, len(encryptedData), "", nil, jwt)
if uploadResult != nil { if uploadResult == nil {
uploadResult.Name = filename return
uploadResult.Mime = mtype
uploadResult.CipherKey = cipherKey
} }
uploadResult.Name = filename
uploadResult.Mime = mtype
uploadResult.CipherKey = cipherKey
uploadResult.Size = uint32(clearDataLen)
} else { } else {
// upload data // upload data
uploadResult, err = upload_content(uploadUrl, func(w io.Writer) (err error) { uploadResult, err = upload_content(uploadUrl, func(w io.Writer) (err error) {
_, err = w.Write(data) _, err = w.Write(data)
return return
}, filename, contentIsGzipped, len(data), mtype, pairMap, jwt) }, filename, contentIsGzipped, len(data), mtype, pairMap, jwt)
} if uploadResult == nil {
return
if uploadResult == nil { }
return uploadResult.Size = uint32(clearDataLen)
} if contentIsGzipped {
uploadResult.Gzip = 1
uploadResult.Size = uint32(clearDataLen) }
if contentIsGzipped {
uploadResult.Gzip = 1
} }
return uploadResult, err return uploadResult, err