Skip to content

Commit

Permalink
windows path fixes [#38]
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Nov 12, 2023
1 parent dd243c7 commit 3bcea87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pmtiles/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
11 changes: 11 additions & 0 deletions pmtiles/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pmtiles

import (
"github.com/stretchr/testify/assert"
"os"
"strings"
"testing"
)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 3bcea87

Please sign in to comment.