github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/internal/pcsfunctions/pcsupload/utils.go (about)

     1  package pcsupload
     2  
     3  import (
     4  	"crypto/md5"
     5  	"fmt"
     6  	"github.com/qjfoidnh/BaiduPCS-Go/baidupcs"
     7  	"io"
     8  	"strconv"
     9  )
    10  
    11  func getBlockSize(fileSize int64) int64 {
    12  	blockNum := fileSize / baidupcs.MinUploadBlockSize
    13  	if blockNum > 999 {
    14  		return fileSize/999 + 1
    15  	}
    16  	return baidupcs.MinUploadBlockSize
    17  }
    18  
    19  
    20  func creaetDataOffset(contentMD5 string, uk, dataTime, fileSize, subSize int64) (offset int64, err error) {
    21  	h := md5.New()
    22  	ts := strconv.FormatInt(dataTime, 10)
    23  	sumStr := fmt.Sprintf("%d%s%s", uk, contentMD5, ts)
    24  	io.WriteString(h, sumStr)
    25  	mixedMD5 := fmt.Sprintf("%x", h.Sum(nil))
    26  	rawOffset, err := strconv.ParseInt(mixedMD5[0:8], 16, 64)
    27  	if err != nil {
    28  		return
    29  	}
    30  	if fileSize - subSize + 1 <= 1 {
    31  		offset = 0
    32  		return
    33  	}
    34  	offset = rawOffset % (fileSize - subSize + 1)
    35  	return
    36  }