This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
generated from beyondstorage/go-service-example
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
267 lines (218 loc) · 6.06 KB
/
utils.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
package azfile
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/Azure/azure-storage-file-go/azfile"
"github.com/beyondstorage/go-endpoint"
ps "github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/credential"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
)
// Storage is the azfile client.
type Storage struct {
client azfile.DirectoryURL
workDir string
defaultPairs DefaultStoragePairs
features StorageFeatures
types.UnimplementedStorager
types.UnimplementedDirer
}
// String implements Storager.String
func (s *Storage) String() string {
return fmt.Sprintf("Storager azfile {WorkDir: %s}", s.workDir)
}
// NewStorager will create Storager only.
func NewStorager(pairs ...types.Pair) (types.Storager, error) {
return newStorager(pairs...)
}
// newStorager will create a storage client.
func newStorager(pairs ...types.Pair) (store *Storage, err error) {
defer func() {
if err != nil {
err = services.InitError{Op: "new_storager", Type: Type, Err: formatError(err), Pairs: pairs}
}
}()
opt, err := parsePairStorageNew(pairs)
if err != nil {
return nil, err
}
store = &Storage{
workDir: "/",
}
if opt.HasWorkDir {
store.workDir = opt.WorkDir
}
ep, err := endpoint.Parse(opt.Endpoint)
if err != nil {
return nil, err
}
var uri string
switch ep.Protocol() {
case endpoint.ProtocolHTTP:
uri, _, _ = ep.HTTP()
case endpoint.ProtocolHTTPS:
uri, _, _ = ep.HTTPS()
default:
return nil, services.PairUnsupportedError{Pair: ps.WithEndpoint(opt.Endpoint)}
}
primaryURL, _ := url.Parse(uri)
cred, err := credential.Parse(opt.Credential)
if err != nil {
return nil, err
}
if cred.Protocol() != credential.ProtocolHmac {
return nil, services.PairUnsupportedError{Pair: ps.WithCredential(opt.Credential)}
}
credValue, err := azfile.NewSharedKeyCredential(cred.Hmac())
if err != nil {
return nil, err
}
p := azfile.NewPipeline(credValue, azfile.PipelineOptions{
Retry: azfile.RetryOptions{},
})
serviceURL := azfile.NewServiceURL(*primaryURL, p)
ctx := context.Background()
shareURL := serviceURL.NewShareURL(opt.Name)
workDir := strings.TrimPrefix(store.workDir, "/")
store.client = shareURL.NewDirectoryURL(workDir)
_, err = store.client.Create(ctx, azfile.Metadata{}, azfile.SMBProperties{})
if err != nil {
return nil, err
}
if opt.HasDefaultStoragePairs {
store.defaultPairs = opt.DefaultStoragePairs
}
if opt.HasStorageFeatures {
store.features = opt.StorageFeatures
}
return store, nil
}
func (s *Storage) formatError(op string, err error, path ...string) error {
if err == nil {
return nil
}
return services.StorageError{
Op: op,
Err: formatError(err),
Storager: s,
Path: path,
}
}
// formatError converts errors returned by SDK into errors defined in go-storage and go-service-*.
// The original error SHOULD NOT be wrapped.
func formatError(err error) error {
if _, ok := err.(services.InternalError); ok {
return err
}
e, ok := err.(azfile.StorageError)
if ok {
switch azfile.StorageErrorCodeType(e.ServiceCode()) {
case "":
switch e.Response().StatusCode {
case fileNotFound:
return fmt.Errorf("%w: %v", services.ErrObjectNotExist, err)
default:
return fmt.Errorf("%w: %v", services.ErrUnexpected, err)
}
case azfile.StorageErrorCodeResourceNotFound:
return fmt.Errorf("%w: %v", services.ErrObjectNotExist, err)
case azfile.StorageErrorCodeInsufficientAccountPermissions:
return fmt.Errorf("%w: %v", services.ErrPermissionDenied, err)
default:
return fmt.Errorf("%w: %v", services.ErrUnexpected, err)
}
}
return fmt.Errorf("%w: %v", services.ErrUnexpected, err)
}
// getAbsPath will calculate object storage's abs path
func (s *Storage) getAbsPath(path string) string {
if strings.HasPrefix(path, s.workDir) {
return strings.TrimPrefix(path, "/")
}
prefix := strings.TrimPrefix(s.workDir, "/")
return prefix + path
}
// getRelPath will get object storage's rel path.
func (s *Storage) getRelPath(path string) string {
prefix := strings.TrimPrefix(s.workDir, "/")
return strings.TrimPrefix(path, prefix)
}
// getRelativePath will get relative path(fileName or directoryName) based on workDir for DirectoryURL or FileURL.
func (s *Storage) getRelativePath(path string) string {
relativePath := path
if strings.HasPrefix(path, s.workDir) {
relativePath = strings.TrimPrefix(path, s.workDir)
return strings.TrimPrefix(relativePath, "/")
}
return relativePath
}
func (s *Storage) mkDirs(ctx context.Context, path string) (err error) {
if "." == path {
return
}
subDirs := strings.Split(path, "/")
i := len(subDirs)
existedDir := ""
for i > 0 {
existedDir = strings.Join(subDirs[0:i], "/")
_, err = s.client.NewDirectoryURL(existedDir).GetProperties(ctx)
if err == nil {
// dir already exists
break
} else if !checkError(err, fileNotFound) {
// Something error other then file not found, return directly.
return err
} else {
// dir does not exist
i--
existedDir = ""
}
}
currentDir := existedDir
for _, v := range subDirs[i:] {
if currentDir == "" {
currentDir = v
} else {
currentDir += "/" + v
}
_, err = s.client.NewDirectoryURL(currentDir).Create(ctx, azfile.Metadata{}, azfile.SMBProperties{})
if err != nil {
return err
}
}
return
}
func (s *Storage) newObject(done bool) *types.Object {
return types.NewObject(s, done)
}
func (s *Storage) formatFileObject(v azfile.FileItem) (o *types.Object, err error) {
o = s.newObject(true)
o.ID = v.Name
o.Path = s.getRelPath(v.Name)
o.Mode |= types.ModeRead
if v.Properties.ContentLength != 0 {
o.SetContentLength(v.Properties.ContentLength)
}
return
}
func (s *Storage) formatDirObject(v azfile.DirectoryItem) (o *types.Object, err error) {
o = s.newObject(true)
o.ID = v.Name
o.Path = s.getRelPath(v.Name)
o.Mode |= types.ModeDir
return
}
const (
// File not found error.
fileNotFound = 404
)
func checkError(err error, expect int) bool {
e, ok := err.(azfile.StorageError)
if !ok {
return false
}
return e.Response().StatusCode == expect
}