From 08536b96303c4b696ec076645f54aaa853e66cc8 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Sun, 25 Feb 2024 22:49:14 +0800 Subject: [PATCH] file bucket can return bytes than requested --- pmtiles/bucket.go | 10 +++++++--- pmtiles/server_test.go | 3 --- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pmtiles/bucket.go b/pmtiles/bucket.go index bf985ba..f48724b 100644 --- a/pmtiles/bucket.go +++ b/pmtiles/bucket.go @@ -60,11 +60,15 @@ func (m mockBucket) NewRangeReaderEtag(_ context.Context, key string, offset int if len(etag) > 0 && resultEtag != etag { return nil, "", 412, &RefreshRequiredError{} } - if offset+length > int64(len(bs)) { + if offset > int64(len(bs)) { return nil, "", 416, &RefreshRequiredError{416} } - return io.NopCloser(bytes.NewReader(bs[offset:(offset + length)])), resultEtag, 206, nil + end := offset + length + if end > int64(len(bs)) { + end = int64(len(bs)) + } + return io.NopCloser(bytes.NewReader(bs[offset:end])), resultEtag, 206, nil } // FileBucket is a bucket backed by a directory on disk @@ -125,7 +129,7 @@ func (b FileBucket) NewRangeReaderEtag(_ context.Context, key string, offset, le result := make([]byte, length) read, err := file.ReadAt(result, offset) - if err == io.EOF && offset == 0 { + if err == io.EOF { part := result[0:read] return io.NopCloser(bytes.NewReader(part)), newEtag, 206, nil } diff --git a/pmtiles/server_test.go b/pmtiles/server_test.go index 76bbf46..38263d5 100644 --- a/pmtiles/server_test.go +++ b/pmtiles/server_test.go @@ -102,9 +102,6 @@ func fakeArchive(t *testing.T, header HeaderV3, metadata map[string]interface{}, archiveBytes = append(archiveBytes, metadataBytes...) archiveBytes = append(archiveBytes, leavesBytes...) archiveBytes = append(archiveBytes, tileDataBytes...) - if len(archiveBytes) < 16384 { - archiveBytes = append(archiveBytes, make([]byte, 16384-len(archiveBytes))...) - } return archiveBytes }