-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfiler.go
More file actions
78 lines (66 loc) · 2.21 KB
/
filer.go
File metadata and controls
78 lines (66 loc) · 2.21 KB
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
// Package config provides methods for managing configuration of apps.
package volumes
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
drycc "github.com/drycc/controller-sdk-go"
"github.com/drycc/controller-sdk-go/api"
)
// ListDir to an app's volume.
func ListDir(c *drycc.Client, appID, volumeID, path string, results int) (api.FilerDirEntries, int, error) {
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/client/?path=%s", appID, volumeID, url.QueryEscape(path))
body, count, reqErr := c.LimitedRequest(u, results)
if reqErr != nil && !drycc.IsErrAPIMismatch(reqErr) {
return []api.FilerDirEntry{}, -1, reqErr
}
var filerDirEntries []api.FilerDirEntry
if err := json.Unmarshal([]byte(body), &filerDirEntries); err != nil {
return []api.FilerDirEntry{}, -1, err
}
return filerDirEntries, count, reqErr
}
// Getfile to an app's volume.
func GetFile(c *drycc.Client, appID, volumeID, path string) (*http.Response, error) {
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/client/%s", appID, volumeID, path)
req, err := c.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// Put file to an app's volume.
func PostFile(c *drycc.Client, appID, volumeID, volumePath, name string, size int64, reader io.Reader) (*http.Response, error) {
buffer := new(bytes.Buffer)
writer := multipart.NewWriter(buffer)
if err := writer.WriteField("path", volumePath); err != nil {
return nil, err
}
if _, err := writer.CreateFormFile("file", name); err != nil {
return nil, err
}
size += int64(buffer.Len())
head := strings.NewReader(buffer.String())
buffer.Reset()
writer.Close()
bottom := strings.NewReader(buffer.String())
size += int64(buffer.Len())
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/client/", appID, volumeID)
r, err := c.NewRequest("POST", u, io.MultiReader(head, reader, bottom))
if err != nil {
return nil, err
}
r.ContentLength = size
r.Header.Add("Content-Type", writer.FormDataContentType())
return c.Do(r)
}
// Get file to an app's volume.
func DeleteFile(c *drycc.Client, appID, volumeID, path string) (*http.Response, error) {
u := fmt.Sprintf("/v2/apps/%s/volumes/%s/client/%s", appID, volumeID, path)
return c.Request("DELETE", u, nil)
}