Skip to content

Commit 64b60d2

Browse files
committed
add thumb download support.
1 parent 2c3b64b commit 64b60d2

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

telegram/media.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"os"
1313
"path/filepath"
14+
"strings"
1415
"sync"
1516
"sync/atomic"
1617
"time"
@@ -383,6 +384,10 @@ type DownloadOptions struct {
383384
DCId int32 `json:"dc_id,omitempty"`
384385
// Destination Writer
385386
Buffer io.Writer `json:"-"`
387+
// Weather to download the thumb only
388+
ThumbOnly bool `json:"thumb_only,omitempty"`
389+
// Thumb size to download
390+
ThumbSize PhotoSize `json:"thumb_size,omitempty"`
386391
}
387392

388393
type Destination struct {
@@ -417,7 +422,11 @@ func (mb *Destination) Close() error {
417422

418423
func (c *Client) DownloadMedia(file any, Opts ...*DownloadOptions) (string, error) {
419424
opts := getVariadic(Opts, &DownloadOptions{})
420-
location, dc, size, fileName, err := GetFileLocation(file)
425+
426+
location, dc, size, fileName, err := GetFileLocation(file, FileLocationOptions{
427+
ThumbOnly: opts.ThumbOnly,
428+
ThumbSize: opts.ThumbSize,
429+
})
421430
if err != nil {
422431
return "", err
423432
}
@@ -525,6 +534,12 @@ func (c *Client) DownloadMedia(file any, Opts ...*DownloadOptions) (string, erro
525534
if handleIfFlood(err, c) {
526535
continue
527536
}
537+
538+
if strings.Contains(err.Error(), "FILE_REFERENCE_EXPIRED") {
539+
c.Log.Debug(err)
540+
return // File reference expired
541+
}
542+
528543
c.Log.Debug(errors.Wrap(err, fmt.Sprintf("part - (%d) - retrying...", p)))
529544
continue
530545
}
@@ -576,6 +591,11 @@ retrySinglePart:
576591
if handleIfFlood(err, c) {
577592
continue
578593
}
594+
if strings.Contains(err.Error(), "FILE_REFERENCE_EXPIRED") {
595+
c.Log.Debug(err)
596+
return // File reference expired
597+
}
598+
579599
c.Log.Debug(errors.Wrap(err, fmt.Sprintf("part - (%d) - retrying...", p)))
580600
continue
581601
}

telegram/utils.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,13 @@ func matchError(err error, str string) bool {
187187
return false
188188
}
189189

190+
type FileLocationOptions struct {
191+
ThumbOnly bool
192+
ThumbSize PhotoSize
193+
}
194+
190195
// GetFileLocation returns file location, datacenter, file size and file name
191-
func GetFileLocation(file any) (InputFileLocation, int32, int64, string, error) {
196+
func GetFileLocation(file any, opts ...FileLocationOptions) (InputFileLocation, int32, int64, string, error) {
192197
var (
193198
location any
194199
dataCenter int32 = 4
@@ -237,14 +242,38 @@ mediaMessageSwitch:
237242
}
238243
switch l := location.(type) {
239244
case *DocumentObj:
245+
if len(opts) > 0 && opts[0].ThumbOnly {
246+
var selectedThumb = l.Thumbs[0]
247+
if opts[0].ThumbSize != nil {
248+
selectedThumb = opts[0].ThumbSize
249+
}
250+
251+
size, sizeType := getPhotoSize(selectedThumb)
252+
return &InputDocumentFileLocation{
253+
ID: l.ID,
254+
AccessHash: l.AccessHash,
255+
FileReference: l.FileReference,
256+
ThumbSize: sizeType,
257+
}, l.DcID, size, GetFileName(l), nil
258+
}
259+
240260
return &InputDocumentFileLocation{
241261
ID: l.ID,
242262
AccessHash: l.AccessHash,
243263
FileReference: l.FileReference,
244264
ThumbSize: "",
245265
}, l.DcID, l.Size, GetFileName(l), nil
246266
case *PhotoObj:
247-
size, sizeType := getPhotoSize(l.Sizes[len(l.Sizes)-1])
267+
var selectedThumb = l.Sizes[len(l.Sizes)-1]
268+
if len(opts) > 0 && opts[0].ThumbOnly {
269+
if opts[0].ThumbSize != nil {
270+
selectedThumb = opts[0].ThumbSize
271+
} else {
272+
selectedThumb = l.Sizes[0]
273+
}
274+
}
275+
276+
size, sizeType := getPhotoSize(selectedThumb)
248277
return &InputPhotoFileLocation{
249278
ID: l.ID,
250279
AccessHash: l.AccessHash,

0 commit comments

Comments
 (0)