Skip to content
Snippets Groups Projects
Commit 9ee13765 authored by Travis Ralston's avatar Travis Ralston
Browse files

Run all image decoding through a central place

parent 12596fc7
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,8 @@ jobs:
go-version: '1.20'
- name: "Install libheif"
run: "chmod +x ./.github/workflows/build-libheif.sh && ./.github/workflows/build-libheif.sh"
- name: "Install other dependencies"
run: "sudo apt-get install -y libjpeg-turbo-dev"
- run: './build.sh' # verify the thing compiles
static:
name: 'Go Static (1.20)'
......@@ -23,6 +25,8 @@ jobs:
go-version: '1.20'
- name: "Install libheif"
run: "chmod +x ./.github/workflows/build-libheif.sh && ./.github/workflows/build-libheif.sh"
- name: "Install other dependencies"
run: "sudo apt-get install -y libjpeg-turbo-dev"
- name: "Prepare: compile assets"
run: "GOBIN=$PWD/bin go install -v ./cmd/compile_assets"
- name: "Run: compile assets"
......
......@@ -3,7 +3,7 @@
FROM golang:1.20-alpine AS builder
# Install build dependencies
RUN apk add --no-cache git musl-dev dos2unix build-base libde265-dev libheif-dev
RUN apk add --no-cache git musl-dev dos2unix build-base libde265-dev libheif-dev libjpeg-turbo-dev
WORKDIR /opt
COPY . /opt
......
......@@ -7,7 +7,6 @@ import (
"strings"
"time"
"github.com/disintegration/imaging"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
"github.com/turt2live/matrix-media-repo/api/_apimeta"
......@@ -20,6 +19,7 @@ import (
"github.com/turt2live/matrix-media-repo/thumbnailing"
"github.com/turt2live/matrix-media-repo/thumbnailing/i"
"github.com/turt2live/matrix-media-repo/util"
"github.com/turt2live/matrix-media-repo/util/idec"
)
type mediaInfoHashes struct {
......@@ -115,7 +115,7 @@ func MediaInfo(r *http.Request, rctx rcontext.RequestContext, user _apimeta.User
}
if strings.HasPrefix(response.ContentType, "image/") {
img, err := imaging.Decode(stream)
img, err := idec.Decode(stream)
if err == nil {
response.Width = img.Bounds().Max.X
response.Height = img.Bounds().Max.Y
......
......@@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"image"
"io"
"math"
"net/http"
......@@ -20,6 +19,7 @@ import (
"github.com/turt2live/matrix-media-repo/plugins/plugin_common"
"github.com/turt2live/matrix-media-repo/plugins/plugin_interfaces"
"github.com/turt2live/matrix-media-repo/util"
"github.com/turt2live/matrix-media-repo/util/idec"
)
type AntispamOCR struct {
......@@ -84,7 +84,7 @@ func (a *AntispamOCR) CheckForSpam(b64 string, filename string, contentType stri
return false, nil
}
img, _, err := image.Decode(bytes.NewBuffer(b))
img, err := idec.Decode(bytes.NewBuffer(b))
if err != nil {
return false, err
}
......
......@@ -59,6 +59,7 @@ require (
github.com/t2bot/pgo-fleet/embedded v1.0.1
github.com/testcontainers/testcontainers-go v0.23.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.23.0
github.com/viam-labs/go-libjpeg v0.3.2-0.20230621193101-e6b235025092
golang.org/x/sync v0.3.0
golang.org/x/term v0.12.0
)
......@@ -116,7 +117,6 @@ require (
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/smartystreets/assertions v1.0.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
......
......@@ -375,8 +375,7 @@ github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a/go.mod h1:wozgYq9WEBQBa
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/assertions v1.0.0 h1:UVQPSSmc3qtTi+zPPkCXvZX9VvW/xT/NsRvKfwY81a8=
github.com/smartystreets/assertions v1.0.0/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUrLW/7eUrw0BU5VaoM=
github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
......@@ -408,6 +407,8 @@ github.com/testcontainers/testcontainers-go v0.23.0 h1:ERYTSikX01QczBLPZpqsETTBO
github.com/testcontainers/testcontainers-go v0.23.0/go.mod h1:3gzuZfb7T9qfcH2pHpV4RLlWrPjeWNQah6XlYQ32c4I=
github.com/testcontainers/testcontainers-go/modules/postgres v0.23.0 h1:OEGUC1YTN1RyS4xqsHmlyYkBWm9lMJcswoV4JSHJQOM=
github.com/testcontainers/testcontainers-go/modules/postgres v0.23.0/go.mod h1:YnqIhPwhjqVbJBuvSRJS6pa9Cy1PDRJcrM6T63Uw2ms=
github.com/viam-labs/go-libjpeg v0.3.2-0.20230621193101-e6b235025092 h1:DsILjTWWpYH9jl+AsxqkSdbocxkrHxCwCBgHnYTsnfs=
github.com/viam-labs/go-libjpeg v0.3.2-0.20230621193101-e6b235025092/go.mod h1:BCaeb3/txASzLy3XwzuzqFMs+u0cEf+wdiQQL3TxQVo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
......@@ -419,6 +420,7 @@ go.opentelemetry.io/otel/sdk v1.12.0/go.mod h1:WYcvtgquYvgODEvxOry5owO2y9MyciW7J
go.opentelemetry.io/otel/sdk/metric v0.35.0/go.mod h1:eDyp1GxSiwV98kr7w4pzrszQh/eze9MqBqPd2bCPmyE=
go.opentelemetry.io/otel/trace v1.12.0/go.mod h1:pHlgBynn6s25qJ2szD+Bv+iwKJttjHSI3lUAyf0GNuQ=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.viam.com/test v1.1.0 h1:KXf8crEwt0bHn5iMsjmXg0vPio/yzKGrfd20sWhe/t4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
......
......@@ -8,6 +8,7 @@ import (
"github.com/disintegration/imaging"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/database"
"github.com/turt2live/matrix-media-repo/util/idec"
"github.com/turt2live/matrix-media-repo/util/readers"
)
......@@ -31,7 +32,7 @@ func CalculateBlurhashAsync(ctx rcontext.RequestContext, reader io.Reader, sizeB
// Same goes for pixel size
var c image.Config
br := readers.NewBufferReadsReader(reader)
c, _, err = image.DecodeConfig(br)
c, err = idec.DecodeConfig(br)
if err != nil {
return
}
......@@ -41,7 +42,7 @@ func CalculateBlurhashAsync(ctx rcontext.RequestContext, reader io.Reader, sizeB
reader = br.GetRewoundReader()
var img image.Image
img, err = imaging.Decode(reader)
img, err = idec.Decode(reader)
if err != nil {
ctx.Log.Debug("Skipping blurhash on this upload due to error: ", err)
return
......
......@@ -10,6 +10,7 @@ import (
"github.com/disintegration/imaging"
"github.com/stretchr/testify/assert"
"github.com/turt2live/matrix-media-repo/util/idec"
)
var evenColor = color.RGBA{R: 255, G: 0, B: 0, A: 255}
......@@ -45,7 +46,7 @@ func MakeTestImage(width int, height int) (string, io.Reader, error) {
}
func AssertIsTestImage(t *testing.T, i io.Reader) {
img, _, err := image.Decode(i)
img, err := idec.Decode(i)
assert.NoError(t, err, "Error decoding image")
width := img.Bounds().Max.X
height := img.Bounds().Max.Y
......
......@@ -2,13 +2,13 @@ package i
import (
"errors"
"image"
"io"
_ "github.com/strukturag/libheif/go/heif"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/thumbnailing/m"
"github.com/turt2live/matrix-media-repo/util"
"github.com/turt2live/matrix-media-repo/util/idec"
)
type heifGenerator struct {
......@@ -27,7 +27,7 @@ func (d heifGenerator) matches(img io.Reader, contentType string) bool {
}
func (d heifGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx rcontext.RequestContext) (bool, int, int, error) {
cfg, _, err := image.DecodeConfig(b)
cfg, err := idec.DecodeConfig(b)
if err != nil {
return false, 0, 0, err
}
......@@ -35,7 +35,7 @@ func (d heifGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx
}
func (d heifGenerator) GenerateThumbnail(b io.Reader, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
src, _, err := image.Decode(b)
src, err := idec.Decode(b)
if err != nil {
return nil, errors.New("heif: error decoding thumbnail: " + err.Error())
}
......
......@@ -6,11 +6,11 @@ import (
_ "image/jpeg"
"io"
"github.com/disintegration/imaging"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/thumbnailing/m"
"github.com/turt2live/matrix-media-repo/thumbnailing/u"
"github.com/turt2live/matrix-media-repo/util"
"github.com/turt2live/matrix-media-repo/util/idec"
"github.com/turt2live/matrix-media-repo/util/readers"
)
......@@ -38,7 +38,7 @@ func (d jpgGenerator) GenerateThumbnail(b io.Reader, contentType string, width i
orientation := u.ExtractExifOrientation(br)
b = br.GetRewoundReader()
src, err := imaging.Decode(b)
src, err := idec.Decode(b)
if err != nil {
return nil, errors.New("jpg: error decoding thumbnail: " + err.Error())
}
......
......@@ -19,6 +19,7 @@ import (
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/thumbnailing/m"
"github.com/turt2live/matrix-media-repo/thumbnailing/u"
"github.com/turt2live/matrix-media-repo/util/idec"
"github.com/turt2live/matrix-media-repo/util/readers"
)
......@@ -103,7 +104,7 @@ func (d mp3Generator) GenerateFromStream(audio beep.StreamSeekCloser, format bee
sq := int(math.Round(float64(height) * 0.66))
var artworkImg image.Image
if meta != nil && meta.Picture() != nil {
artwork, _, _ := image.Decode(bytes.NewBuffer(meta.Picture().Data))
artwork, _ := idec.Decode(bytes.NewBuffer(meta.Picture().Data))
if artwork != nil {
artworkImg, _ = u.MakeThumbnail(artwork, "crop", sq, sq)
}
......@@ -127,7 +128,7 @@ func (d mp3Generator) GenerateFromStream(audio beep.StreamSeekCloser, format bee
f, _ := os.OpenFile(path.Join(config.Runtime.AssetsPath, "default-artwork.png"), os.O_RDONLY, 0640)
if f != nil {
defer f.Close()
tmp, _, _ := image.Decode(f)
tmp, _ := idec.Decode(f)
if tmp != nil {
artworkImg, _ = u.MakeThumbnail(tmp, "crop", ax, ay)
}
......
......@@ -6,10 +6,10 @@ import (
_ "image/png"
"io"
"github.com/disintegration/imaging"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/thumbnailing/m"
"github.com/turt2live/matrix-media-repo/thumbnailing/u"
"github.com/turt2live/matrix-media-repo/util/idec"
)
type pngGenerator struct {
......@@ -28,7 +28,7 @@ func (d pngGenerator) matches(img io.Reader, contentType string) bool {
}
func (d pngGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx rcontext.RequestContext) (bool, int, int, error) {
i, _, err := image.DecodeConfig(b)
i, err := idec.DecodeConfig(b)
if err != nil {
return false, 0, 0, err
}
......@@ -36,7 +36,7 @@ func (d pngGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx r
}
func (d pngGenerator) GenerateThumbnail(b io.Reader, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*m.Thumbnail, error) {
src, err := imaging.Decode(b)
src, err := idec.Decode(b)
if err != nil {
return nil, errors.New("png: error decoding thumbnail: " + err.Error())
}
......
package idec
import (
"image"
"io"
"github.com/viam-labs/go-libjpeg/jpeg"
"github.com/disintegration/imaging"
"github.com/turt2live/matrix-media-repo/util/readers"
)
func DecodeConfig(r io.Reader) (image.Config, error) {
br := readers.NewBufferReadsReader(r)
c, err := jpeg.DecodeConfig(br)
b := br.GetRewoundReader()
if err == nil {
return c, nil
}
c, _, err = image.DecodeConfig(b)
return c, err
}
func Decode(r io.Reader) (image.Image, error) {
br := readers.NewBufferReadsReader(r)
_, err := jpeg.DecodeConfig(br)
b := br.GetRewoundReader()
if err == nil {
return jpeg.Decode(b, &jpeg.DecoderOptions{})
}
return imaging.Decode(r)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment