Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sending heic images #636

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 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,21 @@ 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 +3523,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