fix path validation with sj:/bucket (#1543)
* Validates before uplink attempts to download or upload * Separate Validate function out to utils package and add tests * Adds another test case * Move proper URL Handling to FPath Package * Add individual check notes and further test cases * Fix Comment Spacing
This commit is contained in:
parent
ea4a61f0c0
commit
a55a9538c9
@ -37,36 +37,37 @@ func New(p string) (FPath, error) {
|
||||
|
||||
var u *url.URL
|
||||
var err error
|
||||
// Loops to ensure URL is formatted correctly and does not get malformed during url.Parse()
|
||||
for {
|
||||
u, err = url.Parse(p)
|
||||
if err != nil {
|
||||
return fp, fmt.Errorf("malformed URL: %v, use format sj://bucket/", err)
|
||||
}
|
||||
|
||||
// no scheme means local path
|
||||
if u.Scheme == "" {
|
||||
fp.local = true
|
||||
return fp, nil
|
||||
}
|
||||
|
||||
// not a valid scheme (s3, sj)
|
||||
if _, validScheme := storjScheme[u.Scheme]; !validScheme {
|
||||
return fp, fmt.Errorf("unsupported URL scheme: %s, use format sj://bucket/", u.Scheme)
|
||||
}
|
||||
|
||||
// empty url
|
||||
if u.Host == "" && u.Path == "" {
|
||||
return fp, errors.New("no bucket specified, use format sj://bucket/")
|
||||
}
|
||||
|
||||
// u.host equals the bucket name, if existing url is valid
|
||||
if u.Host != "" {
|
||||
break
|
||||
}
|
||||
|
||||
p = strings.Replace(p, ":///", "://", 1)
|
||||
// remove additional / if url.Parse() corrects from sj:/bucket to sj:///bucket
|
||||
p = strings.Replace(u.String(), ":///", "://", 1)
|
||||
}
|
||||
|
||||
// port was specified but is not necessary/allowed
|
||||
if u.Port() != "" {
|
||||
return fp, errors.New("port in Storj URL is not supported, use format sj://bucket/")
|
||||
}
|
||||
|
||||
// set path information from url
|
||||
fp.bucket = u.Host
|
||||
if u.Path != "" {
|
||||
fp.path = strings.TrimLeft(path.Clean(u.Path), "/")
|
||||
|
@ -20,6 +20,27 @@ func TestStorjURL(t *testing.T) {
|
||||
base string
|
||||
joint string
|
||||
}{
|
||||
{
|
||||
url: "sj:/mybucket",
|
||||
bucket: "mybucket",
|
||||
path: "",
|
||||
base: "",
|
||||
joint: "suffix",
|
||||
},
|
||||
{
|
||||
url: "sj:/mybucket/",
|
||||
bucket: "mybucket",
|
||||
path: "",
|
||||
base: "",
|
||||
joint: "suffix",
|
||||
},
|
||||
{
|
||||
url: "sj:/mybucket/myfile",
|
||||
bucket: "mybucket",
|
||||
path: "myfile",
|
||||
base: "myfile",
|
||||
joint: "suffix",
|
||||
},
|
||||
{
|
||||
url: "sj://mybucket",
|
||||
bucket: "mybucket",
|
||||
@ -95,6 +116,7 @@ func TestStorjURL(t *testing.T) {
|
||||
func TestInvalidStorjURL(t *testing.T) {
|
||||
for i, tt := range []string{
|
||||
"://",
|
||||
"sj:bucket",
|
||||
"sj://",
|
||||
"sj:///",
|
||||
"sj://mybucket:8080/",
|
||||
|
Loading…
Reference in New Issue
Block a user