-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_store.go
79 lines (65 loc) · 1.64 KB
/
file_store.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
package files
import (
"fmt"
"net/url"
"strings"
)
// IFileStore Files store interface
// This interface is used for concrete implementation of any file store (local file system, HTTP files, Google / AWS Buckets etc.
type IFileStore interface {
// URI returns the resource URI with schema
// Schema can be: file, gcs, http etc
URI() string
// List files in the URI using regexp filter
List(filter string) (result []IFile, err error)
// Apply action on files in the file store
Apply(filter string, action func(string)) error
// Exists test for resource existence
Exists(uri string) (result bool)
// Delete resource
Delete(uri string) (err error)
}
// CombineUri creates a URI from segments
func CombineUri(uri string, parts ...string) string {
if strings.HasSuffix(uri, "/") {
uri = uri[:len(uri)-1]
}
result := uri
for _, part := range parts {
if strings.HasSuffix(part, "/") {
part = part[:len(part)-1]
}
result = fmt.Sprintf("%s/%s", result, part)
}
return result
}
// GetUriPath extract path from the uri
func GetUriPath(uri string) (path string, err error) {
if Url, er := url.Parse(uri); er != nil {
return "", er
} else {
return Url.Path, nil
}
}
// ParseUri decompose URI to parts
func ParseUri(uri string) (schema, path, file, ext string, err error) {
Url, er := url.Parse(uri)
if er != nil {
return "", "", "", "", er
}
schema = Url.Scheme
idx := strings.LastIndex(Url.Path, "/")
if idx < 0 {
path = "/"
} else {
path = Url.Path[0:idx]
}
ide := strings.LastIndex(Url.Path, ".")
if ide == -1 {
file = Url.Path[idx+1:]
} else {
file = Url.Path[idx+1 : ide]
ext = Url.Path[ide+1:]
}
return
}