-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
138 lines (108 loc) · 3.69 KB
/
files.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
package sirv
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
)
func (c *Client) SearchFiles(ctx context.Context, payload FileSearchPayload) (*FileSearchResponse, error) {
var searchResp FileSearchResponse
err := c.makeRequest(ctx, http.MethodPost, c.BaseURL+"/files/search", payload, &searchResp)
if err != nil {
return nil, err
}
return &searchResp, nil
}
func (c *Client) ScrollFilesSearch(ctx context.Context, payload FileSearchScrollPayload) (*FileSearchScrollResponse, error) {
var scrollResp FileSearchScrollResponse
err := c.makeRequest(ctx, http.MethodPost, c.BaseURL+"/files/search/scroll", payload, &scrollResp)
if err != nil {
return nil, err
}
return &scrollResp, nil
}
func (c *Client) ReadFolderContents(ctx context.Context, dirname string, continuation string) (*FolderContents, error) {
reqUrl := fmt.Sprintf("%s/files/readdir?dirname=%s", c.BaseURL, url.PathEscape(dirname))
if continuation != "" {
reqUrl = reqUrl + "&continuation=" + url.PathEscape(continuation)
}
var contents FolderContents
err := c.makeRequest(ctx, http.MethodGet, reqUrl, nil, &contents)
if err != nil {
return nil, err
}
return &contents, nil
}
func (c *Client) GetFileInfo(ctx context.Context, filename string) (*FileInfo, error) {
reqUrl := fmt.Sprintf("%s/files/stat?filename=%s", c.BaseURL, url.PathEscape(filename))
var fileInfo FileInfo
err := c.makeRequest(ctx, http.MethodGet, reqUrl, nil, &fileInfo)
if err != nil {
return nil, err
}
return &fileInfo, nil
}
func (c *Client) DownloadFile(ctx context.Context, filename Filename, destPath string) error {
reqUrl := fmt.Sprintf("%s/files/download?filename=%s", c.BaseURL, url.PathEscape(string(filename)))
resp, err := c.makeHTTPRequest(ctx, http.MethodGet, reqUrl, nil, "application/json")
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(destPath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func (c *Client) DeleteFileOrEmptyFolder(ctx context.Context, filename Filename) error {
reqUrl := fmt.Sprintf("%s/files/delete?filename=%s", c.BaseURL, url.PathEscape(string(filename)))
resp, err := c.makeHTTPRequest(ctx, http.MethodPost, reqUrl, nil, "application/json")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
func (c *Client) UploadFile(ctx context.Context, dstFilename, localPath string) error {
file, err := os.Open(localPath)
if err != nil {
return err
}
defer file.Close()
reqUrl := fmt.Sprintf("%s/files/upload?filename=%s", c.BaseURL, url.PathEscape(dstFilename))
_, err = c.makeHTTPRequest(ctx, http.MethodPost, reqUrl, file, "")
return err
}
func (c *Client) CreateEmptyFolder(ctx context.Context, dirname string) error {
reqUrl := fmt.Sprintf("%s/files/mkdir?dirname=%s", c.BaseURL, url.PathEscape(dirname))
resp, err := c.makeHTTPRequest(ctx, http.MethodPost, reqUrl, nil, "application/json")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code: %d. Response: %s", resp.StatusCode, string(bodyBytes))
}
return nil
}
func (c *Client) RenameFileOrFolder(ctx context.Context, from, to string) error {
reqUrl := fmt.Sprintf("%s/files/rename?from=%s&to=%s", c.BaseURL, url.PathEscape(from), url.PathEscape(to))
resp, err := c.makeHTTPRequest(ctx, http.MethodPost, reqUrl, nil, "application/json")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}