-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdirectory.go
152 lines (121 loc) · 3.88 KB
/
directory.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
package seafile
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
//账户信息
type DirectoryEntry struct {
Id string
Type string
Name string
Size int
Permission string
Mtime int
ParentDir string `json:"parent_dir"`
}
//列出资料库中指定位置目录的文件和子目录
func (lib *Library) ListDirectoryEntries(path string) ([]DirectoryEntry, error) {
return lib.ListDirectoryEntriesWithOption(path, nil)
}
//列出资料库中指定位置目录的文件
func (lib *Library) ListDirectoryFileEntries(path string) ([]DirectoryEntry, error) {
query := url.Values{"t": {"f"}}
return lib.ListDirectoryEntriesWithOption(path, query)
}
//列出资料库中指定位置目录的子目录
func (lib *Library) ListDirectoryDirectoryEntries(path string) ([]DirectoryEntry, error) {
query := url.Values{"t": {"d"}}
return lib.ListDirectoryEntriesWithOption(path, query)
}
//列出资料库中指定位置目录下的所有目录,并递归地获取其子目录下的目录
func (lib *Library) ListDirectoryEntriesRecursive(path string) ([]DirectoryEntry, error) {
query := url.Values{"t": {"d"}, "recursive": {"1"}}
return lib.ListDirectoryEntriesWithOption(path, query)
}
//列出资料库指定位置的目录内容
func (lib *Library) ListDirectoryEntriesWithOption(path string, query url.Values) ([]DirectoryEntry, error) {
if query == nil {
query = url.Values{}
}
if path == "" {
path = "/"
}
query.Set("p", path)
resp, err := lib.doRequest("GET", "/dir/?"+query.Encode(), nil, nil)
if err != nil {
return nil, fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
info := []DirectoryEntry{}
//err = json.NewDecoder(resp.Body).Decode(&info)
err = json.Unmarshal(b, &info)
if err != nil {
return nil, fmt.Errorf("读取错误:%s %s", resp.Status, err)
}
return info, nil
}
//在资料库创建目录
// NOTE: 如果指定目录以及存在,会自动创建重命名后的目录,而不会失败
func (lib *Library) CreateDirectory(path string) error {
query := url.Values{"p": {path}}
uri := "/dir/?" + query.Encode()
body := bytes.NewBufferString("operation=mkdir")
header := http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}
resp, err := lib.doRequest("POST", uri, header, body)
if err != nil {
return fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode == http.StatusCreated {
return nil
}
return fmt.Errorf("%s %s", resp.Status, string(b))
}
//删除目录
func (lib *Library) RemoveDirectory(dir string) error {
query := url.Values{"p": {dir}}
resp, err := lib.doRequest("DELETE", "/dir/?"+query.Encode(), nil, nil)
if err != nil {
return fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("读取错误:%s %s", resp.Status, err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("错误:%s %s", resp.Status, string(b))
}
return nil
}
//重命名目录
// NOTE: 如果新目录已经存在,会自动创建重命名后的目录,而不会失败
func (lib *Library) RenameDirectory(path, newname string) error {
q := url.Values{"p": {path}}
d := url.Values{
"operation": {"rename"},
"newname": {newname},
}
body := bytes.NewBufferString(d.Encode())
hdr := http.Header{"Content-Type": {"application/x-www-form-urlencoded"}}
resp, err := lib.doRequest("POST", "/dir/?"+q.Encode(), hdr, body)
if err != nil {
return fmt.Errorf("请求错误:%s", err)
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil || len(b) == 0 {
return fmt.Errorf("读取错误: %s", err)
}
//FIXME:文档上说返回HTTP 301为成功,实测却是HTTP 200。
if resp.StatusCode == http.StatusOK {
return nil
}
return fmt.Errorf("[%s] %s", resp.Status, string(b))
}