This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
prefix_wrapper.go
58 lines (47 loc) · 1.57 KB
/
prefix_wrapper.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
package storage
import (
"context"
"fmt"
"io"
"strings"
)
// NewPrefixWrapper creates a FS which wraps fs and prefixes all paths with prefix.
func NewPrefixWrapper(fs FS, prefix string) FS {
return &prefixWrapper{
fs: fs,
prefix: prefix,
}
}
type prefixWrapper struct {
fs FS
prefix string
}
func (p *prefixWrapper) addPrefix(path string) string {
return fmt.Sprintf("%v%v", p.prefix, path)
}
// Open implements FS.
func (p *prefixWrapper) Open(ctx context.Context, path string, options *ReaderOptions) (*File, error) {
return p.fs.Open(ctx, p.addPrefix(path), options)
}
// Attributes implements FS.
func (p *prefixWrapper) Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error) {
return p.fs.Attributes(ctx, p.addPrefix(path), options)
}
// Create implements FS.
func (p *prefixWrapper) Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error) {
return p.fs.Create(ctx, p.addPrefix(path), options)
}
// Delete implements FS.
func (p *prefixWrapper) Delete(ctx context.Context, path string) error {
return p.fs.Delete(ctx, p.addPrefix(path))
}
// Walk transverses all paths underneath path, calling fn on each visited path.
func (p *prefixWrapper) Walk(ctx context.Context, path string, fn WalkFn) error {
return p.fs.Walk(ctx, p.addPrefix(path), func(path string) error {
path = strings.TrimPrefix(path, p.prefix)
return fn(path)
})
}
func (p *prefixWrapper) URL(ctx context.Context, path string, options *SignedURLOptions) (string, error) {
return p.fs.URL(ctx, p.addPrefix(path), options)
}