From 80118c5ababf5265aae5e41b19eb5899cdc79700 Mon Sep 17 00:00:00 2001 From: raphaelcastaneda Date: Sat, 22 Jul 2023 17:18:46 -0700 Subject: [PATCH 1/2] Support sending heic images Implements feature request mautrix/whatsapp#635 by converting heic files to jpeg before attaching to a message --- go.mod | 1 + go.sum | 2 ++ portal.go | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/go.mod b/go.mod index 5cddd371..cbacadc6 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect github.com/golang/protobuf v1.5.3 // indirect + github.com/jdeng/goheif v0.0.0-20200323230657-a0d6a8b3e68f // indirect github.com/kr/text v0.2.0 // indirect github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect diff --git a/go.sum b/go.sum index ca089ef2..0f9aaad2 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/jdeng/goheif v0.0.0-20200323230657-a0d6a8b3e68f h1:jYkcRYsnnvPF07yn4XJx3k8duM4KDw3QYB3p8bUrk80= +github.com/jdeng/goheif v0.0.0-20200323230657-a0d6a8b3e68f/go.mod h1:G7IyA3/eR9IFmUIPdyP3c0l4ZaqEvXAk876WfaQ8plc= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= diff --git a/portal.go b/portal.go index 628ae761..36ca1846 100644 --- a/portal.go +++ b/portal.go @@ -42,6 +42,7 @@ import ( "time" "github.com/chai2010/webp" + "github.com/jdeng/goheif" "github.com/rs/zerolog" "github.com/tidwall/gjson" "golang.org/x/exp/slices" @@ -3377,6 +3378,23 @@ func (portal *Portal) downloadThumbnail(ctx context.Context, original []byte, th return createThumbnail(original, png) } +func (portal *Portal) convertHEICtoJPEG(heicImage []byte) ([]byte, error) { + + + heicEncoded, err := goheif.Decode(bytes.NewReader(heicImage)) + if err != nil { + return nil, fmt.Errorf("failed to decode heic image: %w", err) + } + + var jpgBuffer bytes.Buffer + + if err = jpeg.Encode(&jpgBuffer, heicEncoded, nil); err != nil { + return nil, fmt.Errorf("failed to encode jpeg image: %w", err) + } + + return jpgBuffer.Bytes(), nil +} + func (portal *Portal) convertWebPtoPNG(webpImage []byte) ([]byte, error) { webpDecoded, err := webp.Decode(bytes.NewReader(webpImage)) if err != nil { @@ -3507,6 +3525,9 @@ func (portal *Portal) preprocessMatrixMedia(ctx context.Context, sender *User, r case "image/webp": data, convertErr = portal.convertWebPtoPNG(data) content.Info.MimeType = "image/png" + case "image/heic": + data, convertErr = portal.convertHEICtoJPEG(data) + content.Info.MimeType = "image/jpeg" default: return nil, fmt.Errorf("%w %q in image message", errMediaUnsupportedType, mimeType) } From d916fabf0cc2978d8874dde6a0473906a89b96ee Mon Sep 17 00:00:00 2001 From: raphaelcastaneda Date: Sun, 23 Jul 2023 01:55:38 -0700 Subject: [PATCH 2/2] cleanup whitespace --- portal.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/portal.go b/portal.go index 36ca1846..979b7424 100644 --- a/portal.go +++ b/portal.go @@ -3379,8 +3379,6 @@ func (portal *Portal) downloadThumbnail(ctx context.Context, original []byte, th } func (portal *Portal) convertHEICtoJPEG(heicImage []byte) ([]byte, error) { - - heicEncoded, err := goheif.Decode(bytes.NewReader(heicImage)) if err != nil { return nil, fmt.Errorf("failed to decode heic image: %w", err)