Skip to content

Commit 759c050

Browse files
committed
msgconv/wa-media: reduce sticker size
1 parent 1cd8d74 commit 759c050

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

pkg/msgconv/wa-media.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ type MediaMessageWithDuration interface {
237237
GetSeconds() uint32
238238
}
239239

240+
const WhatsAppStickerSize = 190
241+
240242
func prepareMediaMessage(rawMsg MediaMessage) *PreparedMedia {
241243
extraInfo := map[string]any{}
242244
data := &PreparedMedia{
@@ -248,6 +250,22 @@ func prepareMediaMessage(rawMsg MediaMessage) *PreparedMedia {
248250
"info": extraInfo,
249251
},
250252
}
253+
if durationMsg, ok := rawMsg.(MediaMessageWithDuration); ok {
254+
data.Info.Duration = int(durationMsg.GetSeconds() * 1000)
255+
}
256+
if dimensionMsg, ok := rawMsg.(MediaMessageWithDimensions); ok {
257+
data.Info.Width = int(dimensionMsg.GetWidth())
258+
data.Info.Height = int(dimensionMsg.GetHeight())
259+
}
260+
if captionMsg, ok := rawMsg.(MediaMessageWithCaption); ok && captionMsg.GetCaption() != "" {
261+
data.Body = captionMsg.GetCaption()
262+
} else {
263+
data.Body = data.FileName
264+
}
265+
data.Info.Size = int(rawMsg.GetFileLength())
266+
data.Info.MimeType = rawMsg.GetMimetype()
267+
data.ContextInfo = rawMsg.GetContextInfo()
268+
251269
switch msg := rawMsg.(type) {
252270
case *waE2E.ImageMessage:
253271
data.MsgType = event.MsgImage
@@ -272,6 +290,16 @@ func prepareMediaMessage(rawMsg MediaMessage) *PreparedMedia {
272290
if msg.GetMimetype() == "application/was" && data.FileName == "sticker" {
273291
data.FileName = "sticker.json"
274292
}
293+
if data.Info.Width == data.Info.Height {
294+
data.Info.Width = WhatsAppStickerSize
295+
data.Info.Height = WhatsAppStickerSize
296+
} else if data.Info.Width > data.Info.Height {
297+
data.Info.Height /= data.Info.Width / WhatsAppStickerSize
298+
data.Info.Width = WhatsAppStickerSize
299+
} else {
300+
data.Info.Width /= data.Info.Height / WhatsAppStickerSize
301+
data.Info.Height = WhatsAppStickerSize
302+
}
275303
case *waE2E.VideoMessage:
276304
data.MsgType = event.MsgVideo
277305
if msg.GetGifPlayback() {
@@ -285,22 +313,7 @@ func prepareMediaMessage(rawMsg MediaMessage) *PreparedMedia {
285313
default:
286314
panic(fmt.Errorf("unknown media message type %T", rawMsg))
287315
}
288-
if durationMsg, ok := rawMsg.(MediaMessageWithDuration); ok {
289-
data.Info.Duration = int(durationMsg.GetSeconds() * 1000)
290-
}
291-
if dimensionMsg, ok := rawMsg.(MediaMessageWithDimensions); ok {
292-
data.Info.Width = int(dimensionMsg.GetWidth())
293-
data.Info.Height = int(dimensionMsg.GetHeight())
294-
}
295-
if captionMsg, ok := rawMsg.(MediaMessageWithCaption); ok && captionMsg.GetCaption() != "" {
296-
data.Body = captionMsg.GetCaption()
297-
} else {
298-
data.Body = data.FileName
299-
}
300316

301-
data.Info.Size = int(rawMsg.GetFileLength())
302-
data.Info.MimeType = rawMsg.GetMimetype()
303-
data.ContextInfo = rawMsg.GetContextInfo()
304317
return data
305318
}
306319

0 commit comments

Comments
 (0)