-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileystem.go
80 lines (66 loc) · 2.08 KB
/
fileystem.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
package ls3
import (
"github.com/relvacode/ls3/exception"
"io/fs"
)
type BucketFilesystemProvider interface {
// ListBuckets lists all available filesystemProvider in the provider.
ListBuckets() ([]string, error)
// Open returns a filesystem for a given bucket name.
Open(bucket string) (fs.FS, error)
}
// SingleBucketFilesystem implements BucketFilesystemProvider that
// always returns the same filesystem for any bucket name provided.
type SingleBucketFilesystem struct {
fs.FS
}
// ListBuckets always returns the same bucket name.
// The actual name doesn't matter, as the provider will always return the same filesystem.
func (p *SingleBucketFilesystem) ListBuckets() ([]string, error) {
return []string{"any"}, nil
}
func (p *SingleBucketFilesystem) Open(_ string) (fs.FS, error) {
return p.FS, nil
}
type SubdirBucketFilesystem struct {
fs.FS
}
// ListBuckets returns all subdirectories of the base filesystem.
func (p *SubdirBucketFilesystem) ListBuckets() ([]string, error) {
entries, err := fs.ReadDir(p.FS, ".")
if err != nil {
return nil, &exception.Error{
ErrorCode: exception.InternalError,
Message: "Unable to list filesystemProvider at this time.",
}
}
var buckets []string
for _, entry := range entries {
if !entry.IsDir() {
// Ignore non-directories
continue
}
buckets = append(buckets, entry.Name())
}
return buckets, nil
}
// Open returns a subdirectory of the base filesystem for each bucket.
// The error NoSuchBucket is returned if fs.Stat of the bucket path returns an error.
// The error InvalidBucketState is returned if fs.Sub returns an error.
func (p *SubdirBucketFilesystem) Open(bucket string) (fs.FS, error) {
fi, err := fs.Stat(p.FS, bucket)
if err != nil || !fi.IsDir() {
return nil, &exception.Error{
ErrorCode: exception.NoSuchBucket,
Message: "The specified bucket does not exist.",
}
}
sub, err := fs.Sub(p.FS, bucket)
if err != nil {
return nil, &exception.Error{
ErrorCode: exception.InvalidBucketState,
Message: "The request is not valid for the current state of the bucket.",
}
}
return sub, nil
}