-
Notifications
You must be signed in to change notification settings - Fork 310
/
upload_database.go
198 lines (169 loc) · 4.29 KB
/
upload_database.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package pcsupload
import (
"errors"
"github.com/felixonmars/BaiduPCS-Go/internal/pcsconfig"
"github.com/felixonmars/BaiduPCS-Go/pcsutil/checksum"
"github.com/felixonmars/BaiduPCS-Go/pcsutil/converter"
"github.com/felixonmars/BaiduPCS-Go/pcsutil/jsonhelper"
"github.com/felixonmars/BaiduPCS-Go/requester/uploader"
"os"
"path/filepath"
"strings"
"time"
)
type (
// Uploading 未完成上传的信息
Uploading struct {
*checksum.LocalFileMeta
State *uploader.InstanceState `json:"state"`
}
// UploadingDatabase 未完成上传的数据库
UploadingDatabase struct {
UploadingList []*Uploading `json:"upload_state"`
Timestamp int64 `json:"timestamp"`
dataFile *os.File
}
)
// NewUploadingDatabase 初始化未完成上传的数据库, 从库中读取内容
func NewUploadingDatabase() (ud *UploadingDatabase, err error) {
file, err := os.OpenFile(filepath.Join(pcsconfig.GetConfigDir(), UploadingFileName), os.O_CREATE|os.O_RDWR, 0777)
if err != nil {
return nil, err
}
ud = &UploadingDatabase{
dataFile: file,
}
info, err := file.Stat()
if err != nil {
return nil, err
}
if info.Size() <= 0 {
return ud, nil
}
err = jsonhelper.UnmarshalData(file, ud)
if err != nil {
return nil, err
}
return ud, nil
}
// Save 保存内容
func (ud *UploadingDatabase) Save() error {
if ud.dataFile == nil {
return errors.New("dataFile is nil")
}
ud.Timestamp = time.Now().Unix()
var (
builder = &strings.Builder{}
err = jsonhelper.MarshalData(builder, ud)
)
if err != nil {
panic(err)
}
err = ud.dataFile.Truncate(int64(builder.Len()))
if err != nil {
return err
}
str := builder.String()
_, err = ud.dataFile.WriteAt(converter.ToBytes(str), 0)
if err != nil {
return err
}
return nil
}
// UpdateUploading 更新正在上传
func (ud *UploadingDatabase) UpdateUploading(meta *checksum.LocalFileMeta, state *uploader.InstanceState) {
if meta == nil {
return
}
meta.CompleteAbsPath()
for k, uploading := range ud.UploadingList {
if uploading.LocalFileMeta == nil {
continue
}
if uploading.LocalFileMeta.EqualLengthMD5(meta) || uploading.LocalFileMeta.Path == meta.Path {
ud.UploadingList[k].State = state
return
}
}
ud.UploadingList = append(ud.UploadingList, &Uploading{
LocalFileMeta: meta,
State: state,
})
}
func (ud *UploadingDatabase) deleteIndex(k int) {
ud.UploadingList = append(ud.UploadingList[:k], ud.UploadingList[k+1:]...)
}
// Delete 删除
func (ud *UploadingDatabase) Delete(meta *checksum.LocalFileMeta) bool {
if meta == nil {
return false
}
meta.CompleteAbsPath()
for k, uploading := range ud.UploadingList {
if uploading.LocalFileMeta == nil {
continue
}
if uploading.LocalFileMeta.EqualLengthMD5(meta) || uploading.LocalFileMeta.Path == meta.Path {
ud.deleteIndex(k)
return true
}
}
return false
}
// Search 搜索
func (ud *UploadingDatabase) Search(meta *checksum.LocalFileMeta) *uploader.InstanceState {
if meta == nil {
return nil
}
meta.CompleteAbsPath()
ud.clearModTimeChange()
for _, uploading := range ud.UploadingList {
if uploading.LocalFileMeta == nil {
continue
}
if uploading.LocalFileMeta.EqualLengthMD5(meta) {
return uploading.State
}
if uploading.LocalFileMeta.Path == meta.Path {
// 移除旧的信息
// 目前只是比较了文件大小
if meta.Length != uploading.LocalFileMeta.Length {
ud.Delete(meta)
return nil
}
// 覆盖数据
meta.MD5 = uploading.LocalFileMeta.MD5
meta.SliceMD5 = uploading.LocalFileMeta.SliceMD5
return uploading.State
}
}
return nil
}
func (ud *UploadingDatabase) clearModTimeChange() {
for i := 0; i < len(ud.UploadingList); i++ {
uploading := ud.UploadingList[i]
if uploading.LocalFileMeta == nil {
continue
}
if uploading.ModTime == -1 { // 忽略
continue
}
info, err := os.Stat(uploading.LocalFileMeta.Path)
if err != nil {
ud.deleteIndex(i)
i--
pcsUploadVerbose.Warnf("clear invalid file path: %s, err: %s\n", uploading.LocalFileMeta.Path, err)
continue
}
if uploading.LocalFileMeta.ModTime != info.ModTime().Unix() {
ud.deleteIndex(i)
i--
pcsUploadVerbose.Infof("clear modified file path: %s\n", uploading.LocalFileMeta.Path)
continue
}
}
}
// Close 关闭数据库
func (ud *UploadingDatabase) Close() error {
return ud.dataFile.Close()
}