-
Notifications
You must be signed in to change notification settings - Fork 99
/
storage_url.go
210 lines (184 loc) · 5.37 KB
/
storage_url.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
204
205
206
207
208
209
210
package lib
import (
"fmt"
"net/url"
"os"
"strings"
)
// SchemePrefix is the prefix of oss url
const SchemePrefix string = "oss://"
type CloudURLType string
const (
CloudURLNone CloudURLType = "none"
CloudURLService CloudURLType = "service"
CloudURLBucket CloudURLType = "bucket"
CloudURLObject CloudURLType = "object"
)
// StorageURLer is the interface for all url
type StorageURLer interface {
IsCloudURL() bool
IsFileURL() bool
ToString() string
}
// CloudURL describes oss url
type CloudURL struct {
urlStr string
bucket string
object string
}
// Init is used to create a cloud url from a user input url
func (cu *CloudURL) Init(urlStr, encodingType string) error {
cu.urlStr = urlStr
if err := cu.parseBucketObject(encodingType); err != nil {
return err
}
if err := cu.checkBucketObject(encodingType); err != nil {
return err
}
return nil
}
func (cu *CloudURL) parseBucketObject(encodingType string) error {
var err error
path := cu.urlStr
if strings.HasPrefix(strings.ToLower(path), SchemePrefix) {
path = string(path[len(SchemePrefix):])
} else {
// deal with the url: /bucket/object
if strings.HasPrefix(path, "/") {
path = string(path[1:])
}
}
sli := strings.SplitN(path, "/", 2)
cu.bucket = sli[0]
if len(sli) > 1 {
cu.object = sli[1]
if encodingType == URLEncodingType {
if cu.object, err = url.QueryUnescape(cu.object); err != nil {
return fmt.Errorf("invalid cloud url: %s, object name is not url encoded, %s", cu.urlStr, err.Error())
}
}
}
return nil
}
func (cu *CloudURL) checkBucketObject(encodingType string) error {
if cu.bucket == "" && cu.object != "" {
return fmt.Errorf("invalid cloud url: %s, miss bucket", cu.urlStr)
}
if encodingType == URLEncodingType && cu.bucket != "" && cu.object == "" {
if bucket, err := url.QueryUnescape(cu.bucket); err == nil && bucket != cu.bucket {
return fmt.Errorf("invalid cloud url: %s, bucket url do not support --encoding-type option", cu.urlStr)
}
}
return nil
}
func (cu *CloudURL) checkObjectPrefix() error {
if strings.HasPrefix(cu.object, "/") {
return fmt.Errorf("invalid cloud url: %s, object name should not begin with \"/\"", cu.urlStr)
}
if strings.HasPrefix(cu.object, "\\") {
return fmt.Errorf("invalid cloud url: %s, object name should not begin with \"\\\"", cu.urlStr)
}
return nil
}
func (cu *CloudURL) checkIsObjectURL() error {
if cu.bucket == "" {
return fmt.Errorf("invalid cloud url: %s, miss bucket", cu.urlStr)
}
if cu.object == "" {
return fmt.Errorf("invalid cloud url: %s, miss object", cu.urlStr)
}
return nil
}
// IsCloudURL shows if the url is a cloud url
func (cu CloudURL) IsCloudURL() bool {
return true
}
// IsFileURL shows if the url is a file url
func (cu CloudURL) IsFileURL() bool {
return false
}
// ToString reconstruct url
func (cu CloudURL) ToString() string {
if cu.object == "" {
return fmt.Sprintf("%s%s", SchemePrefix, cu.bucket)
}
return fmt.Sprintf("%s%s/%s", SchemePrefix, cu.bucket, cu.object)
}
// FileURL describes file url
type FileURL struct {
urlStr string
}
// Init simulate inheritance, and polymorphism
func (fu *FileURL) Init(urlStr, encodingType string) error {
if encodingType == URLEncodingType {
vurl, err := url.QueryUnescape(urlStr)
if err != nil {
return fmt.Errorf("invalid cloud url: %s, file name is not url encoded, %s", urlStr, err.Error())
}
urlStr = vurl
}
if len(urlStr) >= 2 && urlStr[:2] == "~"+string(os.PathSeparator) {
homeDir := currentHomeDir()
if homeDir != "" {
urlStr = strings.Replace(urlStr, "~", homeDir, 1)
} else {
return fmt.Errorf("current home dir is empty")
}
}
fu.urlStr = urlStr
return nil
}
// IsCloudURL simulate inheritance, and polymorphism
func (fu FileURL) IsCloudURL() bool {
return false
}
// IsFileURL simulate inheritance, and polymorphism
func (fu FileURL) IsFileURL() bool {
return true
}
// ToString simulate inheritance, and polymorphism
func (fu FileURL) ToString() string {
return fu.urlStr
}
// StorageURLFromString analysis input url type and build a storage url from the url
func StorageURLFromString(urlStr, encodingType string) (StorageURLer, error) {
if strings.HasPrefix(strings.ToLower(urlStr), SchemePrefix) {
var cloudURL CloudURL
if err := cloudURL.Init(urlStr, encodingType); err != nil {
return nil, err
}
return cloudURL, nil
}
var fileURL FileURL
if err := fileURL.Init(urlStr, encodingType); err != nil {
return nil, err
}
return fileURL, nil
}
// CloudURLFromString get a oss url from url, if url is not a cloud url, return error
func CloudURLFromString(urlStr, encodingType string) (CloudURL, error) {
storageURL, err := StorageURLFromString(urlStr, encodingType)
if err != nil {
return CloudURL{}, err
}
if !storageURL.IsCloudURL() {
return CloudURL{}, fmt.Errorf("invalid cloud url: \"%s\", please make sure the url starts with: \"%s\"", urlStr, SchemePrefix)
}
return storageURL.(CloudURL), nil
}
// ObjectURLFromString get a oss url from url, if url is not a cloud url, return error
func ObjectURLFromString(urlStr, encodingType string) (CloudURL, error) {
cloudURL, err := CloudURLFromString(urlStr, encodingType)
if err != nil {
return cloudURL, err
}
return cloudURL, cloudURL.checkIsObjectURL()
}
// CloudURLToString format url string from input
func CloudURLToString(bucket string, object string) string {
cloudURL := CloudURL{
bucket: bucket,
object: object,
}
return cloudURL.ToString()
}