Skip to content

Commit

Permalink
#129, fix last part sender not allocated.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmarnathCJD committed Aug 6, 2024
1 parent 74c4115 commit efed1e0
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions telegram/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,9 @@ func (c *Client) DownloadMedia(file interface{}, Opts ...*DownloadOptions) (stri
partOver := size % int64(partSize)

totalParts := parts
if partOver > 0 {
totalParts++
}

numWorkers := countWorkers(parts)
if opts.Threads > 0 {
Expand Down Expand Up @@ -425,8 +428,8 @@ func (c *Client) DownloadMedia(file interface{}, Opts ...*DownloadOptions) (stri
numWorkers = wPreallocated

createAndAppendSender := func(dcId int, senders []Sender, senderIndex int) {
conn, _ := c.CreateExportedSender(dcId)
if conn != nil {
conn, err := c.CreateExportedSender(dcId)
if conn != nil && err == nil {
senders[senderIndex] = Sender{c: conn}
go c.AddNewExportedSenderToMap(dcId, conn)
numWorkers++
Expand Down Expand Up @@ -550,6 +553,14 @@ func (c *Client) DownloadMedia(file interface{}, Opts ...*DownloadOptions) (stri
}
}

if w[0].c == nil {
createAndAppendSender(int(dc), w, 0)
}

if w[0].c == nil {
return "", errors.Wrap(errors.New("failed to create sender for dc "+fmt.Sprint(dc)), "download failed")
}

c.Logger.Debug(fmt.Sprintf("downloading last part %d/%d in chunks of %d", totalParts-1, totalParts, partOver/1024))

buf, err := w[0].c.UploadGetFile(&UploadGetFileParams{
Expand Down

0 comments on commit efed1e0

Please sign in to comment.