-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_dir.go
195 lines (162 loc) · 4.93 KB
/
api_dir.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
package seafile
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"time"
)
type Dir struct {
Id string `json:"obj_id"` //Dir的ID貌似全是0
Name string `json:"obj_name"`
RepoId string `json:"repo_id"`
ParentDir string `json:"parent_dir"`
Perm string
Mtime time.Time
Size int
FileCount int `json:"file_count"`
DirCount int `json:"dir_count"`
repo *Repo `json:"-"`
}
//文件完整路径
func (dir *Dir) Path() string {
return filepath.Join(dir.ParentDir, dir.Name)
}
//获取目录
func (repo *Repo) GetDir(path string) (*Dir, error) {
q := url.Values{"path": {path}}
resp, err := repo.client.apiGET(repo.Uri() + "/dir/detail/?" + q.Encode())
if err != nil {
return nil, fmt.Errorf("请求文件信息失败: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("文件信息错误: %s", resp.Status)
}
var detail struct {
Name string
Path string
Size int
Mtime time.Time
RepoId string `json:"repo_id"`
FileCount int `json:"file_count"`
DirCount int `json:"dir_count"`
}
err = json.NewDecoder(resp.Body).Decode(&detail)
if err != nil {
return nil, fmt.Errorf("解析文件夹信息失败: %s, %s", resp.Status, err)
}
fmt.Println(detail.Path)
dir := Dir{
//Id: detail.Id,//该接口暂时没有提供该信息
//Perm: detail.Perm,该接口暂时没有提供该信息
repo: repo,
Name: detail.Name,
Mtime: detail.Mtime,
RepoId: detail.RepoId,
ParentDir: filepath.Dir(path),
Size: detail.Size,
FileCount: detail.FileCount,
DirCount: detail.DirCount,
}
return &dir, nil
}
//创建文件夹
//如果文件夹已存在,则按照重命名规则创建新文件
func (repo *Repo) Mk(path string) (*Dir, error) {
q := url.Values{"p": {path}}
d := url.Values{"operation": {"mkdir"}}
resp, err := repo.client.apiPOSTForm(repo.Uri()+"/dir/?"+q.Encode(), d)
if err != nil {
return nil, fmt.Errorf("请求资料库信息失败: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("[%s]%s", resp.Status, string(b))
}
var dir Dir
err = json.NewDecoder(resp.Body).Decode(&dir)
if err != nil {
return nil, fmt.Errorf("解析资料库信息失败: %s, %s", resp.Status, err)
}
dir.repo = repo
return &dir, nil
}
//文件夹内容结构
//TODO:尝试与File、Dir合并
type DirEntry struct {
Id string
Name string
Type string //file或dir
Mtime int
Permission string
ParentDir string `json:"parent_dir"` //仅在递归获取子目录时有效
Size int //file entry only
ModifierName string `json:"modifier_name"` //file entry only
ModifierEmail string `json:"modifier_email"` //file entry only
ModifierContactEmail string `json:"modifier_contact_email"` //file entry only
IsLocked bool `json:"is_locked"` //file entry only
LockTime string `json:"lock_time"` //file entry only
LockOwner string `json:"lock_owner"` //file entry only
LockOwnerName string `json:"lock_owner_name"` //file entry only
LockedByMe bool `json:"locked_by_me"` //file entry only
}
//获取文件夹内容
func (dir *Dir) getEntriesWithOption(t string, recursive bool) ([]DirEntry, error) {
q := url.Values{
"t": {t},
"p": {dir.Path()},
"recursive": {"0"},
}
if recursive {
q.Set("recursive", "1")
}
resp, err := dir.repo.client.apiGET(dir.repo.Uri() + "/dir/?" + q.Encode())
if err != nil {
return nil, fmt.Errorf("请求资料库信息失败: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("[%s]%s", resp.Status, string(b))
}
var entries []DirEntry
err = json.NewDecoder(resp.Body).Decode(&entries)
if err != nil {
return nil, fmt.Errorf("解析资料库信息失败: %s, %s", resp.Status, err)
}
return entries, nil
}
//获取文件夹的所有内容
func (dir *Dir) GetEntries() ([]DirEntry, error) {
return dir.getEntriesWithOption("", false)
}
//获取文件夹下的文件
func (dir *Dir) GetSubFiles() ([]DirEntry, error) {
return dir.getEntriesWithOption("f", false)
}
//获取文件夹下的子文件夹
func (dir *Dir) GetSubDirs() ([]DirEntry, error) {
return dir.getEntriesWithOption("d", false)
}
//递归获取文件夹所有的子文件夹
func (dir *Dir) GetSubDirTree() ([]DirEntry, error) {
return dir.getEntriesWithOption("d", true)
}
//删除文件夹
func (dir *Dir) Delete() error {
q := url.Values{"p": {dir.Path()}}
resp, err := dir.repo.client.apiDELETE(dir.repo.Uri() + "/dir/?" + q.Encode())
if err != nil {
return fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
b, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("[%s] %s", resp.Status, string(b))
}