|
|
@ -2,7 +2,6 @@ package operation |
|
|
|
|
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"compress/flate" |
|
|
|
"crypto/md5" |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
@ -59,80 +58,23 @@ func UploadData(uploadUrl string, filename string, cipher bool, data []byte, isI |
|
|
|
func Upload(uploadUrl string, filename string, cipher bool, reader io.Reader, isInputGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error) { |
|
|
|
hash := md5.New() |
|
|
|
reader = io.TeeReader(reader, hash) |
|
|
|
uploadResult, err = doUpload(uploadUrl, filename, cipher, reader, isInputGzipped, mtype, pairMap, flate.BestSpeed, jwt) |
|
|
|
uploadResult, err = doUpload(uploadUrl, filename, cipher, reader, isInputGzipped, mtype, pairMap, jwt) |
|
|
|
if uploadResult != nil { |
|
|
|
uploadResult.Md5 = fmt.Sprintf("%x", hash.Sum(nil)) |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func doUploadData(uploadUrl string, filename string, cipher bool, data []byte, isInputGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error) { |
|
|
|
contentIsGzipped := isInputGzipped |
|
|
|
shouldGzipNow := false |
|
|
|
if !isInputGzipped { |
|
|
|
if shouldBeZipped, iAmSure := util.IsGzippableFileType(filepath.Base(filename), mtype); mtype == "" || iAmSure && shouldBeZipped { |
|
|
|
shouldGzipNow = true |
|
|
|
contentIsGzipped = true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var clearDataLen int |
|
|
|
|
|
|
|
// gzip if possible
|
|
|
|
// this could be double copying
|
|
|
|
clearDataLen = len(data) |
|
|
|
if shouldGzipNow { |
|
|
|
data, err = util.GzipData(data) |
|
|
|
} else if isInputGzipped { |
|
|
|
// just to get the clear data length
|
|
|
|
clearData, err := util.UnGzipData(data) |
|
|
|
if err == nil { |
|
|
|
clearDataLen = len(clearData) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if cipher { |
|
|
|
// encrypt(gzip(data))
|
|
|
|
|
|
|
|
// encrypt
|
|
|
|
cipherKey := util.GenCipherKey() |
|
|
|
encryptedData, encryptionErr := util.Encrypt(data, cipherKey) |
|
|
|
if encryptionErr != nil { |
|
|
|
err = fmt.Errorf("encrypt input: %v", encryptionErr) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// upload data
|
|
|
|
uploadResult, err = upload_content(uploadUrl, func(w io.Writer) (err error) { |
|
|
|
_, err = w.Write(encryptedData) |
|
|
|
return |
|
|
|
}, "", false, "", nil, jwt) |
|
|
|
if uploadResult != nil { |
|
|
|
uploadResult.Name = filename |
|
|
|
uploadResult.Mime = mtype |
|
|
|
uploadResult.CipherKey = cipherKey |
|
|
|
} |
|
|
|
} else { |
|
|
|
// upload data
|
|
|
|
uploadResult, err = upload_content(uploadUrl, func(w io.Writer) (err error) { |
|
|
|
_, err = w.Write(data) |
|
|
|
return |
|
|
|
}, filename, contentIsGzipped, mtype, pairMap, jwt) |
|
|
|
} |
|
|
|
|
|
|
|
if uploadResult == nil { |
|
|
|
func doUpload(uploadUrl string, filename string, cipher bool, reader io.Reader, isInputGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error) { |
|
|
|
data, readErr := ioutil.ReadAll(reader) |
|
|
|
if readErr != nil { |
|
|
|
err = fmt.Errorf("read input: %v", readErr) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
uploadResult.Size = uint32(clearDataLen) |
|
|
|
if contentIsGzipped { |
|
|
|
uploadResult.Gzip = 1 |
|
|
|
} |
|
|
|
|
|
|
|
return uploadResult, err |
|
|
|
return doUploadData(uploadUrl, filename, cipher, data, isInputGzipped, mtype, pairMap, jwt) |
|
|
|
} |
|
|
|
|
|
|
|
func doUpload(uploadUrl string, filename string, cipher bool, reader io.Reader, isInputGzipped bool, mtype string, pairMap map[string]string, compression int, jwt security.EncodedJwt) (uploadResult *UploadResult, err error) { |
|
|
|
func doUploadData(uploadUrl string, filename string, cipher bool, data []byte, isInputGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (uploadResult *UploadResult, err error) { |
|
|
|
contentIsGzipped := isInputGzipped |
|
|
|
shouldGzipNow := false |
|
|
|
if !isInputGzipped { |
|
|
@ -146,11 +88,6 @@ func doUpload(uploadUrl string, filename string, cipher bool, reader io.Reader, |
|
|
|
|
|
|
|
// gzip if possible
|
|
|
|
// this could be double copying
|
|
|
|
data, readErr := ioutil.ReadAll(reader) |
|
|
|
if readErr != nil { |
|
|
|
err = fmt.Errorf("read input: %v", readErr) |
|
|
|
return |
|
|
|
} |
|
|
|
clearDataLen = len(data) |
|
|
|
if shouldGzipNow { |
|
|
|
data, err = util.GzipData(data) |
|
|
@ -182,7 +119,6 @@ func doUpload(uploadUrl string, filename string, cipher bool, reader io.Reader, |
|
|
|
uploadResult.Name = filename |
|
|
|
uploadResult.Mime = mtype |
|
|
|
uploadResult.CipherKey = cipherKey |
|
|
|
uploadResult.Size = uint32(clearDataLen) |
|
|
|
} |
|
|
|
} else { |
|
|
|
// upload data
|
|
|
|