forked from linxGnu/goseaweedfs
-
Notifications
You must be signed in to change notification settings - Fork 12
/
filer.go
236 lines (206 loc) · 5.72 KB
/
filer.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
package goseaweedfs
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// Filer client
type Filer struct {
base *url.URL
client *httpClient
authKey string
}
// FilerUploadResult upload result of response from filer server. According to https://github.com/chrislusf/seaweedfs/wiki/Filer-Server-API.
type FilerUploadResult struct {
Name string `json:"name,omitempty"`
FileURL string `json:"url,omitempty"`
FileID string `json:"fid,omitempty"`
Size int64 `json:"size,omitempty"`
Error string `json:"error,omitempty"`
}
// NewFiler new filer with filer server's url
func NewFiler(u string, client *http.Client, opts ...FilerOption) (f *Filer, err error) {
return newFiler(u, client, opts...)
}
func newFiler(u string, client *http.Client, opts ...FilerOption) (f *Filer, err error) {
// base url
base, err := parseURI(u)
if err != nil {
return
}
// filer
f = &Filer{
base: base,
}
// apply options
for _, opt := range opts {
opt(f)
}
// client
var clientOpts []HttpClientOption
if f.authKey != "" {
clientOpts = append(clientOpts, WithHttpClientAuthKey(f.authKey))
}
f.client = newHTTPClient(client, clientOpts...)
return f, nil
}
// Close underlying daemons.
func (f *Filer) Close() (err error) {
if f.client != nil {
err = f.client.Close()
}
return
}
// UploadFile a file.
func (f *Filer) UploadFile(localFilePath, newPath, collection, ttl string) (result *FilerUploadResult, err error) {
// file part
fp, err := NewFilePart(localFilePath)
if err != nil {
return result, err
}
defer fp.Close()
// file reader
var fileReader io.Reader
if fp.FileSize == 0 {
// empty file
fileReader = bytes.NewBuffer(EmptyMark.Bytes())
} else {
// non-empty file
fileReader = fp.Reader
}
// upload
var data []byte
data, status, err := f.client.upload(encodeURI(*f.base, newPath, normalize(nil, collection, ttl)), localFilePath, fileReader, fp.MimeType)
if err != nil {
if !strings.Contains(err.Error(), "The process cannot access the file because another process has locked a portion of the file.") {
return result, err
}
}
// upload result
var res FilerUploadResult
if err = json.Unmarshal(data, &res); err != nil {
if status == 404 {
return nil, errors.New("404 not found")
}
return result, err
}
result = &res
// error
if status >= 400 {
return result, errors.New(res.Error)
}
return result, nil
}
// UploadDir upload files from a directory.
func (f *Filer) UploadDir(localDirPath, newPath, collection, ttl string) (results []*FilerUploadResult, err error) {
// normalize path
localDirPath = normalizePath(localDirPath)
if strings.HasSuffix(localDirPath, "/") {
localDirPath = localDirPath[:(len(localDirPath) - 1)]
}
if !strings.HasPrefix(newPath, "/") {
newPath = "/" + newPath
}
files, err := ListFilesRecursive(localDirPath)
if err != nil {
return results, err
}
for _, info := range files {
filePath := normalizePath(info.Path)
newFilePath := newPath + strings.Replace(filePath, localDirPath, "", -1)
result, err := f.UploadFile(filePath, newFilePath, collection, ttl)
if err != nil {
return results, err
}
results = append(results, result)
}
return
}
// Upload content.
func (f *Filer) Upload(content io.Reader, fileSize int64, newPath, collection, ttl string) (result *FilerUploadResult, err error) {
fp := NewFilePartFromReader(ioutil.NopCloser(content), newPath, fileSize)
var data []byte
data, _, err = f.client.upload(encodeURI(*f.base, newPath, normalize(nil, collection, ttl)), newPath, ioutil.NopCloser(content), "")
if err == nil {
result = &FilerUploadResult{}
err = json.Unmarshal(data, result)
}
_ = fp.Close()
return
}
// ListDir List a directory.
func (f *Filer) ListDir(path string) (files []FilerFileInfo, err error) {
data, _, err := f.GetJson(path, nil)
if err != nil {
return files, err
}
if len(data) == 0 {
return
}
var res FilerListDirResponse
if err := json.Unmarshal(data, &res); err != nil {
return files, err
}
for _, file := range res.Entries {
file = GetFileWithExtendedFields(file)
files = append(files, file)
}
return
}
// ListDirRecursive List a directory recursively.
func (f *Filer) ListDirRecursive(path string) (files []FilerFileInfo, err error) {
entries, err := f.ListDir(path)
if err != nil {
return files, err
}
for _, file := range entries {
file = GetFileWithExtendedFields(file)
if file.IsDir {
file.Children, err = f.ListDirRecursive(file.FullPath)
if err != nil {
return files, err
}
}
files = append(files, file)
}
return
}
// Get response data from filer.
func (f *Filer) Get(path string, args url.Values, header map[string]string) (data []byte, statusCode int, err error) {
data, statusCode, err = f.client.get(encodeURI(*f.base, path, args), header)
return
}
// GetJson Get response data from filer.
func (f *Filer) GetJson(path string, args url.Values) (data []byte, statusCode int, err error) {
header := map[string]string{
"Accept": "application/json",
}
data, statusCode, err = f.client.get(encodeURI(*f.base, path, args), header)
return
}
// Download a file.
func (f *Filer) Download(path string, args url.Values, callback func(io.Reader) error) (err error) {
_, err = f.client.download(encodeURI(*f.base, path, args), callback)
return
}
// Delete a file/dir.
func (f *Filer) Delete(path string, args url.Values) (err error) {
_, err = f.client.delete(encodeURI(*f.base, path, args))
return
}
// DeleteDir a dir.
func (f *Filer) DeleteDir(path string) (err error) {
args := map[string][]string{"recursive": {"true"}}
_, err = f.client.delete(encodeURI(*f.base, path, args))
return
}
// DeleteFile a file.
func (f *Filer) DeleteFile(path string) (err error) {
_, err = f.client.delete(encodeURI(*f.base, path, nil))
return
}