github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/pcsutil/checksum/file.go (about)

     1  package checksum
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/fzfile/BaiduPCS-Go/baidupcs"
     6  	"path/filepath"
     7  )
     8  
     9  // EqualLengthMD5 检测md5和大小是否相同
    10  func (lfm *LocalFileMeta) EqualLengthMD5(m *LocalFileMeta) bool {
    11  	if lfm.Length != m.Length {
    12  		return false
    13  	}
    14  	if bytes.Compare(lfm.MD5, m.MD5) != 0 {
    15  		return false
    16  	}
    17  	return true
    18  }
    19  
    20  // CompleteAbsPath 补齐绝对路径
    21  func (lfm *LocalFileMeta) CompleteAbsPath() {
    22  	if filepath.IsAbs(lfm.Path) {
    23  		return
    24  	}
    25  
    26  	absPath, err := filepath.Abs(lfm.Path)
    27  	if err != nil {
    28  		return
    29  	}
    30  
    31  	lfm.Path = absPath
    32  }
    33  
    34  // GetFileSum 获取文件的大小, md5, 前256KB切片的 md5, crc32
    35  func GetFileSum(localPath string, flag int) (lfc *LocalFileChecksum, err error) {
    36  	lfc = NewLocalFileChecksum(localPath, int(baidupcs.SliceMD5Size))
    37  	defer lfc.Close()
    38  
    39  	err = lfc.OpenPath()
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	err = lfc.Sum(flag)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	return lfc, nil
    49  }