Skip to content

Commit

Permalink
converts webP to png using go library
Browse files Browse the repository at this point in the history
-uses `golang.org/x/image/webp` to convert webP to PNG in memory
- adds modules to `go.mod` and `go.sum`
  • Loading branch information
apmechev committed Sep 18, 2021
1 parent 5fb26e6 commit bcebcb7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 37 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.8
github.com/prometheus/client_golang v1.11.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
gopkg.in/yaml.v2 v2.4.0
maunium.net/go/mauflag v1.0.0
maunium.net/go/maulogger/v2 v2.3.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf h1:B2n+Zi5QeYRDAEodEu72OS36gmTWjgpXr2+cWcBW90o=
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -184,6 +186,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
48 changes: 11 additions & 37 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"image/gif"
"image/jpeg"
"image/png"
"golang.org/x/image/webp"
"io/ioutil"
"math"
"math/rand"
Expand Down Expand Up @@ -1889,47 +1890,20 @@ func (portal *Portal) downloadThumbnail(content *event.MessageEventContent, id i
return buf.Bytes()
}

func (portal *Portal) convertWebPtoPng(webp []byte) ([]byte, error) {
dir, err := ioutil.TempDir("", "webp-convert-*")
if err != nil {
return nil, fmt.Errorf("failed to make temp dir: %w", err)
func (portal *Portal) convertWebPtoPng(webp_image []byte) ([]byte, error) {
webp_decoded, err := webp.Decode(bytes.NewReader(webp_image))
if err != nil {
return nil, fmt.Errorf("failed to decode webp image: %w", err)
}
defer os.RemoveAll(dir)

inputFile, err := os.OpenFile(filepath.Join(dir, "input.webp"), os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)
if err != nil {
return nil, fmt.Errorf("failed open input file: %w", err)
var png_buffer bytes.Buffer;
if err := png.Encode(&png_buffer, webp_decoded); err != nil {
return nil, fmt.Errorf("failed to encode png image: %w", err)
}
_, err = inputFile.Write(webp)
if err != nil {
_ = inputFile.Close()
return nil, fmt.Errorf("failed to write gif to input file: %w", err)
}
_ = inputFile.Close()

outputFileName := filepath.Join(dir, "output.png")
cmd := exec.Command("dwebp", inputFile.Name(), "-o", outputFileName)
vcLog := portal.log.Sub("WebPconverter").Writer(log.LevelWarn)
cmd.Stdout = vcLog
cmd.Stderr = vcLog

err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("failed to run dwebp: %w", err)
}
outputFile, err := os.OpenFile(filepath.Join(dir, "output.png"), os.O_RDONLY, 0)
if err != nil {
return nil, fmt.Errorf("failed to open output file: %w", err)
}
defer func() {
_ = outputFile.Close()
_ = os.Remove(outputFile.Name())
}()
png, err := ioutil.ReadAll(outputFile)
if err != nil {
return nil, fmt.Errorf("failed to read png from output file: %w", err)
}
return png, nil
var png_file = png_buffer.Bytes()
portal.log.Debugf("Converted png file of size %f", len(png_file))
return png_file, nil
}

func (portal *Portal) convertGifToVideo(gif []byte) ([]byte, error) {
Expand Down

0 comments on commit bcebcb7

Please sign in to comment.