From 3bcea87f077b695dcae04d1bf2304eba9b38a2df Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Sun, 12 Nov 2023 16:12:14 +0800 Subject: [PATCH] windows path fixes [#38] --- pmtiles/bucket.go | 8 ++++++-- pmtiles/bucket_test.go | 11 +++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pmtiles/bucket.go b/pmtiles/bucket.go index 9353f92..100d00b 100644 --- a/pmtiles/bucket.go +++ b/pmtiles/bucket.go @@ -78,18 +78,22 @@ func NormalizeBucketKey(bucket string, prefix string, key string) (string, strin } return u.Scheme + "://" + u.Host + dir, file, nil } else { + fileprotocol := "file://" + if string(os.PathSeparator) != "/" { + fileprotocol += "/" + } if prefix != "" { abs, err := filepath.Abs(prefix) if err != nil { return "", "", err } - return "file://" + abs, key, nil + return fileprotocol + filepath.ToSlash(abs), key, nil } abs, err := filepath.Abs(key) if err != nil { return "", "", err } - return "file://" + filepath.Dir(abs), filepath.Base(abs), nil + return fileprotocol + filepath.ToSlash(filepath.Dir(abs)), filepath.Base(abs), nil } } diff --git a/pmtiles/bucket_test.go b/pmtiles/bucket_test.go index ee63607..9e009e7 100644 --- a/pmtiles/bucket_test.go +++ b/pmtiles/bucket_test.go @@ -2,6 +2,7 @@ package pmtiles import ( "github.com/stretchr/testify/assert" + "os" "strings" "testing" ) @@ -13,6 +14,15 @@ func TestNormalizeLocalFile(t *testing.T) { assert.True(t, strings.HasPrefix(bucket, "file://")) } +func TestNormalizeLocalFileWindows(t *testing.T) { + if string(os.PathSeparator) != "/" { + bucket, key, _ := NormalizeBucketKey("", "", "\\foo\\bar.pmtiles") + assert.Equal(t, "bar.pmtiles", key) + assert.True(t, strings.HasSuffix(bucket, "/foo")) + assert.True(t, strings.HasPrefix(bucket, "file://")) + } +} + func TestNormalizeHttp(t *testing.T) { bucket, key, _ := NormalizeBucketKey("", "", "http://example.com/foo/bar.pmtiles") assert.Equal(t, "bar.pmtiles", key) @@ -24,6 +34,7 @@ func TestNormalizeAwsSdkVersion(t *testing.T) { assert.Equal(t, "abc", key) assert.Equal(t, "s3://mybucket?awssdk=v2&endpoint=https%3A%2F%2Ffoo.bar", bucket) } + func TestNormalizePathPrefixServer(t *testing.T) { bucket, key, _ := NormalizeBucketKey("", "../foo", "") assert.Equal(t, "", key)