-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkodofsx.go
50 lines (40 loc) · 1.18 KB
/
kodofsx.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
package kodofs
import (
"context"
"fmt"
"net/http"
"github.com/qiniu/x/http/fsx"
"github.com/xushiwei/kodofs/kodoutil"
)
// -----------------------------------------------------------------------------------------
const (
Scheme = kodoutil.Scheme
)
func init() {
fsx.Register(Scheme, Open)
}
// Open a kodofs file system by url in form of "kodo:<bucketName>?<token>".
func Open(ctx context.Context, url string) (_ http.FileSystem, _ fsx.Closer, err error) {
bucket, ak, sk, err := kodoutil.Parse(url)
if err != nil {
return
}
host, ok := hosts[bucket]
if !ok {
err = fmt.Errorf("host of bucket `%s` not found, please call kodofs.Register", bucket)
return
}
return New(ak, sk, bucket, host, nil), nil, nil
}
// -----------------------------------------------------------------------------------------
var (
hosts = make(map[string]string, 8)
)
// Register registers (bucket, host) pairs for kodofs.
func Register(bucketHostPairs ...string) {
for i, n := 0, len(bucketHostPairs); i < n; i += 2 {
bucket, host := bucketHostPairs[i], bucketHostPairs[i+1]
hosts[bucket] = host
}
}
// -----------------------------------------------------------------------------------------