forked from garyvalue/bdpass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbdpass.go
64 lines (55 loc) · 1.06 KB
/
bdpass.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
64
package bdpass
import (
"crypto/md5"
"encoding/hex"
"hash/crc32"
"io"
"os"
"path/filepath"
)
const (
_size = 256 * 1024
)
type RapidUploadMeta struct {
Filename string
ContentLength int64
ContentMD5 string
SliceMD5 string
ContentCRC32 uint32
}
func Stat(filename string) (*RapidUploadMeta, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
meta := &RapidUploadMeta{
Filename: filepath.Base(filename),
}
fi, err := file.Stat()
if err != nil {
return nil, err
}
meta.ContentLength = fi.Size()
data := make([]byte, _size)
n, err := file.Read(data)
if err != nil {
return nil, err
}
sliceMD5 := md5.Sum(data[:n])
meta.SliceMD5 = hex.EncodeToString(sliceMD5[:])
hash := md5.New()
hash32 := crc32.NewIEEE()
dst := io.MultiWriter(hash, hash32)
_, err = dst.Write(data[:n])
if err != nil {
return nil, err
}
_, err = io.Copy(dst, file)
if err != nil {
return nil, err
}
meta.ContentMD5 = hex.EncodeToString(hash.Sum(nil))
meta.ContentCRC32 = hash32.Sum32()
return meta, nil
}