Files
SaveAny-Bot/common/tdler/dler.go
T
krau 784888f44c fix(tdler): treat offset past EOF as end of file
gotd's downloader is size-unaware: when a file's size is an exact
multiple of the 1MiB part size, it issues one final upload.getFile
at offset == size, which Telegram rejects with 400 OFFSET_INVALID,
failing the whole download. Wrap the client to answer such requests
with an empty chunk, matching the EOF semantics the downloader
expects. Regression test uses a fake client with real server
behavior (OFFSET_INVALID past EOF).
2026-08-25 14:00:47 +08:00

36 lines
1.2 KiB
Go

package tdler
import (
"context"
"github.com/gotd/td/telegram/downloader"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/common/utils/dlutil"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/pkg/consts/tglimit"
"github.com/krau/SaveAny-Bot/pkg/tfile"
)
func NewDownloader(file tfile.TGFile) *downloader.Builder {
return downloader.NewDownloader().WithPartSize(tglimit.MaxPartSize).
Download(eofAwareClient{Client: file.Dler(), size: file.Size()}, file.Location()).
WithThreads(dlutil.BestThreads(file.Size(), config.C().Threads))
}
// eofAwareClient answers upload.getFile requests at or past the end of the
// file with an empty chunk. gotd's downloader is size-unaware: for files
// whose size is an exact multiple of the part size it issues one final
// request at offset == size and expects an empty chunk, but Telegram rejects
// it with 400 OFFSET_INVALID and the whole download fails.
type eofAwareClient struct {
downloader.Client
size int64
}
func (c eofAwareClient) UploadGetFile(ctx context.Context, req *tg.UploadGetFileRequest) (tg.UploadFileClass, error) {
if req.Offset >= c.size {
return &tg.UploadFile{}, nil
}
return c.Client.UploadGetFile(ctx, req)
}