-
Notifications
You must be signed in to change notification settings - Fork 24
/
structs.go
309 lines (280 loc) · 7.61 KB
/
structs.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package baidu
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io"
"net/url"
"os"
"strings"
"time"
)
var (
ErrRefreshTokenExpired = errors.New("refresh_token expired")
ErrAccessTokenEmpty = errors.New("user not authorized ")
ErrAccessTokenExpired = errors.New("access token expired")
)
type ErrorResponse struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
TokenResponse
}
func (e *ErrorResponse) String() string {
return fmt.Sprintf("error: %s;error_description: %s", e.Error, e.ErrorDescription)
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
SessionKey string `json:"session_key"`
SessionSecret string `json:"session_secret"`
CreateAt int64 `json:"-"`
RefreshTokenCreateAt int64 `json:"-"`
}
func (t *TokenResponse) Clone() *TokenResponse {
return &TokenResponse{
AccessToken: t.AccessToken,
ExpiresIn: t.ExpiresIn,
RefreshToken: t.RefreshToken,
Scope: t.Scope,
SessionKey: t.SessionKey,
SessionSecret: t.SessionSecret,
CreateAt: t.CreateAt,
RefreshTokenCreateAt: t.RefreshTokenCreateAt,
}
}
func (t *TokenResponse) IsExpired() bool {
return time.Now().Unix() >= t.CreateAt+t.ExpiresIn
}
func (t *TokenResponse) IsRefreshTokenExpired() bool {
return time.Now().AddDate(-10, 0, 0).Unix() >= t.RefreshTokenCreateAt
}
type UserInfo struct {
ErrNo int `json:"errno"`
ErrMsg string `json:"errmsg"`
BaiduName string `json:"baidu_name"`
NetdiskName string `json:"netdisk_name"`
AvatarUrl string `json:"avatar_url"`
VipType int `json:"vip_type"`
UserId int `json:"uk"`
}
func (u *UserInfo) Clone() *UserInfo {
return &UserInfo{
ErrNo: u.ErrNo,
ErrMsg: u.ErrMsg,
BaiduName: u.BaiduName,
NetdiskName: u.NetdiskName,
AvatarUrl: u.AvatarUrl,
VipType: u.VipType,
UserId: u.UserId,
}
}
func (u *UserInfo) String() string {
b, _ := json.Marshal(u)
return string(b)
}
type PreCreateUploadFileParam struct {
Path string `json:"path"`
Size int `json:"size"`
IsDir bool `json:"is_dir"`
AutoInit int `json:"autoinit"`
RType int `json:"rtype"`
UploadId string `json:"uploadid"`
BlockList []string `json:"block_list"`
ContentMD5 string `json:"content_md5"`
SliceMD5 string `json:"slice_md5"`
LocalCTime int64 `json:"local_ctime"`
LocalMTime int64 `json:"local_mtime"`
}
func NewPreCreateUploadFileParam(filename string, path string) (*PreCreateUploadFileParam, error) {
reader, err := os.Open(filename)
if err != nil {
return nil, err
}
defer reader.Close()
info, err := reader.Stat()
if err != nil {
return nil, err
}
b := make([]byte, 4096*1024)
blockList := make([]string, 0)
for {
n, err := io.ReadFull(reader, b)
if err == io.EOF {
break
}
has := md5.Sum(b[:n])
md5str1 := fmt.Sprintf("%x", has)
blockList = append(blockList, md5str1)
}
return &PreCreateUploadFileParam{
Path: path,
Size: int(info.Size()),
IsDir: false,
AutoInit: 1,
RType: 3,
BlockList: blockList,
LocalCTime: info.ModTime().Unix(),
LocalMTime: time.Now().Unix(),
}, nil
}
func (u *PreCreateUploadFileParam) Values() url.Values {
values := url.Values{}
values.Add("path", u.Path)
values.Add("size", fmt.Sprintf("%d", u.Size))
if u.IsDir {
values.Add("is_dir", "1")
} else {
values.Add("is_dir", "0")
}
values.Add("autoinit", "1")
if u.RType > 0 {
values.Add("rtype", fmt.Sprintf("%d", u.RType))
} else {
values.Add("rtype", "0")
}
if u.UploadId != "" {
values.Add("uploadid", u.UploadId)
}
if u.BlockList != nil && len(u.BlockList) > 0 {
values.Add("block_list", fmt.Sprintf("[\"%s\"]", strings.Join(u.BlockList, "\",\"")))
}
if u.ContentMD5 != "" {
values.Add("content-md5", u.ContentMD5)
}
if u.SliceMD5 != "" {
values.Add("slice-md5", u.SliceMD5)
}
if u.LocalMTime > 0 {
values.Add("local_ctime", fmt.Sprintf("%d", u.LocalCTime))
}
if u.LocalMTime > 0 {
values.Add("local_mtime", fmt.Sprintf("%d", u.LocalMTime))
}
return values
}
func (u *PreCreateUploadFileParam) String() string {
b, _ := json.Marshal(u)
return string(b)
}
type PreCreateUploadFile struct {
ErrNo int `json:"errno"`
Path string `json:"path"`
UploadId string `json:"uploadid"`
ReturnType int `json:"return_type"`
BlockList []int `json:"block_list"`
Info UploadFileInfo `json:"info,omitempty"`
}
func (u *PreCreateUploadFile) String() string {
b, _ := json.Marshal(u)
return string(b)
}
type UploadFileInfo struct {
Size int `json:"size"`
Category int `json:"category"`
IsDir int `json:"is_dir"`
Path string `json:"path"`
FsId int64 `json:"fs_id"`
MD5 string `json:"md5"`
CTime int64 `json:"ctime"`
MTime int64 `json:"mtime"`
}
func (f UploadFileInfo) String() string {
b, _ := json.Marshal(&f)
return string(b)
}
type SuperFileParam struct {
AccessToken string
Method string
Type string
Path string
UploadId string
PartSeq int
}
func (s *SuperFileParam) Values() url.Values {
values := url.Values{}
values.Add("access_token", s.AccessToken)
values.Add("method", "upload")
values.Add("type", "tmpfile")
values.Add("path", s.Path)
values.Add("uploadid", s.UploadId)
values.Add("partseq", fmt.Sprintf("%d", s.PartSeq))
return values
}
type SuperFile struct {
ErrorNo int `json:"error_code"`
ErrorMsg string `json:"error_msg"`
Md5 string `json:"md5"`
RequestId uint64 `json:"request_id"`
}
type CreateFileParam struct {
Path string `json:"path"`
Size int `json:"size"`
IsDir bool `json:"isdir"`
RType int `json:"rtype"`
UploadId string `json:"uploadid"`
BlockList []string `json:"block_list"`
LocalCTime int64 `json:"local_ctime"`
LocalMTime int64 `json:"local_mtime"`
ZipQuality int `json:"zip_quality"`
ZipSign string `json:"zip_sign"`
IsRevision int `json:"is_revision"`
Mode int `json:"mode"`
ExifInfo string `json:"exif_info"`
}
func NewCreateFileParam(path string, size int, isDir bool) *CreateFileParam {
return &CreateFileParam{
Path: path,
Size: size,
IsDir: isDir,
}
}
func (p *CreateFileParam) Values() url.Values {
values := url.Values{}
values.Add("path", p.Path)
values.Add("size", fmt.Sprintf("%d", p.Size))
if p.IsDir {
values.Add("isdir", "1")
} else {
values.Add("isdir", "0")
}
values.Add("rtype", fmt.Sprintf("%d", p.RType))
values.Add("uploadid", p.UploadId)
if p.BlockList != nil && len(p.BlockList) > 0 {
values.Add("block_list", fmt.Sprintf("[\"%s\"]", strings.Join(p.BlockList, "\",\"")))
}
if p.LocalCTime > 0 {
values.Add("local_ctime", fmt.Sprintf("%d", p.LocalCTime))
}
if p.LocalMTime > 0 {
values.Add("local_mtime", fmt.Sprintf("%d", p.LocalMTime))
}
if p.ZipQuality > 0 {
values.Add("zip_quality", fmt.Sprintf("%d", p.ZipQuality))
}
if p.ZipSign != "" {
values.Add("zip_sign", p.ZipSign)
}
if p.Mode > 0 {
values.Add("mode", fmt.Sprintf("%d", p.Mode))
}
if p.ExifInfo != "" {
values.Add("exif_info", p.ExifInfo)
}
return values
}
func (p *CreateFileParam) String() string {
b, _ := json.Marshal(p)
return string(b)
}
type CreateFile struct {
UploadFileInfo
ErrNo int `json:"errno"`
ServerFilename string `json:"server_filename"`
}
func (p *CreateFile) String() string {
b, _ := json.Marshal(p)
return string(b)
}