Skip to content

Commit fa101f4

Browse files
committed
fix(tdler): only apply EOF guard when size is known
Photos pass size 0 (unknown, not available in InputPhotoFileLocation); the guard then answered the first request at offset 0 with an empty chunk, silently saving every photo as a 0-byte file. Fixes #238
1 parent 784888f commit fa101f4

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎common/tdler/dler.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type eofAwareClient struct {
2828
}
2929

3030
func (c eofAwareClient) UploadGetFile(ctx context.Context, req *tg.UploadGetFileRequest) (tg.UploadFileClass, error) {
31-
if req.Offset >= c.size {
31+
if c.size > 0 && req.Offset >= c.size {
3232
return &tg.UploadFile{}, nil
3333
}
3434
return c.Client.UploadGetFile(ctx, req)

‎common/tdler/dler_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,31 @@ func TestDownloadServerLikeEOF(t *testing.T) {
110110
})
111111
}
112112
}
113+
114+
// Photos have no size in InputPhotoFileLocation, so TGFile.Size() is 0
115+
// ("unknown"). The EOF guard must not fire on the very first request at
116+
// offset 0, or the download silently yields a 0-byte file.
117+
func TestDownloadPhotoUnknownSize(t *testing.T) {
118+
data := make([]byte, 89708)
119+
for i := range data {
120+
data[i] = byte(i % 251)
121+
}
122+
client := &serverLikeClient{data: data}
123+
file := tfile.NewTGFile(
124+
&tg.InputPhotoFileLocation{ID: 1, AccessHash: 2},
125+
client, 0, "photo.png",
126+
)
127+
128+
dl := NewDownloader(file)
129+
buf := make([]byte, len(data))
130+
_, err := dl.WithThreads(1).Parallel(context.Background(), &memWriterAt{b: buf})
131+
if err != nil {
132+
t.Fatalf("download failed: %v", err)
133+
}
134+
if !bytes.Equal(buf, data) {
135+
t.Fatalf("downloaded %d bytes, want %d matching bytes", len(buf), len(data))
136+
}
137+
if client.maxOffset >= int64(len(data)) {
138+
t.Fatalf("requested offset %d at or past EOF (size %d)", client.maxOffset, len(data))
139+
}
140+
}

0 commit comments

Comments
 (0)