Skip to content

Commit

Permalink
Support sending heic images
Browse files Browse the repository at this point in the history
Implements feature request #635 by converting heic files
to jpeg before attaching to a message
  • Loading branch information
Raphael Castaneda committed Jul 23, 2023
1 parent 5c8c736 commit 445e58f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
21 changes: 21 additions & 0 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 445e58f

Please sign in to comment.