-
Notifications
You must be signed in to change notification settings - Fork 32
/
location.go
203 lines (176 loc) · 5.18 KB
/
location.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package gs
import (
"errors"
"path"
"regexp"
"strings"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
"github.com/c2fo/vfs/v5"
"github.com/c2fo/vfs/v5/utils"
)
// Location implements vfs.Location for gs fs.
type Location struct {
fileSystem *FileSystem
prefix string
bucket string
bucketHandle BucketHandleWrapper
}
// String returns the full URI of the location.
func (l *Location) String() string {
return l.URI()
}
// List returns a list of file name strings for the current location.
func (l *Location) List() ([]string, error) {
return l.ListByPrefix("")
}
// ListByPrefix returns a slice of file base names and any error, if any
// List functions return only file basenames
func (l *Location) ListByPrefix(filenamePrefix string) ([]string, error) {
prefix := utils.RemoveLeadingSlash(utils.EnsureTrailingSlash(path.Join(l.prefix, filenamePrefix)))
d := path.Dir(prefix)
q := &storage.Query{
Delimiter: "/",
Prefix: prefix,
Versions: false,
}
handle, err := l.getBucketHandle()
if err != nil {
return nil, err
}
var fileNames []string
it := handle.WrappedObjects(l.fileSystem.ctx, q)
for {
objAttrs, err := it.Next()
if err != nil {
if err == iterator.Done {
break
}
return nil, err
}
// only include objects, not "directories"
if objAttrs.Prefix == "" && objAttrs.Name != d && !strings.HasSuffix(objAttrs.Name, "/") {
fn := strings.TrimPrefix(objAttrs.Name, utils.EnsureTrailingSlash(d))
fileNames = append(fileNames, fn)
}
}
return fileNames, nil
}
// ListByRegex returns a list of file names at the location which match the provided regular expression.
func (l *Location) ListByRegex(regex *regexp.Regexp) ([]string, error) {
keys, err := l.List()
if err != nil {
return []string{}, err
}
var filteredKeys []string
for _, key := range keys {
if regex.MatchString(key) {
filteredKeys = append(filteredKeys, key)
}
}
return filteredKeys, nil
}
// Volume returns the GCS bucket name.
func (l *Location) Volume() string {
return l.bucket
}
// Path returns the path of the file at the current location, starting with a leading '/'
func (l *Location) Path() string {
return utils.EnsureLeadingSlash(utils.EnsureTrailingSlash(l.prefix))
}
// Exists returns whether the location exists or not. In the case of an error, false is returned.
func (l *Location) Exists() (bool, error) {
_, err := l.getBucketAttrs()
if err != nil {
if err == storage.ErrBucketNotExist {
return false, nil
}
return false, err
}
return true, nil
}
// NewLocation creates a new location instance relative to the current location's path.
func (l *Location) NewLocation(relativePath string) (vfs.Location, error) {
if l == nil {
return nil, errors.New("non-nil gs.Location pointer is required")
}
// make a copy of the original location first, then ChangeDir, leaving the original location as-is
newLocation := &Location{}
*newLocation = *l
err := newLocation.ChangeDir(relativePath)
if err != nil {
return nil, err
}
return newLocation, nil
}
// ChangeDir changes the current location's path to the new, relative path.
func (l *Location) ChangeDir(relativePath string) error {
if l == nil {
return errors.New("non-nil gs.Location pointer is required")
}
if relativePath == "" {
return errors.New("non-empty string relativePath is required")
}
err := utils.ValidateRelativeLocationPath(relativePath)
if err != nil {
return err
}
l.prefix = utils.EnsureTrailingSlash(utils.EnsureLeadingSlash(path.Join(l.prefix, relativePath)))
return nil
}
// FileSystem returns the GCS file system instance.
func (l *Location) FileSystem() vfs.FileSystem {
return l.fileSystem
}
// NewFile returns a new file instance at the given path, relative to the current location.
func (l *Location) NewFile(filePath string) (vfs.File, error) {
if l == nil {
return nil, errors.New("non-nil gs.Location pointer is required")
}
if filePath == "" {
return nil, errors.New("non-empty string filePath is required")
}
err := utils.ValidateRelativeFilePath(filePath)
if err != nil {
return nil, err
}
newFile := &File{
fileSystem: l.fileSystem,
bucket: l.bucket,
key: utils.EnsureLeadingSlash(path.Join(l.prefix, filePath)),
}
return newFile, nil
}
// DeleteFile deletes the file at the given path, relative to the current location.
func (l *Location) DeleteFile(fileName string) error {
file, err := l.NewFile(fileName)
if err != nil {
return err
}
return file.Delete()
}
// URI returns a URI string for the GCS location.
func (l *Location) URI() string {
return utils.GetLocationURI(l)
}
// getBucketHandle returns cached Bucket struct for file
func (l *Location) getBucketHandle() (BucketHandleWrapper, error) {
if l.bucketHandle != nil {
return l.bucketHandle, nil
}
client, err := l.fileSystem.Client()
if err != nil {
return nil, err
}
handler := &RetryBucketHandler{Retry: l.fileSystem.Retry(), handler: client.Bucket(l.bucket)}
l.bucketHandle = handler
return l.bucketHandle, nil
}
// getObjectAttrs returns the file's attributes
func (l *Location) getBucketAttrs() (*storage.BucketAttrs, error) {
handle, err := l.getBucketHandle()
if err != nil {
return nil, err
}
return handle.Attrs(l.fileSystem.ctx)
}