-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.go
91 lines (78 loc) · 2 KB
/
registry.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
package fs
import (
"context"
"fmt"
"io"
"net/url"
"sync"
)
var registry = struct {
sync.RWMutex
m map[string]func() FileSystem
}{
m: map[string]func() FileSystem{},
}
// Register registers a filesystem provider for the given scheme.
func Register(scheme string, constructor func() FileSystem) {
registry.Lock()
registry.m[scheme] = constructor
registry.Unlock()
}
// Copy copies a file from srcURL to dstURL.
func Copy(ctx context.Context, dstURL, srcURL *url.URL) error {
src, err := Open(ctx, srcURL)
if err != nil {
return fmt.Errorf("error opening source: %w", err)
}
dst, err := Create(ctx, dstURL)
if err != nil {
_ = src.Close()
return fmt.Errorf("error opening destination: %w", err)
}
_, err = io.Copy(dst, NewProgressReader(src, GetProgressFunc(ctx)))
if err != nil {
_ = src.Close()
_ = dst.Close()
return fmt.Errorf("error copying from source to destination: %w", err)
}
err = src.Close()
if err != nil {
_ = dst.Close()
return fmt.Errorf("error closing source: %w", err)
}
err = dst.Close()
if err != nil {
return fmt.Errorf("error closing destination: %w", err)
}
return nil
}
// Create creates a file.
func Create(ctx context.Context, dstURL *url.URL) (io.WriteCloser, error) {
registry.RLock()
dstFS, ok := registry.m[dstURL.Scheme]
registry.RUnlock()
if !ok {
return nil, fmt.Errorf("unknown destination scheme: %s", dstURL.Scheme)
}
return dstFS().Create(ctx, dstURL)
}
// Open opens a file.
func Open(ctx context.Context, srcURL *url.URL) (io.ReadCloser, error) {
registry.RLock()
srcFS, ok := registry.m[srcURL.Scheme]
registry.RUnlock()
if !ok {
return nil, fmt.Errorf("unknown source scheme: %s", srcURL.Scheme)
}
return srcFS().Open(ctx, srcURL)
}
// Stat gets the file info for a file.
func Stat(ctx context.Context, srcURL *url.URL) (FileInfo, error) {
registry.RLock()
srcFS, ok := registry.m[srcURL.Scheme]
registry.RUnlock()
if !ok {
return nil, fmt.Errorf("unknown source scheme: %s", srcURL.Scheme)
}
return srcFS().Stat(ctx, srcURL)
}