-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
94 lines (80 loc) · 1.65 KB
/
utils.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package pixiv_api_go
import (
"crypto/sha1"
"crypto/sha256"
"fmt"
"hash"
"io"
"os"
"path/filepath"
)
func CheckAndMkdir(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return os.MkdirAll(dir, 0755)
}
return nil
}
type HashType int
const (
NoHash HashType = -1
Sha1 HashType = iota
Sha256
)
func WriteFileAndCalHash(reader io.Reader, filename string, hashType HashType) (int64, string, error) {
dirName := filepath.Dir(filename)
err := CheckAndMkdir(dirName)
if err != nil {
return 0, "", err
}
file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
if err != nil {
return 0, "", err
}
defer func() {
_ = file.Close()
}()
if hashType == NoHash {
size, err := io.Copy(file, reader)
return size, "", err
}
var h hash.Hash
switch hashType {
case Sha256:
h = sha256.New()
break
case Sha1:
h = sha1.New()
break
default:
h = sha1.New()
}
r := io.TeeReader(reader, h)
size, err := io.Copy(file, r)
if err != nil {
return 0, "", err
}
sum := fmt.Sprintf("%x", h.Sum(nil))
return size, sum, nil
}
func WriteFile(reader io.Reader, filename string) (int64, error) {
size, _, err := WriteFileAndCalHash(reader, filename, NoHash)
return size, err
}
func WriteFIleCalSha1(reader io.Reader, filename string) (int64, string, error) {
return WriteFileAndCalHash(reader, filename, Sha1)
}
func FileSha1Sum(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer func() {
_ = file.Close()
}()
h := sha1.New()
if _, err := io.Copy(h, file); err != nil {
return "", nil
}
sum := fmt.Sprintf("%x", h.Sum(nil))
return sum, nil
}