-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary.go
228 lines (190 loc) · 5.28 KB
/
library.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
package seafile
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
const (
LibraryTypeMine = "mine" //我的资料库类型
LibraryTypeShared = "shared" //私人共享给我的资料库类型
LibraryTypeGroup = "group" //群组共享给我的资料库类型
LibraryTypeOrg = "org" //公共资料库类型
)
//资料库
type Library struct {
Id string
Name string
Type string
Root string
Owner string
Permission string
Encrypted bool
Virtual bool
Version int
Mtime int
Size int
MtimeRelative string `json:"mtime_relative"`
HeadCommitId string `json:"head_commit_id"`
SizeFormatted string `json:"size_formatted"`
client *Client
}
//获取可用的资料库
func (cli *Client) ListAllLibraries() ([]*Library, error) {
return cli.ListLibrariesByType("")
}
//获取拥有的资料库
func (cli *Client) ListOwnedLibraries() ([]*Library, error) {
return cli.ListLibrariesByType(LibraryTypeMine)
}
//获取私人共享而来的资料库
func (cli *Client) ListSharedLibraries() ([]*Library, error) {
return cli.ListLibrariesByType(LibraryTypeShared)
}
//获取群组共享而来的资料库
func (cli *Client) ListGroupLibraries() ([]*Library, error) {
return cli.ListLibrariesByType(LibraryTypeGroup)
}
//获取公共的资料库
func (cli *Client) ListOrgLibraries() ([]*Library, error) {
return cli.ListLibrariesByType(LibraryTypeOrg)
}
//获取指定类型的资料库
func (cli *Client) ListLibrariesByType(libType string) ([]*Library, error) {
uri := "/repos/"
if libType != "" {
uri += "?type=" + libType
}
resp, err := cli.doRequest("GET", uri, nil, nil)
if err != nil {
return nil, fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
info := []*Library{}
err = json.NewDecoder(resp.Body).Decode(&info)
if err != nil {
return nil, fmt.Errorf("读取错误:%s %s", resp.Status, err)
}
for _, lib := range info {
lib.client = cli
}
return info, nil
}
//获取默认资料库
func (cli *Client) GetDefaultLibrary() (*Library, error) {
return cli.GetLibrary("")
}
func (cli *Client) GetLibrary(name string) (*Library, error) {
var id string
var err error
//如果name为空字符串,则获取默认资料库
if name == "" {
id, err = cli.GetDefaultLibraryId()
if err != nil {
return nil, err
}
}
libraries, err := cli.ListAllLibraries()
if err != nil {
return nil, fmt.Errorf("获取资料库列表失败: %s", err)
}
if name == "" {
for _, library := range libraries {
if library.Id == id {
return library, nil
}
}
} else {
for _, library := range libraries {
if library.Name == name {
return library, nil
}
}
}
return nil, fmt.Errorf("未找到资料库")
}
//获取默认资料库ID
func (cli *Client) GetDefaultLibraryId() (string, error) {
resp, err := cli.doRequest("GET", "/default-repo/", nil, nil)
if err != nil {
return "", fmt.Errorf("获取默认资料库失败: %s", err)
}
defer resp.Body.Close()
var respInfo struct {
Exists bool
RepoId string `json:"repo_id"`
}
err = json.NewDecoder(resp.Body).Decode(&respInfo)
if err != nil {
return "", fmt.Errorf("获取默认资料库失败: %s", err)
}
if !respInfo.Exists {
return "", fmt.Errorf("默认资料库不存在")
}
return respInfo.RepoId, nil
}
func (lib *Library) doRequest(method, uri string, header http.Header, body io.Reader) (*http.Response, error) {
if !strings.HasPrefix(uri, "http://") && !strings.HasPrefix(uri, "https://") {
uri = "/repos/" + lib.Id + uri
}
return lib.client.doRequest(method, uri, header, body)
}
//获取资料库的上传地址
func (lib *Library) UploadLink() (string, error) {
resp, err := lib.doRequest("GET", "/upload-link/", nil, nil)
if err != nil {
return "", fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
var link string
err = json.NewDecoder(resp.Body).Decode(&link)
if err != nil {
return "", fmt.Errorf("解析错误:%s %s", resp.Status, err)
}
//返回值是"xxx"格式的,需要去掉头尾的引号
return link, nil
}
//资料库提交
type LibraryCommit struct {
Id string
Desc string
Ctime int
Creator string
Conflict bool
NewMerge bool `json:"new_merge"`
CreatorName string `json:"creator_name"`
RootId string `json:"root_id"`
RepoId string `json:"repo_id"`
ParentId string `json:"parent_id"`
SecondParentId string `json:"second_parent_id"`
RevFileSize int `json:"rev_file_size"`
RevFileId string `json:"rev_file_id"`
RevRenamedOldPath string `json:"rev_renamed_old_path"`
library *Library `json:"-"`
}
//获取资料库的提交历史
func (lib *Library) History() ([]*LibraryCommit, error) {
resp, err := lib.doRequest("GET", "/history", nil, nil)
if err != nil {
return nil, fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取错误: %s", err)
}
var respInfo struct {
PageNext bool `json:"page_next"`
Commits []*LibraryCommit
}
err = json.Unmarshal(b, &respInfo)
if err != nil {
return nil, fmt.Errorf("解析错误: %s %s", err, string(b))
}
for _, commit := range respInfo.Commits {
commit.library = lib
}
return respInfo.Commits, nil
}