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
103 lines (90 loc) · 2.01 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
package storj
import (
"context"
"fmt"
"path/filepath"
"github.com/beyondstorage/go-credential"
ps "github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
"storj.io/uplink"
)
// Storage is the example client.
type Storage struct {
project *uplink.Project
defaultPairs DefaultStoragePairs
features StorageFeatures
name string
workDir string
types.UnimplementedStorager
}
// String implements Storager.String
func (s *Storage) String() string {
return fmt.Sprintf(
"Storager storj {Name: %s, WorkDir: %s}",
s.name, s.workDir,
)
}
// NewStorager will create Storager only.
func NewStorager(pairs ...types.Pair) (types.Storager, error) {
var accessGrant string
opt, err := parsePairStorageNew(pairs)
if err != nil {
return nil, err
}
st := &Storage{
name: opt.Name,
workDir: "/",
}
if opt.HasWorkDir {
st.workDir = opt.WorkDir
}
if opt.HasDefaultStoragePairs {
st.defaultPairs = opt.DefaultStoragePairs
}
if opt.HasStorageFeatures {
st.features = opt.StorageFeatures
}
cp, err := credential.Parse(opt.Credential)
if err != nil {
return nil, err
}
switch cp.Protocol() {
case credential.ProtocolAPIKey:
accessGrant = cp.APIKey()
default:
return nil, services.PairUnsupportedError{Pair: ps.WithCredential(opt.Credential)}
}
access, err := uplink.ParseAccess(accessGrant)
if err != nil {
return nil, err
}
st.project, err = uplink.OpenProject(context.Background(), access)
if err != nil {
return nil, err
}
return st, 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,
}
}
func formatError(err error) error {
if _, ok := err.(services.InternalError); ok {
return err
}
return err
}
func (s *Storage) getAbsPath(path string) string {
if filepath.IsAbs(path) {
return path
}
return s.workDir + path
}