Skip to content

Commit 7aa92b7

Browse files
committed
support for download of animated pfps
1 parent c83b471 commit 7aa92b7

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

telegram/media.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,8 @@ type DownloadOptions struct {
407407
ThumbOnly bool `json:"thumb_only,omitempty"`
408408
// Thumb size to download
409409
ThumbSize PhotoSize `json:"thumb_size,omitempty"`
410+
// Weather to download video file (profile photo, etc)
411+
IsVideo bool `json:"is_video,omitempty"`
410412
}
411413

412414
type Destination struct {
@@ -445,6 +447,7 @@ func (c *Client) DownloadMedia(file any, Opts ...*DownloadOptions) (string, erro
445447
location, dc, size, fileName, err := GetFileLocation(file, FileLocationOptions{
446448
ThumbOnly: opts.ThumbOnly,
447449
ThumbSize: opts.ThumbSize,
450+
Video: opts.IsVideo,
448451
})
449452
if err != nil {
450453
return "", err

telegram/utils.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,13 @@ func MatchError(err error, str string) bool {
195195
type FileLocationOptions struct {
196196
ThumbOnly bool
197197
ThumbSize PhotoSize
198+
Video bool
198199
}
199200

200201
// GetFileLocation returns file location, datacenter, file size and file name
201202
func GetFileLocation(file any, opts ...FileLocationOptions) (InputFileLocation, int32, int64, string, error) {
203+
var opt = getVariadic(opts, FileLocationOptions{})
204+
202205
var (
203206
location any
204207
dataCenter int32 = 4
@@ -247,10 +250,10 @@ mediaMessageSwitch:
247250
}
248251
switch l := location.(type) {
249252
case *DocumentObj:
250-
if len(opts) > 0 && opts[0].ThumbOnly {
253+
if opt.ThumbOnly {
251254
var selectedThumb = l.Thumbs[0]
252-
if opts[0].ThumbSize != nil {
253-
selectedThumb = opts[0].ThumbSize
255+
if opt.ThumbSize != nil {
256+
selectedThumb = opt.ThumbSize
254257
}
255258

256259
size, sizeType := getPhotoSize(selectedThumb)
@@ -269,6 +272,17 @@ mediaMessageSwitch:
269272
ThumbSize: "",
270273
}, l.DcID, l.Size, GetFileName(l), nil
271274
case *PhotoObj:
275+
if len(l.VideoSizes) > 0 && opt.Video {
276+
var selectedThumb = l.VideoSizes[len(l.VideoSizes)-1]
277+
size, sizeType := getVideoSize(selectedThumb)
278+
return &InputPhotoFileLocation{
279+
ID: l.ID,
280+
AccessHash: l.AccessHash,
281+
FileReference: l.FileReference,
282+
ThumbSize: sizeType,
283+
}, l.DcID, size, GetFileName(l, true), nil
284+
}
285+
272286
var selectedThumb = l.Sizes[len(l.Sizes)-1]
273287
if len(opts) > 0 && opts[0].ThumbOnly {
274288
if opts[0].ThumbSize != nil {
@@ -312,6 +326,15 @@ func getPhotoSize(sizes PhotoSize) (int64, string) {
312326
}
313327
}
314328

329+
func getVideoSize(sizes VideoSize) (int64, string) {
330+
switch s := sizes.(type) {
331+
case *VideoSizeObj:
332+
return int64(s.Size), s.Type
333+
}
334+
335+
return 0, ""
336+
}
337+
315338
func getMax(a []int32) int32 {
316339
if len(a) == 0 {
317340
return 0
@@ -342,7 +365,9 @@ func sanitizePath(path string, filename string) string {
342365
// *MessageMedia
343366
// *Document
344367
// *Photo
345-
func GetFileName(f any) string {
368+
func GetFileName(f any, video ...bool) string {
369+
var isVid = getVariadic(video, false)
370+
346371
getDocName := func(doc *DocumentObj) string {
347372
var name string
348373
for _, attr := range doc.Attributes {
@@ -377,12 +402,18 @@ func GetFileName(f any) string {
377402
case *MessageMediaDocument:
378403
return getDocName(f.Document.(*DocumentObj))
379404
case *MessageMediaPhoto:
405+
if isVid {
406+
return fmt.Sprintf("video_%s_%d.mp4", time.Now().Format("2006-01-02_15-04-05"), rand.Intn(1000))
407+
}
380408
return fmt.Sprintf("photo_%s_%d.jpg", time.Now().Format("2006-01-02_15-04-05"), rand.Intn(1000))
381409
case *MessageMediaContact:
382410
return fmt.Sprintf("contact_%s_%d.vcf", f.FirstName, rand.Intn(1000))
383411
case *DocumentObj:
384412
return getDocName(f)
385413
case *PhotoObj:
414+
if isVid {
415+
return fmt.Sprintf("video_%s_%d.mp4", time.Now().Format("2006-01-02_15-04-05"), rand.Intn(1000))
416+
}
386417
return fmt.Sprintf("photo_%s_%d.jpg", time.Now().Format("2006-01-02_15-04-05"), rand.Intn(1000))
387418
case *InputPeerPhotoFileLocation:
388419
return fmt.Sprintf("profile_photo_%s_%d.jpg", time.Now().Format("2006-01-02_15-04-05"), rand.Intn(1000))

0 commit comments

Comments
 (0)