-
Notifications
You must be signed in to change notification settings - Fork 13
/
cloud.go
288 lines (233 loc) · 7.2 KB
/
cloud.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
package cloud
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"strings"
)
// A client represents a client connection to a {own|next}cloud
type Client struct {
Url *url.URL
Username string
Password string
}
// Error type encapsulates the returned error messages from the
// server.
type Error struct {
// Exception contains the type of the exception returned by
// the server.
Exception string `xml:"exception"`
// Message contains the error message string from the server.
Message string `xml:"message"`
}
func (e *Error) Error() string {
return fmt.Sprintf("Exception: %s, Message: %s", e.Exception, e.Message)
}
type ShareElement struct {
Id uint `xml:"id"`
Url string `xml:"url"`
}
type ShareResult struct {
XMLName xml.Name `xml:"ocs"`
Status string `xml:"meta>status"`
StatusCode uint `xml:"meta>statuscode"`
Message string `xml:"meta>message"`
Id uint `xml:"data>id"`
Url string `xml:"data>url"`
Elements []ShareElement `xml:"data>element"`
}
// Dial connects to an {own|next}Cloud instance at the specified
// address using the given credentials.
func Dial(host, username, password string) (*Client, error) {
url, err := url.Parse(host)
if err != nil {
return nil, err
}
return &Client{
Url: url,
Username: username,
Password: password,
}, nil
}
// Mkdir creates a new directory on the cloud with the specified name.
func (c *Client) Mkdir(path string) error {
_, err := c.sendWebDavRequest("MKCOL", path, nil)
return err
}
// Delete removes the specified folder from the cloud.
func (c *Client) Delete(path string) error {
_, err := c.sendWebDavRequest("DELETE", path, nil)
return err
}
// Upload uploads the specified source to the specified destination
// path on the cloud.
func (c *Client) Upload(src []byte, dest string) error {
_, err := c.sendWebDavRequest("PUT", dest, src)
return err
}
// UploadDir uploads an entire directory on the cloud. It returns the
// path of uploaded files or error. It uses glob pattern in src.
func (c *Client) UploadDir(src string, dest string) ([]string, error) {
files, err := filepath.Glob(src)
if err != nil {
return nil, err
}
for _, file := range files {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
err = c.Upload(data, filepath.Join(dest, filepath.Base(file)))
if err != nil {
return nil, err
}
}
return files, nil
}
// Download downloads a file from the specified path.
func (c *Client) Download(path string) ([]byte, error) {
return c.sendWebDavRequest("GET", path, nil)
}
func (c *Client) Exists(path string) bool {
_, err := c.sendWebDavRequest("PROPFIND", path, nil)
return err == nil
}
func (c *Client) CreateGroupFolder(mountPoint string) (*ShareResult, error) {
return c.sendAppsRequest("POST", "groupfolders/folders", fmt.Sprintf("mountpoint=%s", mountPoint))
}
func (c *Client) AddGroupToGroupFolder(group string, folderId uint) (*ShareResult, error) {
return c.sendAppsRequest("POST", fmt.Sprintf("groupfolders/folders/%d/groups", folderId), fmt.Sprintf("group=%s", group))
}
func (c *Client) SetGroupPermissionsForGroupFolder(permissions int, group string, folderId uint) (*ShareResult, error) {
return c.sendAppsRequest("POST", fmt.Sprintf("apps/groupfolders/folders/%d/groups/%s", folderId, group), fmt.Sprintf("permissions=%d", permissions))
}
func (c *Client) CreateShare(path string, shareType int, publicUpload string, permissions int) (*ShareResult, error) {
return c.sendOCSRequest("POST", "shares", fmt.Sprintf("path=%s&shareType=%d&publicUpload=%s&permissions=%d", path, shareType, publicUpload, permissions))
}
func (c *Client) GetShare(path string) (*ShareResult, error) {
return c.sendOCSRequest("GET", fmt.Sprintf("shares?path=%s", path), "")
}
func (c *Client) DeleteShare(id uint) (*ShareResult, error) {
return c.sendOCSRequest("DELETE", fmt.Sprintf("shares/%d", id), "")
}
func (c *Client) CreateFileDropShare(path string) (*ShareResult, error) {
result, err := c.CreateShare(path, 3, "true", 4)
if err != nil {
return nil, err
}
id := result.Id
return c.sendOCSRequest("PUT", fmt.Sprintf("shares/%d", id), "permissions=4")
}
func (c *Client) CreateReadOnlyShare(path string) (*ShareResult, error) {
result, err := c.CreateShare(path, 3, "true", 4)
if err != nil {
return nil, err
}
id := result.Id
return c.sendOCSRequest("PUT", fmt.Sprintf("shares/%d", id), "permissions=1")
}
func (c *Client) sendWebDavRequest(request string, path string, data []byte) ([]byte, error) {
// Create the https request
webdavPath := filepath.Join("remote.php/webdav", path)
folderUrl, err := url.Parse(webdavPath)
if err != nil {
return nil, err
}
client := &http.Client{}
req, err := http.NewRequest(request, c.Url.ResolveReference(folderUrl).String(), bytes.NewReader(data))
if err != nil {
return nil, err
}
req.SetBasicAuth(c.Username, c.Password)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if len(body) > 0 {
if body[0] == '<' {
error := Error{}
err = xml.Unmarshal(body, &error)
if err != nil {
return body, err
}
if error.Exception != "" {
return nil, err
}
}
}
return body, nil
}
func (c *Client) sendAppsRequest(request string, path string, data string) (*ShareResult, error) {
// Create the https request
appsPath := filepath.Join("apps", path)
folderUrl, err := url.Parse(appsPath)
if err != nil {
return nil, err
}
client := &http.Client{}
req, err := http.NewRequest(request, c.Url.ResolveReference(folderUrl).String(), strings.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Add("OCS-APIRequest", "true")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.Username, c.Password)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := ShareResult{}
err = xml.Unmarshal(body, &result)
if err != nil {
return nil, err
}
if result.StatusCode != 100 {
return nil, fmt.Errorf("Share API returned an unsuccessful status code %d", result.StatusCode)
}
return &result, nil
}
func (c *Client) sendOCSRequest(request string, path string, data string) (*ShareResult, error) {
// Create the https request
appsPath := filepath.Join("ocs/v2.php/apps/files_sharing/api/v1", path)
folderUrl, err := url.Parse(appsPath)
if err != nil {
return nil, err
}
client := &http.Client{}
req, err := http.NewRequest(request, c.Url.ResolveReference(folderUrl).String(), strings.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Add("OCS-APIRequest", "true")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(c.Username, c.Password)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := ShareResult{}
err = xml.Unmarshal(body, &result)
if err != nil {
return nil, err
}
if result.StatusCode != 200 {
return nil, fmt.Errorf("Share API returned an unsuccessful status code %d", result.StatusCode)
}
return &result, nil
}