forked from Angey40/BaiduPCS-Go
/
file.go
63 lines (52 loc) · 1.06 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package checksum
import (
"bytes"
"fmt"
"os"
"path/filepath"
)
// EqualLengthMD5 检测md5和大小是否相同
func (lfm *LocalFileMeta) EqualLengthMD5(m *LocalFileMeta) bool {
if lfm.Length != m.Length {
return false
}
if bytes.Compare(lfm.MD5, m.MD5) != 0 {
return false
}
return true
}
// CompleteAbsPath 补齐绝对路径
func (lfm *LocalFileMeta) CompleteAbsPath() {
if filepath.IsAbs(lfm.Path) {
return
}
absPath, err := filepath.Abs(lfm.Path)
if err != nil {
return
}
lfm.Path = absPath
}
// GetFileSum 获取文件的大小, md5, 前256KB切片的 md5, crc32
func GetFileSum(localPath string, cfg *SumConfig) (lf *LocalFile, err error) {
file, err := os.Open(localPath)
if err != nil {
return nil, err
}
defer file.Close()
fileStat, err := file.Stat()
if err != nil {
return nil, err
}
if fileStat.IsDir() {
return nil, fmt.Errorf("sum %s: is a directory", localPath)
}
lf = &LocalFile{
File: file,
LocalFileMeta: LocalFileMeta{
Path: localPath,
Length: fileStat.Size(),
},
}
lf.Sum(*cfg)
return lf, nil
}