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
142 lines (118 loc) · 2.98 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
package ipfs
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
ipfs "github.com/ipfs/go-ipfs-api"
cmds "github.com/ipfs/go-ipfs-cmds"
"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 {
ipfs *ipfs.Shell
defaultPairs DefaultStoragePairs
features StorageFeatures
workDir string
gateway string
types.UnimplementedStorager
types.UnimplementedCopier
types.UnimplementedMover
types.UnimplementedDirer
types.UnimplementedStorageHTTPSigner
}
// String implements Storager.String
func (s *Storage) String() string {
return fmt.Sprintf("Storager IPFS {WorkDir: %s}", s.workDir)
}
// NewStorager will create Storager only.
func NewStorager(pairs ...types.Pair) (types.Storager, error) {
opt, err := parsePairStorageNew(pairs)
if err != nil {
return nil, err
}
st := &Storage{
workDir: "/",
}
if opt.HasWorkDir {
if !strings.HasSuffix(opt.WorkDir, "/") {
opt.WorkDir += "/"
}
st.workDir = opt.WorkDir
}
ep, err := endpoint.Parse(opt.Endpoint)
if err != nil {
return nil, err
}
var e string
switch ep.Protocol() {
case endpoint.ProtocolHTTP:
e, _, _ = ep.HTTP()
case endpoint.ProtocolHTTPS:
e, _, _ = ep.HTTPS()
default:
return nil, services.PairUnsupportedError{Pair: ps.WithEndpoint(opt.Endpoint)}
}
gate, err := endpoint.Parse(opt.Gateway)
if err != nil {
return nil, err
}
switch gate.Protocol() {
case endpoint.ProtocolHTTP:
st.gateway, _, _ = gate.HTTP()
case endpoint.ProtocolHTTPS:
st.gateway, _, _ = gate.HTTPS()
default:
return nil, services.PairUnsupportedError{Pair: WithGateway(opt.Gateway)}
}
sh := ipfs.NewShell(e)
if !sh.IsUp() {
return nil, errors.New("ipfs not online")
}
st.ipfs = sh
return st, nil
}
func formatError(err error) error {
if _, ok := err.(services.InternalError); ok {
return err
}
e, ok := err.(*ipfs.Error)
if !ok {
return fmt.Errorf("%w: %v", services.ErrUnexpected, err)
}
switch e.Message {
case os.ErrNotExist.Error():
return fmt.Errorf("%w: %v", services.ErrObjectNotExist, err)
}
// ref: https://github.com/ipfs/go-ipfs-cmds/blob/4ade007405e5d3befb14184290576c63cc43a6a3/error.go#L31
switch e.Code {
case int(cmds.ErrRateLimited):
return fmt.Errorf("%w: %v", services.ErrRequestThrottled, err)
case int(cmds.ErrImplementation):
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,
}
}
// getAbsPath will calculate object storage's abs path
func (s *Storage) getAbsPath(path string) string {
path = strings.ReplaceAll(path, "\\", "/")
if filepath.IsAbs(path) {
return path
}
return s.workDir + path
}