forked from lilith44/uoa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uoa_template.go
137 lines (112 loc) · 3.29 KB
/
uoa_template.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
package uoa
import (
`context`
`fmt`
`net/url`
`strings`
`github.com/tencentyun/cos-go-sdk-v5`
)
// 内部接口封装
// 使用模板方法设计模式
type uoaTemplate struct {
cos uoaInternal
}
func (t *uoaTemplate) Credentials(ctx context.Context, path Path, opts ...credentialsOption) (credentials *Credentials, err error) {
options := defaultCredentialOptions()
for _, opt := range opts {
opt.applyCredential(options)
}
key := t.key(path, options.environment, options.separator)
var keys []string
if 0 != len(options.patterns) {
keys = make([]string, 0, len(options.patterns))
for _, pattern := range options.patterns {
keys = append(keys, fmt.Sprintf("%s%s%s", key, options.separator, pattern))
}
} else {
keys = []string{key}
}
var base *credentialsBase
switch options.uoaType {
case TypeCos:
base, err = t.cos.credentials(ctx, options, keys...)
}
if nil != err {
return
}
// 注入通用字段
credentials = &Credentials{
credentialsBase: base,
Url: options.endpoint,
Separator: options.separator,
}
return
}
func (t *uoaTemplate) Url(ctx context.Context, path Path, filename string, opts ...urlOption) (url *url.URL, err error) {
options := defaultUrlOptions()
for _, opt := range opts {
opt.applyUrl(options)
}
key := t.key(path, options.environment, options.separator)
switch options.uoaType {
case TypeCos:
url, err = t.cos.url(ctx, key, filename, options)
}
return
}
func (t *uoaTemplate) InitiateMultipartUpload(ctx context.Context, path Path, opts ...multipartOption) (uploadId string, err error) {
options := defaultMultipartOptions()
for _, opt := range opts {
opt.applyMultipart(options)
}
fileKey := t.key(path, options.environment, options.separator)
switch options.uoaType {
case TypeCos:
uploadId, err = t.cos.initiateMultipartUpload(ctx, fileKey, options)
}
return
}
func (t *uoaTemplate) CompleteMultipartUpload(ctx context.Context, path Path, uploadId string, parts interface{}, opts ...multipartOption) (err error) {
options := defaultMultipartOptions()
for _, opt := range opts {
opt.applyMultipart(options)
}
fileKey := t.key(path, options.environment, options.separator)
switch options.uoaType {
case TypeCos:
err = t.cos.completeMultipartUpload(ctx, fileKey, uploadId, parts.([]cos.Object), options)
}
return
}
func (t *uoaTemplate) AbortMultipartUpload(ctx context.Context, path Path, uploadId string, opts ...multipartOption) (err error) {
options := defaultMultipartOptions()
for _, opt := range opts {
opt.applyMultipart(options)
}
fileKey := t.key(path, options.environment, options.separator)
switch options.uoaType {
case TypeCos:
err = t.cos.abortMultipartUpload(ctx, fileKey, uploadId, options)
}
return
}
func (t *uoaTemplate) Delete(ctx context.Context, path Path, opts ...deleteOption) (err error) {
options := defaultDeleteOptions()
for _, opt := range opts {
opt.applyDelete(options)
}
key := t.key(path, options.environment, options.separator)
switch options.uoaType {
case TypeCos:
err = t.cos.delete(ctx, key, options)
}
return
}
func (t *uoaTemplate) key(path Path, environment string, separator string) (key string) {
paths := path.Paths()
if "" != environment {
paths = append([]string{environment}, paths...)
}
key = strings.Join(path.Paths(), separator)
return
}