From 45a7e117eaa5e06c5dba27d20731af407b4b735b Mon Sep 17 00:00:00 2001 From: iamxvbaba <28732408+iamxvbaba@users.noreply.github.com> Date: Thu, 30 Jul 2026 16:05:14 +0800 Subject: [PATCH] chore(giftfetch): sync NFT gift export fields --- cmd/giftfetch/main.go | 13 +++++++------ cmd/giftfetch/main_test.go | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cmd/giftfetch/main.go b/cmd/giftfetch/main.go index 7736ab6e..bad96e32 100644 --- a/cmd/giftfetch/main.go +++ b/cmd/giftfetch/main.go @@ -830,12 +830,13 @@ func downloadPartSize(expectedSize int64) int { if expectedSize <= 0 || expectedSize >= max { return int(max) } - // Choose a valid 4 KiB-aligned limit strictly larger than the file whenever - // possible, so downloader.Stream recognizes the first short chunk as final - // without an extra EOF probe. - partSize := ((expectedSize + 1 + unit - 1) / unit) * unit - if partSize > max { - partSize = max + // Non-precise upload.getFile limits must use the client-compatible chunk + // ladder (4, 8, ..., 512 KiB), whose values also divide a 1 MiB window. + // Merely rounding to an arbitrary 4 KiB multiple (for example 48 KiB) + // is rejected with LIMIT_INVALID by some official file DCs. + partSize := unit + for partSize <= expectedSize && partSize < max { + partSize *= 2 } return int(partSize) } diff --git a/cmd/giftfetch/main_test.go b/cmd/giftfetch/main_test.go index 93233f80..da4a62c9 100644 --- a/cmd/giftfetch/main_test.go +++ b/cmd/giftfetch/main_test.go @@ -69,6 +69,7 @@ func TestDownloadPartSize(t *testing.T) { {size: 1, want: 4 << 10}, {size: (4 << 10) - 1, want: 4 << 10}, {size: 4 << 10, want: 8 << 10}, + {size: 48_632, want: 64 << 10}, {size: (512 << 10) - 1, want: 512 << 10}, {size: 512 << 10, want: 512 << 10}, {size: 1 << 20, want: 512 << 10}, @@ -80,6 +81,22 @@ func TestDownloadPartSize(t *testing.T) { } } +func TestDownloadPartSizeUsesNonPreciseChunkLadder(t *testing.T) { + const oneMiB = 1 << 20 + for size := int64(1); size < 512<<10; size += 997 { + partSize := downloadPartSize(size) + if partSize < 4<<10 || partSize > 512<<10 || partSize%(4<<10) != 0 { + t.Fatalf("downloadPartSize(%d) = %d is outside the valid 4 KiB-aligned range", size, partSize) + } + if oneMiB%partSize != 0 { + t.Fatalf("downloadPartSize(%d) = %d does not divide a 1 MiB request window", size, partSize) + } + if int64(partSize) <= size { + t.Fatalf("downloadPartSize(%d) = %d does not cover the known-size single chunk", size, partSize) + } + } +} + func TestParseAllowedMissingThumbs(t *testing.T) { allowed, err := parseAllowedMissingThumbs("5417911440709285239:photo:m,42:video:v") if err != nil {