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 0
/
utils.go
225 lines (195 loc) · 5.02 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
package ftp
import (
"fmt"
"net/textproto"
"path/filepath"
"strings"
"time"
"github.com/jlaffaye/ftp"
mime "github.com/qingstor/go-mime"
credential "github.com/beyondstorage/go-credential"
endpoint "github.com/beyondstorage/go-endpoint"
ps "github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
)
// Storage is the example client.
type Storage struct {
connection *ftp.ServerConn
user string
password string
url string
workDir string
defaultPairs DefaultStoragePairs
features StorageFeatures
types.UnimplementedStorager
}
// String implements Storager.String
func (s *Storage) String() string {
return fmt.Sprintf("Storager ftp {URL: %s, User: %s, WorkDir: %s}", s.url, s.user, s.workDir)
}
// NewStorager will create Storager only.
func NewStorager(pairs ...types.Pair) (types.Storager, error) {
return newStorager(pairs...)
}
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}
}
}()
store = &Storage{
connection: nil,
user: "anonymous",
password: "anonymous",
url: "localhost:21",
workDir: "/",
}
opt, err := parsePairStorageNew(pairs)
if err != nil {
return
}
if opt.HasEndpoint {
ep, err := endpoint.Parse(opt.Endpoint)
if err != nil {
return nil, err
}
var host string
var port int
switch ep.Protocol() {
case endpoint.ProtocolTCP:
_, host, port = ep.TCP()
default:
return nil, services.PairUnsupportedError{Pair: ps.WithEndpoint(opt.Endpoint)}
}
url := fmt.Sprintf("%s:%d", host, port)
store.url = url
}
if opt.HasWorkDir {
store.workDir = filepath.ToSlash(opt.WorkDir)
}
if opt.HasCredential {
cp, err := credential.Parse(opt.Credential)
if err != nil {
return nil, err
}
switch cp.Protocol() {
case credential.ProtocolBasic:
user, pass := cp.Basic()
store.password = pass
store.user = user
default:
return nil, services.PairUnsupportedError{Pair: ps.WithCredential(opt.Credential)}
}
}
err = store.connect()
if err != nil {
return nil, err
}
return
}
func (s *Storage) connect() error {
c, err := ftp.Dial(s.url, ftp.DialWithTimeout(5*time.Second))
if err != nil {
return err
}
err = c.Login(s.user, s.password)
if err != nil {
return err
}
err = c.ChangeDir(s.workDir)
if err != nil {
return err
}
s.connection = c
return nil
}
func (s *Storage) makeDir(path string) error {
if path == s.workDir || path == "." {
return nil
}
rp := s.getAbsPath(path)
return s.connection.MakeDir(rp)
}
// getAbsPath will calculate object storage's abs path(include workDir).
func (s *Storage) getAbsPath(path string) string {
if filepath.IsAbs(path) {
return path
}
absPath := filepath.Join(s.workDir, path)
// Join will clean the trailng "/", we need to append it back.
if strings.HasSuffix(path, string(filepath.Separator)) {
absPath += string(filepath.Separator)
}
return absPath
}
// getRelPath will get object storage's rel path(exclude workDir).
func (s *Storage) getRelPath(path string) string {
path = strings.TrimPrefix(path, string(filepath.Separator))
return strings.TrimPrefix(path, s.workDir)
}
func (s *Storage) getNameList(path string) (namelist []string, err error) {
namelist, err = s.connection.NameList(s.getAbsPath(path))
if err != nil {
return nil, err
}
return
}
func (s *Storage) newObject(done bool) *types.Object {
return types.NewObject(s, done)
}
func (s *Storage) mapMode(fet ftp.EntryType) types.ObjectMode {
switch fet {
case ftp.EntryTypeFile:
return types.ModeRead
case ftp.EntryTypeFolder:
return types.ModeDir
case ftp.EntryTypeLink:
return types.ModeLink
}
return types.ModeRead
}
func (s *Storage) formatFileObject(fe *ftp.Entry, parent string) (obj *types.Object, err error) {
path := filepath.Join(parent, fe.Name)
obj = types.NewObject(s, false)
obj.SetID(path)
obj.SetMode(s.mapMode(fe.Type))
obj.SetPath(s.getRelPath(path))
if fe.Type == ftp.EntryTypeFile {
obj.SetContentLength(int64(fe.Size))
obj.SetContentType(mime.DetectFilePath(path))
}
obj.SetLastModified(fe.Time)
return
}
func formatError(err error) error {
if _, ok := err.(services.InternalError); ok {
return err
}
switch errX := err.(type) {
case *textproto.Error:
switch errX.Code {
case ftp.StatusInvalidCredentials,
ftp.StatusLoginNeedAccount,
ftp.StatusStorNeedAccount:
return fmt.Errorf("%w, %v", services.ErrPermissionDenied, err)
case ftp.StatusFileUnavailable,
ftp.StatusFileActionIgnored:
return fmt.Errorf("%w, %v", services.ErrObjectNotExist, err)
default:
return fmt.Errorf("%w, %v", services.ErrServiceInternal, err)
}
}
return fmt.Errorf("%w, %v", services.ErrUnexpected, err)
}
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,
}
}