-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
161 lines (144 loc) · 2.9 KB
/
copy.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package fs
import (
"github.com/hopeio/cherry/utils/crypto/md5"
"io"
"os"
)
type mode int
const (
Cover mode = iota
SameNameSkip
SameNameAndMd5Skip
// TODO
sameNameRename
)
func (c mode) handle(dst string, src io.Reader) (newname string, skip bool, err error) {
switch c {
case Cover:
return "", false, nil
case SameNameSkip:
if IsExist(dst) {
return "", true, nil
}
case SameNameAndMd5Skip:
if IsExist(dst) {
md51, err := Md5(dst)
if err != nil {
return "", false, err
}
md52, err := md5.EncodeReaderString(src)
if err != nil {
return "", false, err
}
if md51 == md52 {
return "", true, nil
}
}
}
return "", false, nil
}
// Copy : General Approach
func Copy(src, dst string) error {
r, err := os.Open(src)
if err != nil {
return err
}
defer r.Close()
return CreateFromReader(dst, r)
}
func CopyByMode(src, dst string, c mode) error {
r, err := os.Open(src)
if err != nil {
return err
}
defer r.Close()
_, skip, err := c.handle(dst, r)
if err != nil {
return err
}
if skip {
return nil
}
return CreateFromReader(dst, r)
}
const DownloadKey = ".downloading"
func CreateFromReader(filepath string, reader io.Reader) error {
f, err := Create(filepath)
if err != nil {
return err
}
if _, err = io.Copy(f, reader); err != nil {
f.Close()
os.Remove(filepath)
return err
}
if err = f.Close(); err != nil {
os.Remove(filepath)
return err
}
return nil
}
func CreateFromReaderByMode(filepath string, reader io.Reader, c mode) error {
_, skip, err := c.handle(filepath, reader)
if err != nil {
return err
}
if skip {
return nil
}
return CreateFromReader(filepath, reader)
}
func Download(filepath string, reader io.Reader) error {
tmpFilepath := filepath + DownloadKey
err := CreateFromReader(tmpFilepath, reader)
if err != nil {
return err
}
return os.Rename(tmpFilepath, filepath)
}
func DownloadByMode(filepath string, reader io.Reader, c mode) error {
_, skip, err := c.handle(filepath, reader)
if err != nil {
return err
}
if skip {
return nil
}
return Download(filepath, reader)
}
// CopyDirByMode 递归复制目录
func CopyDirByMode(src, dst string, c mode) error {
if src[len(src)-1] == os.PathSeparator {
src = src[:len(src)-1]
}
if dst[len(dst)-1] == os.PathSeparator {
dst = dst[:len(dst)-1]
}
err := os.MkdirAll(dst, os.ModePerm)
if err != nil {
return err
}
entries, err := os.ReadDir(src)
if len(entries) == 0 {
return nil
}
for _, entry := range entries {
entityName := entry.Name()
if entry.IsDir() {
err = CopyDirByMode(src+PathSeparator+entityName, dst+PathSeparator+entityName, c)
if err != nil {
return err
}
} else {
err = CopyByMode(src+PathSeparator+entityName, dst+PathSeparator+entityName, c)
if err != nil {
return err
}
}
}
return nil
}
// CopyDir 递归复制目录
func CopyDir(src, dst string) error {
return CopyDirByMode(src, dst, Cover)
}