-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_media.go
More file actions
194 lines (175 loc) · 4.57 KB
/
Copy pathconvert_media.go
File metadata and controls
194 lines (175 loc) · 4.57 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package botapi
import (
"github.com/gotd/td/fileid"
"github.com/gotd/td/tg"
)
// encodeFileID encodes a parsed file location into the Bot API file_id and
// file_unique_id strings. The unique-id derivation lives in file.go.
func encodeFileID(f fileid.FileID) (fileID, uniqueID string) {
fileID, _ = fileid.EncodeFileID(f)
return fileID, fileUniqueID(f)
}
// convertMessageMedia maps an incoming tg media attachment onto the Bot API
// Message media fields. Only the attachment types a bot can receive are mapped;
// web pages and unsupported kinds are ignored.
func convertMessageMedia(media tg.MessageMediaClass, r *Message) {
switch media := media.(type) {
case *tg.MessageMediaPhoto:
if p, ok := media.Photo.(*tg.Photo); ok {
r.Photo = photoSizesFromTg(p)
}
case *tg.MessageMediaDocument:
if d, ok := media.Document.(*tg.Document); ok {
setDocumentMedia(d, r)
}
case *tg.MessageMediaContact:
r.Contact = &Contact{
PhoneNumber: media.PhoneNumber,
FirstName: media.FirstName,
LastName: media.LastName,
UserID: media.UserID,
VCard: media.Vcard,
}
case *tg.MessageMediaPoll:
r.Poll = pollFromTg(&media.Poll, &media.Results)
}
}
// photoSizesFromTg converts a tg.Photo into the Bot API list of photo sizes,
// each carrying a usable file_id.
func photoSizesFromTg(photo *tg.Photo) []PhotoSize {
type sized interface {
GetW() int
GetH() int
GetType() string
}
var out []PhotoSize
for _, sz := range photo.Sizes {
size, ok := sz.(sized)
if !ok {
continue
}
t := size.GetType()
if t == "" {
continue
}
var fileSize int
switch s := sz.(type) {
case *tg.PhotoSize:
fileSize = s.Size
case *tg.PhotoCachedSize:
fileSize = len(s.Bytes)
}
fileID, uniqueID := encodeFileID(fileid.FromPhoto(photo, rune(t[0])))
out = append(out, PhotoSize{
FileID: fileID,
FileUniqueID: uniqueID,
Width: size.GetW(),
Height: size.GetH(),
FileSize: fileSize,
})
}
return out
}
// setDocumentMedia inspects a document's attributes and fills the matching Bot
// API media field (sticker, video, video note, voice, audio, animation, or a
// generic document).
func setDocumentMedia(d *tg.Document, r *Message) {
fileID, uniqueID := encodeFileID(fileid.FromDocument(d))
var (
fileName string
width, height int
duration int
animated, round bool
)
for _, attr := range d.Attributes {
switch a := attr.(type) {
case *tg.DocumentAttributeFilename:
fileName = a.FileName
case *tg.DocumentAttributeImageSize:
width, height = a.W, a.H
case *tg.DocumentAttributeVideo:
width, height, duration, round = a.W, a.H, int(a.Duration), a.RoundMessage
case *tg.DocumentAttributeAnimated:
animated = true
}
}
// The attribute set determines the concrete Bot API type. Order matters:
// stickers, then animations, then video/voice/audio, else a plain document.
for _, attr := range d.Attributes {
switch a := attr.(type) {
case *tg.DocumentAttributeSticker:
r.Sticker = &Sticker{
FileID: fileID,
FileUniqueID: uniqueID,
Type: StickerRegular,
Width: width,
Height: height,
Emoji: a.Alt,
FileSize: int(d.Size),
}
return
case *tg.DocumentAttributeAudio:
if a.Voice {
r.Voice = &Voice{
FileID: fileID,
FileUniqueID: uniqueID,
Duration: a.Duration,
MIMEType: d.MimeType,
FileSize: d.Size,
}
} else {
r.Audio = &Audio{
FileID: fileID,
FileUniqueID: uniqueID,
Duration: a.Duration,
Performer: a.Performer,
Title: a.Title,
FileName: fileName,
MIMEType: d.MimeType,
FileSize: d.Size,
}
}
return
}
}
switch {
case animated:
r.Animation = &Animation{
FileID: fileID,
FileUniqueID: uniqueID,
Width: width,
Height: height,
Duration: duration,
FileName: fileName,
MIMEType: d.MimeType,
FileSize: d.Size,
}
case round:
r.VideoNote = &VideoNote{
FileID: fileID,
FileUniqueID: uniqueID,
Length: width,
Duration: duration,
FileSize: int(d.Size),
}
case width != 0 || height != 0:
r.Video = &Video{
FileID: fileID,
FileUniqueID: uniqueID,
Width: width,
Height: height,
Duration: duration,
FileName: fileName,
MIMEType: d.MimeType,
FileSize: d.Size,
}
default:
r.Document = &Document{
FileID: fileID,
FileUniqueID: uniqueID,
FileName: fileName,
MIMEType: d.MimeType,
FileSize: d.Size,
}
}
}