-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* All operations support public HTTP endpoints [#68] * fix normalization of local paths via filepath.Abs * force the parameter awssdk=v2 for s3 buckets to use newer SDK * handle maxzoom-only extracts [#68, #64] * fix default maxzoom; support raw geojson geometries [#68, #64]
- Loading branch information
Showing
8 changed files
with
326 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package pmtiles | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"gocloud.dev/blob" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
type Bucket interface { | ||
Close() error | ||
NewRangeReader(ctx context.Context, key string, offset int64, length int64) (io.ReadCloser, error) | ||
} | ||
|
||
type HttpBucket struct { | ||
baseURL string | ||
} | ||
|
||
func (b HttpBucket) NewRangeReader(ctx context.Context, key string, offset, length int64) (io.ReadCloser, error) { | ||
reqURL := b.baseURL + "/" + key | ||
|
||
req, err := http.NewRequest("GET", reqURL, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1)) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { | ||
resp.Body.Close() | ||
return nil, fmt.Errorf("HTTP error: %d", resp.StatusCode) | ||
} | ||
|
||
return resp.Body, nil | ||
} | ||
|
||
func (b HttpBucket) Close() error { | ||
return nil | ||
} | ||
|
||
type BucketAdapter struct { | ||
Bucket *blob.Bucket | ||
} | ||
|
||
func (ba BucketAdapter) NewRangeReader(ctx context.Context, key string, offset, length int64) (io.ReadCloser, error) { | ||
reader, err := ba.Bucket.NewRangeReader(ctx, key, offset, length, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return reader, nil | ||
} | ||
|
||
func (ba BucketAdapter) Close() error { | ||
return ba.Bucket.Close() | ||
} | ||
|
||
func NormalizeBucketKey(bucket string, prefix string, key string) (string, string, error) { | ||
if bucket == "" { | ||
if strings.HasPrefix(key, "http") { | ||
u, err := url.Parse(key) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
dir, file := path.Split(u.Path) | ||
if strings.HasSuffix(dir, "/") { | ||
dir = dir[:len(dir)-1] | ||
} | ||
return u.Scheme + "://" + u.Host + dir, file, nil | ||
} else { | ||
if prefix != "" { | ||
abs, err := filepath.Abs(prefix) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return "file://" + abs, key, nil | ||
} | ||
abs, err := filepath.Abs(key) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
return "file://" + filepath.Dir(abs), filepath.Base(abs), nil | ||
} | ||
} | ||
|
||
if strings.HasPrefix(bucket, "s3") { | ||
u, err := url.Parse(bucket) | ||
if err != nil { | ||
fmt.Println("Error parsing URL:", err) | ||
return "", "", err | ||
} | ||
values := u.Query() | ||
values.Set("awssdk", "v2") | ||
u.RawQuery = values.Encode() | ||
return u.String(), key, nil | ||
} | ||
return bucket, key, nil | ||
} | ||
|
||
func OpenBucket(ctx context.Context, bucketURL string, bucketPrefix string) (Bucket, error) { | ||
if strings.HasPrefix(bucketURL, "http") { | ||
bucket := HttpBucket{bucketURL} | ||
return bucket, nil | ||
} else { | ||
bucket, err := blob.OpenBucket(ctx, bucketURL) | ||
if bucketPrefix != "/" && bucketPrefix != "." { | ||
bucket = blob.PrefixedBucket(bucket, path.Clean(bucketPrefix)+string(os.PathSeparator)) | ||
} | ||
wrapped_bucket := BucketAdapter{bucket} | ||
return wrapped_bucket, err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pmtiles | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"strings" | ||
"fmt" | ||
) | ||
|
||
|
||
func TestNormalizeLocalFile(t *testing.T) { | ||
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) | ||
assert.Equal(t, "http://example.com/foo", bucket) | ||
} | ||
|
||
func TestNormalizeAwsSdkVersion(t *testing.T) { | ||
bucket, key, _ := NormalizeBucketKey("s3://mybucket?awssdk=v1&endpoint=https://foo.bar", "", "abc") | ||
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) | ||
fmt.Println(bucket) | ||
assert.True(t, strings.HasSuffix(bucket, "/foo")) | ||
assert.True(t, strings.HasPrefix(bucket, "file://")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.