Repository navigation
Expand file tree
/
Copy pathmessage_media.go
More file actions
120 lines (114 loc) · 3.17 KB
/
Copy pathmessage_media.go
File metadata and controls
120 lines (114 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package gotdbot
// GetFile returns the primary media file attached to the message, or nil.
// For a photo, it returns the file from the largest available photo size.
func (m *Message) GetFile() *File {
if m == nil {
return nil
}
switch content := m.Content.(type) {
case *MessagePhoto:
if content != nil && content.Photo != nil {
var largest *File
var largestArea int64
for _, size := range content.Photo.Sizes {
area := int64(size.Width) * int64(size.Height)
if size.Photo != nil && (largest == nil || area >= largestArea) {
largest, largestArea = size.Photo, area
}
}
return largest
}
case *MessageVideo:
if content != nil && content.Video != nil {
return content.Video.Video
}
case *MessageSticker:
if content != nil && content.Sticker != nil {
return content.Sticker.Sticker
}
case *MessageAnimation:
if content != nil && content.Animation != nil {
return content.Animation.Animation
}
case *MessageAudio:
if content != nil && content.Audio != nil {
return content.Audio.Audio
}
case *MessageDocument:
if content != nil && content.Document != nil {
return content.Document.Document
}
case *MessageVoiceNote:
if content != nil && content.VoiceNote != nil {
return content.VoiceNote.Voice
}
case *MessageVideoNote:
if content != nil && content.VideoNote != nil {
return content.VideoNote.Video
}
}
return nil
}
// RemoteFileID returns the remote file ID.
func (m *Message) RemoteFileID() string {
if file := m.GetFile(); file != nil && file.Remote != nil {
return file.Remote.Id
}
return ""
}
// RemoteDuration returns the remote media duration.
func (m *Message) RemoteDuration() int32 {
if m == nil {
return 0
}
switch content := m.Content.(type) {
case *MessageVideo:
if content != nil && content.Video != nil {
return content.Video.Duration
}
case *MessageAnimation:
if content != nil && content.Animation != nil {
return content.Animation.Duration
}
case *MessageAudio:
if content != nil && content.Audio != nil {
return content.Audio.Duration
}
case *MessageVoiceNote:
if content != nil && content.VoiceNote != nil {
return content.VoiceNote.Duration
}
case *MessageVideoNote:
if content != nil && content.VideoNote != nil {
return content.VideoNote.Duration
}
}
return 0
}
// RemoteFileSize returns the size, or the expected size if not known yet.
func (m *Message) RemoteFileSize() int64 {
if file := m.GetFile(); file != nil {
if file.Size > 0 {
return file.Size
}
return file.ExpectedSize
}
return 0
}
// RemoteUniqueFileID returns the remote unique file ID.
func (m *Message) RemoteUniqueFileID() string {
if file := m.GetFile(); file != nil && file.Remote != nil {
return file.Remote.UniqueId
}
return ""
}
// Download downloads the media file attached to the message.
// It uses the attached File.Id and returns ErrNoMedia if no file is attached.
// Use the client that received the message, since file IDs belong to that client.
func (m *Message) Download(c *Client, priority int32, opts *DownloadFileOpts) (*File, error) {
fileInfo := m.GetFile()
if fileInfo == nil {
return nil, ErrNoMedia
}
return c.DownloadFile(fileInfo.Id, priority, opts)
}