-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
293 lines (260 loc) · 7.73 KB
/
service.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
289
290
291
292
293
package afs
import (
"context"
"fmt"
"github.com/viant/afs/file"
"github.com/viant/afs/mem"
"github.com/viant/afs/option"
"github.com/viant/afs/storage"
"github.com/viant/afs/url"
"io"
"io/ioutil"
"os"
"path"
"sync"
"time"
)
//Service represents storage storage
type Service interface {
storage.Lister
storage.Opener
storage.Uploader
storage.BatchUploader
storage.Deleter
storage.Creator
storage.Walker
storage.Getter
//Exists returns true if resource exists
Exists(ctx context.Context, URL string, options ...storage.Option) (bool, error)
//Download download bytes
Download(ctx context.Context, object storage.Object, options ...storage.Option) ([]byte, error)
//DownloadWithURL download bytes for URL
DownloadWithURL(ctx context.Context, URL string, options ...storage.Option) ([]byte, error)
storage.Copier
storage.Mover
//Initialises manager for baseURL with storage options (i.e. auth)
Init(ctx context.Context, baseURL string, options ...storage.Option) error
//NewWriter creates an upload writer
NewWriter(ctx context.Context, URL string, mode os.FileMode, options ...storage.Option) (io.WriteCloser, error)
//Closes all active managers
CloseAll() error
//Closes matched active manager
Close(baseURL string) error
//ErrorCode returns an error code or zero
ErrorCode(scheme string, err error) int
}
//Service implementation
type service struct {
faker bool
mutex *sync.RWMutex
managers map[string]storage.Manager
}
func (s *service) Upload(ctx context.Context, URL string, mode os.FileMode, reader io.Reader, options ...storage.Option) error {
URL = url.Normalize(URL, file.Scheme)
manager, err := s.manager(ctx, URL, options)
if err != nil {
return err
}
return manager.Upload(ctx, URL, mode, reader, options...)
}
func (s *service) Delete(ctx context.Context, URL string, options ...storage.Option) error {
URL = url.Normalize(URL, file.Scheme)
manager, err := s.manager(ctx, URL, options)
if err != nil {
return err
}
return manager.Delete(ctx, URL, options...)
}
func (s *service) Create(ctx context.Context, URL string, mode os.FileMode, isDir bool, options ...storage.Option) error {
URL = url.Normalize(URL, file.Scheme)
manager, err := s.manager(ctx, URL, options)
if err != nil {
return err
}
return manager.Create(ctx, URL, mode, isDir, options...)
}
func (s *service) Object(ctx context.Context, URL string, options ...storage.Option) (storage.Object, error) {
URL = url.Normalize(URL, file.Scheme)
manager, err := s.manager(ctx, URL, options)
if err != nil {
return nil, err
}
if getter, ok := manager.(storage.Getter); ok {
return getter.Object(ctx, URL, options...)
}
return s.object(ctx, manager, URL, options...)
}
func (s *service) object(ctx context.Context, manager storage.Manager, URL string, options ...storage.Option) (storage.Object, error) {
options = append(options, option.NewPage(0, 1))
objects, err := manager.List(ctx, URL, options...)
if err != nil {
return nil, err
}
if len(objects) == 0 {
return nil, fmt.Errorf("%v: not found", URL)
}
return objects[0], nil
}
func (s *service) Exists(ctx context.Context, URL string, options ...storage.Option) (bool, error) {
URL = url.Normalize(URL, file.Scheme)
manager, err := s.manager(ctx, URL, options)
if err != nil {
return false, err
}
return s.exists(ctx, manager, URL, options...)
}
func (s *service) exists(ctx context.Context, manager storage.Manager, URL string, options ...storage.Option) (bool, error) {
if checker, ok := manager.(storage.Checker); ok {
return checker.Exists(ctx, URL, options...)
}
options = append(options, option.NewPage(0, 1))
objects, err := s.List(ctx, URL, options...)
if err != nil {
return false, nil
}
return len(objects) > 0, nil
}
func (s *service) Open(ctx context.Context, object storage.Object, options ...storage.Option) (io.ReadCloser, error) {
return s.OpenURL(ctx, object.URL(), options...)
}
func (s *service) OpenURL(ctx context.Context, URL string, options ...storage.Option) (reader io.ReadCloser, err error) {
URL = url.Normalize(URL, file.Scheme)
var modifier option.Modifier
option.Assign(options, &modifier)
manager, err := s.manager(ctx, URL, options)
if err != nil {
return nil, err
}
reader, err = manager.OpenURL(ctx, URL, options...)
if modifier == nil || err != nil {
return reader, err
}
_, URLPath := url.Base(URL, file.Scheme)
_, name := path.Split(URLPath)
info := file.NewInfo(name, 0, file.DefaultFileOsMode, time.Now(), false)
_, reader, err = modifier(info, reader)
return reader, err
}
func (s *service) Download(ctx context.Context, object storage.Object, options ...storage.Option) ([]byte, error) {
reader, err := s.Open(ctx, object, options...)
if err != nil {
return nil, err
}
defer reader.Close()
return ioutil.ReadAll(reader)
}
func (s *service) DownloadWithURL(ctx context.Context, URL string, options ...storage.Option) ([]byte, error) {
reader, err := s.OpenURL(ctx, URL, options...)
if err != nil {
return nil, err
}
defer reader.Close()
return ioutil.ReadAll(reader)
}
func (s *service) newManager(ctx context.Context, scheme string, options ...storage.Option) (storage.Manager, error) {
if s.faker {
scheme = mem.Scheme
}
provider, err := GetRegistry().Get(scheme)
if err != nil {
return nil, err
}
return provider(options...)
}
//Init initialises service
func (s *service) Init(ctx context.Context, baseURL string, options ...storage.Option) error {
baseURL = url.Normalize(baseURL, file.Scheme)
_, err := s.manager(ctx, baseURL, options)
return err
}
//Close closes storage manager for supplied baseURL
func (s *service) Close(baseURL string) error {
baseURL, _ = url.Base(baseURL, file.Scheme)
s.mutex.Lock()
defer s.mutex.Unlock()
manager, ok := s.managers[baseURL]
if !ok {
return nil
}
return manager.Close()
}
//CloseAll closes all active managers
func (s *service) CloseAll() error {
s.mutex.Lock()
defer s.mutex.Unlock()
var err error
for _, manager := range s.managers {
if e := manager.Close(); e != nil {
err = e
}
}
return err
}
func (s *service) IsAuthChanged(ctx context.Context, manager storage.Manager, URL string, options []storage.Option) bool {
authTracker, ok := manager.(storage.AuthTracker)
if !ok {
return false
}
return authTracker.IsAuthChanged(ctx, URL, options)
}
func (s *service) manager(ctx context.Context, URL string, options []storage.Option) (storage.Manager, error) {
scheme := url.Scheme(URL, file.Scheme)
noCache := &option.NoCache{}
options, _ = option.Assign(options, &noCache)
if noCache.Source == option.NoCacheBaseURL {
return s.newManager(ctx, scheme, options...)
}
key, _ := url.Base(URL, scheme)
extURL := url.SchemeExtensionURL(URL)
key += extURL
if extURL != "" {
if extScheme := url.Scheme(extURL, file.Scheme); extScheme != scheme {
extManager, err := s.manager(ctx, extURL, options)
if err != nil {
return nil, err
}
options = append(options, extManager)
}
}
s.mutex.RLock()
result, ok := s.managers[key]
s.mutex.RUnlock()
if ok {
if !s.IsAuthChanged(ctx, result, URL, options) {
return result, nil
}
_ = result.Close()
}
manager, err := s.newManager(ctx, scheme, options...)
s.mutex.Lock()
defer s.mutex.Unlock()
if err == nil {
s.managers[key] = manager
}
return manager, err
}
//ErrorCode return error code
func (s *service) ErrorCode(scheme string, err error) int {
if err == nil {
return 0
}
manager, e := s.newManager(context.Background(), scheme)
if e != nil {
return 0
}
if coder, ok := manager.(storage.ErrorCoder); ok {
return coder.ErrorCode(err)
}
return 0
}
func newService(faker bool) *service {
return &service{
faker: faker,
mutex: &sync.RWMutex{},
managers: make(map[string]storage.Manager),
}
}
//New returns a abstract storage service
func New() Service {
return newService(false)
}