-
Notifications
You must be signed in to change notification settings - Fork 0
/
optlinker.go
64 lines (56 loc) · 1.47 KB
/
optlinker.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
package vfs
import (
"context"
"net/url"
"path"
)
type TokenValidator interface {
Gen(ctx context.Context, key string, opts ...LinkOptions) (string, error)
Validate(ctx context.Context, key string, opts ...LinkOptions) (bool, error)
}
// OptLinker wrap FS as Linker
type OptLinker struct {
FS
tv TokenValidator
publicAccessUrl url.URL
internalAccessUrl url.URL
}
func NewOptLinker(fs FS, publicAccessUrl url.URL, internalAccessUrl url.URL, tv TokenValidator) *OptLinker {
return &OptLinker{
FS: fs,
tv: tv,
publicAccessUrl: publicAccessUrl,
internalAccessUrl: internalAccessUrl,
}
}
var _ Linker = (*OptLinker)(nil)
func (o *OptLinker) PreSignedURL(ctx context.Context, name string, args ...LinkOptions) (res *Link, err error) {
token := ""
if o.tv != nil {
token, err = o.tv.Gen(ctx, name, args...)
if err != nil {
return nil, err
}
}
url := o.publicAccessUrl
url.Path = path.Join(url.Path, name)
if len(token) > 0 {
url.Query().Add("token", token)
}
res = &Link{}
res.URL = url.String()
return
}
func (o *OptLinker) PublicUrl(ctx context.Context, name string) (res *Link, err error) {
url := o.publicAccessUrl
url.Path = path.Join(url.Path, name)
res = &Link{}
res.URL = url.String()
return
}
func (o *OptLinker) InternalUrl(ctx context.Context, name string, args ...LinkOptions) (res *Link, err error) {
url := o.internalAccessUrl
url.Path = path.Join(url.Path, name)
res.URL = url.String()
return
}