From fcf697ed363b3d41d63d88b9070124f0945a167b Mon Sep 17 00:00:00 2001 From: "Christian G. Warden" Date: Wed, 14 Feb 2024 11:53:21 -0600 Subject: [PATCH 1/3] Fix Copying Paths Containing Escaped Slashes --- file_uri.go | 3 +++ file_uri_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/file_uri.go b/file_uri.go index 6078941..be10808 100644 --- a/file_uri.go +++ b/file_uri.go @@ -37,6 +37,9 @@ func FileURINew(path string) (*FileURI, error) { if uri.Path == "" && uri.Scheme == "s3" { uri.Path = "/" } + if uri.Scheme == "file" { + uri.Path = u.EscapedPath() + } return &uri, nil } diff --git a/file_uri_test.go b/file_uri_test.go index 2ca6a01..240f510 100644 --- a/file_uri_test.go +++ b/file_uri_test.go @@ -46,4 +46,16 @@ func TestURI(t *testing.T) { if turi := uri.Join("new/file.txt"); turi.Path != "test/of/new/file.txt" { t.Error("error join new/file.txt", turi.Path) } + + value = "test/of/test%2Fwith%2Fslashes.txt" + uri, err = FileURINew(value) + if err != nil { + t.Error("error parsing ", value) + } + if uri.Path != "test/of/test%2Fwith%2Fslashes.txt" || uri.Scheme != "file" || uri.Bucket != "" { + t.Error("error parsing ", value) + } + if uri.String() != "file://test/of/test%2Fwith%2Fslashes.txt" { + t.Error("roundtrip ", value, uri.String()) + } } From 6e4eabaa5c4359866286fb44ce33a0441cf47e35 Mon Sep 17 00:00:00 2001 From: "Christian G. Warden" Date: Wed, 14 Feb 2024 12:05:39 -0600 Subject: [PATCH 2/3] Fix Paths Containing Slashes and Spaces --- file_uri.go | 6 +++++- file_uri_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/file_uri.go b/file_uri.go index be10808..76e9981 100644 --- a/file_uri.go +++ b/file_uri.go @@ -38,7 +38,11 @@ func FileURINew(path string) (*FileURI, error) { uri.Path = "/" } if uri.Scheme == "file" { - uri.Path = u.EscapedPath() + if u.RawPath != "" { + uri.Path = u.RawPath + } else { + uri.Path = u.EscapedPath() + } } return &uri, nil diff --git a/file_uri_test.go b/file_uri_test.go index 240f510..1a12f7f 100644 --- a/file_uri_test.go +++ b/file_uri_test.go @@ -58,4 +58,16 @@ func TestURI(t *testing.T) { if uri.String() != "file://test/of/test%2Fwith%2Fslashes.txt" { t.Error("roundtrip ", value, uri.String()) } + + value = "test/of/test%2Fwith%2Fslashes and spaces.txt" + uri, err = FileURINew(value) + if err != nil { + t.Error("error parsing ", value) + } + if uri.Path != "test/of/test%2Fwith%2Fslashes and spaces.txt" || uri.Scheme != "file" || uri.Bucket != "" { + t.Error("error parsing ", value) + } + if uri.String() != "file://test/of/test%2Fwith%2Fslashes and spaces.txt" { + t.Error("roundtrip ", value, uri.String()) + } } From aaf91870162753e9a67ba86a68626e636bb0d32e Mon Sep 17 00:00:00 2001 From: "Christian G. Warden" Date: Wed, 14 Feb 2024 12:29:22 -0600 Subject: [PATCH 3/3] Fix Writing Paths With Escaped Slashes and Spaces to S3 --- file_uri.go | 17 ++++------------- file_uri_test.go | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/file_uri.go b/file_uri.go index 76e9981..444a18e 100644 --- a/file_uri.go +++ b/file_uri.go @@ -25,24 +25,15 @@ func FileURINew(path string) (*FileURI, error) { uri := FileURI{ Scheme: u.Scheme, Bucket: u.Host, - Path: u.Path, } if uri.Scheme == "" { uri.Scheme = "file" } - if uri.Scheme == "s3" && uri.Path != "" { - uri.Path = uri.Path[1:] - } - if uri.Path == "" && uri.Scheme == "s3" { - uri.Path = "/" - } - if uri.Scheme == "file" { - if u.RawPath != "" { - uri.Path = u.RawPath - } else { - uri.Path = u.EscapedPath() - } + if u.RawPath != "" { + uri.Path = u.RawPath + } else { + uri.Path = u.EscapedPath() } return &uri, nil diff --git a/file_uri_test.go b/file_uri_test.go index 1a12f7f..71e1999 100644 --- a/file_uri_test.go +++ b/file_uri_test.go @@ -70,4 +70,19 @@ func TestURI(t *testing.T) { if uri.String() != "file://test/of/test%2Fwith%2Fslashes and spaces.txt" { t.Error("roundtrip ", value, uri.String()) } + + value = "s3://bucket/test/of/test%2Fwith%2Fslashes and spaces.txt" + uri, err = FileURINew(value) + if err != nil { + t.Error("error parsing ", value) + } + if uri.Path != "/test/of/test%2Fwith%2Fslashes and spaces.txt" { + t.Errorf("expected path %s; got %s", "/test/of/test%2Fwith%2Fslashes and spaces.txt", value) + } + if uri.Scheme != "s3" || uri.Bucket != "bucket" { + t.Error("error parsing ", value) + } + if uri.String() != "s3://bucket/test/of/test%2Fwith%2Fslashes and spaces.txt" { + t.Error("roundtrip ", value, uri.String()) + } }