Skip to content

Commit

Permalink
Fix sending media to WhatsApp
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Feb 9, 2024
1 parent 53e112f commit f30bf22
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/zyedidia/clipboard v1.0.4
go.mau.fi/libsignal v0.1.0
go.mau.fi/util v0.3.1-0.20240208085450-32294da153ab
go.mau.fi/whatsmeow v0.0.0-20240208121856-0ef58f1cef30
go.mau.fi/whatsmeow v0.0.0-20240209124521-50c00bd025be
golang.org/x/crypto v0.19.0
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3
golang.org/x/image v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ go.mau.fi/libsignal v0.1.0 h1:vAKI/nJ5tMhdzke4cTK1fb0idJzz1JuEIpmjprueC+c=
go.mau.fi/libsignal v0.1.0/go.mod h1:R8ovrTezxtUNzCQE5PH30StOQWWeBskBsWE55vMfY9I=
go.mau.fi/util v0.3.1-0.20240208085450-32294da153ab h1:XZ8W5vHWlXSGmHn1U+Fvbh+xZr9wuHTvbY+qV7aybDY=
go.mau.fi/util v0.3.1-0.20240208085450-32294da153ab/go.mod h1:rRypwgXVEPILomtFPyQcnbOeuRqf+nRN84vh/CICq4w=
go.mau.fi/whatsmeow v0.0.0-20240208121856-0ef58f1cef30 h1:gwmgU+l0OSww0Me3N022hLdZk7lnEoK8XeaWGlESG1I=
go.mau.fi/whatsmeow v0.0.0-20240208121856-0ef58f1cef30/go.mod h1:lQHbhaG/fI+6hfGqz5Vzn2OBJBEZ05H0kCP6iJXriN4=
go.mau.fi/whatsmeow v0.0.0-20240209124521-50c00bd025be h1:VQKRp4od2zRwu99T9bKeIj+ck+mgevVyJBGU+O2DZUU=
go.mau.fi/whatsmeow v0.0.0-20240209124521-50c00bd025be/go.mod h1:lQHbhaG/fI+6hfGqz5Vzn2OBJBEZ05H0kCP6iJXriN4=
go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto=
go.mau.fi/zeroconfig v0.1.2/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
Expand Down
35 changes: 32 additions & 3 deletions msgconv/to-whatsapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package msgconv

import (
"bytes"
"context"
"fmt"
"image"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -130,6 +132,18 @@ func parseGeoURI(uri string) (lat, long float64, err error) {
return
}

func clampTo400(w, h int) (int, int) {
if w > 400 {
h = h * 400 / w
w = 400
}
if h > 400 {
w = w * 400 / h
h = 400
}
return w, h
}

func (mc *MessageConverter) reuploadMediaToWhatsApp(ctx context.Context, evt *event.Event, content *event.MessageEventContent) (*waMediaTransport.WAMediaTransport, string, error) {
data, mimeType, fileName, err := mc.downloadMatrixMedia(ctx, content)
if err != nil {
Expand Down Expand Up @@ -161,26 +175,41 @@ func (mc *MessageConverter) reuploadMediaToWhatsApp(ctx context.Context, evt *ev
}
customInfo["fi.mau.gif"] = true
}
if content.MsgType == event.MsgImage && content.Info.Width == 0 {
cfg, _, _ := image.DecodeConfig(bytes.NewReader(data))
content.Info.Width, content.Info.Height = cfg.Width, cfg.Height
}
mediaType := msgToMediaType(content.MsgType)
uploaded, err := mc.GetE2EEClient(ctx).Upload(ctx, data, mediaType)
if err != nil {
return nil, "", err
}
w, h := clampTo400(content.Info.Width, content.Info.Height)
if w == 0 && content.MsgType == event.MsgImage {
w, h = 400, 400
}
mediaTransport := &waMediaTransport.WAMediaTransport{
Integral: &waMediaTransport.WAMediaTransport_Integral{
FileSHA256: uploaded.FileSHA256,
MediaKey: uploaded.MediaKey,
FileEncSHA256: uploaded.FileEncSHA256,
DirectPath: uploaded.DirectPath,
MediaKeyTimestamp: time.Now().UnixMilli(),
MediaKeyTimestamp: time.Now().Unix(),
},
Ancillary: &waMediaTransport.WAMediaTransport_Ancillary{
FileLength: uint64(len(data)),
Mimetype: mimeType,
Thumbnail: nil,
ObjectID: uploaded.ObjectID,
// This field is extremely required for some reason.
// Messenger iOS & Android will refuse to display the media if it's not present.
// iOS also requires that width and height are non-empty.
Thumbnail: &waMediaTransport.WAMediaTransport_Ancillary_Thumbnail{
ThumbnailWidth: uint32(w),
ThumbnailHeight: uint32(h),
},
ObjectID: uploaded.ObjectID,
},
}
fmt.Printf("Uploaded media transport: %+v\n", mediaTransport)
return mediaTransport, fileName, nil
}

Expand Down

0 comments on commit f30bf22

Please sign in to comment.