Skip to content

Commit

Permalink
move thumbnailing/u to thumbnailing/preview/metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mpldr committed Mar 3, 2024
1 parent b946e88 commit 07ca3f2
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 45 deletions.
4 changes: 2 additions & 2 deletions thumbnailing/preview/apng.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/getsentry/sentry-go"
"github.com/kettek/apng"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
"github.com/t2bot/matrix-media-repo/thumbnailing/preview/metadata"
)

type apngGenerator struct{}
Expand Down Expand Up @@ -68,7 +68,7 @@ func (d apngGenerator) GenerateThumbnail(b io.Reader, contentType string, width
draw.Draw(frameImg, image.Rect(frame.XOffset, frame.YOffset, frameImg.Rect.Max.X, frameImg.Rect.Max.Y), img, image.Point{X: 0, Y: 0}, draw.Src)

// Do the thumbnailing on the copied frame
frameThumb, err := u.MakeThumbnail(frameImg, method, width, height)
frameThumb, err := metadata.MakeThumbnail(frameImg, method, width, height)
if err != nil {
return nil, fmt.Errorf("apng: error generating thumbnail frame: %w", err)
}
Expand Down
12 changes: 7 additions & 5 deletions thumbnailing/preview/flac.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"io"

"github.com/dhowden/tag"
"github.com/faiface/beep"
"github.com/faiface/beep/flac"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
)

type flacGenerator struct{}
Expand Down Expand Up @@ -37,14 +37,16 @@ func (d flacGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx
}

func (d flacGenerator) GenerateThumbnail(r io.Reader, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*Thumbnail, error) {
tags, rc, err := u.GetID3Tags(r)
rd, err := newReadSeekerWrapper(r)
if err != nil {
return nil, fmt.Errorf("error wrapping reader: %w", err)
}
tags, err := tag.ReadFrom(rd)
if err != nil {
return nil, fmt.Errorf("flac: error getting tags: %w", err)
}
//goland:noinspection GoUnhandledErrorResult
defer rc.Close()

audio, format, err := d.decode(rc)
audio, format, err := d.decode(rd)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions thumbnailing/preview/gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"math"

"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
"github.com/t2bot/matrix-media-repo/thumbnailing/preview/metadata"
)

type gifGenerator struct{}
Expand Down Expand Up @@ -54,7 +54,7 @@ func (d gifGenerator) GenerateThumbnail(b io.Reader, contentType string, width i
draw.Draw(frameImg, frameImg.Bounds(), img, image.Point{X: 0, Y: 0}, draw.Over)

// Do the thumbnailing on the copied frame
frameThumb, err := u.MakeThumbnail(frameImg, method, width, height)
frameThumb, err := metadata.MakeThumbnail(frameImg, method, width, height)
if err != nil {
return nil, fmt.Errorf("gif: error generating thumbnail frame: %w", err)
}
Expand All @@ -76,7 +76,7 @@ func (d gifGenerator) GenerateThumbnail(b io.Reader, contentType string, width i
// The thumbnailer decided that it shouldn't thumbnail, so encode it ourselves
pr, pw := io.Pipe()
go func(pw *io.PipeWriter, p *image.Paletted) {
err = u.Encode(ctx, pw, p)
err = metadata.Encode(ctx, pw, p)
if err != nil {
_ = pw.CloseWithError(fmt.Errorf("gif: error encoding still frame thumbnail: %w", err))
} else {
Expand Down
10 changes: 5 additions & 5 deletions thumbnailing/preview/jpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/disintegration/imaging"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
"github.com/t2bot/matrix-media-repo/thumbnailing/preview/metadata"
"github.com/t2bot/matrix-media-repo/util/readers"
)

Expand All @@ -33,24 +33,24 @@ func (d jpgGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx r

func (d jpgGenerator) GenerateThumbnail(b io.Reader, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*Thumbnail, error) {
br := readers.NewBufferReadsReader(b)
orientation := u.ExtractExifOrientation(br)
orientation := metadata.ExtractExifOrientation(br)
b = br.GetRewoundReader()

src, err := imaging.Decode(b)
if err != nil {
return nil, fmt.Errorf("jpg: error decoding thumbnail: %w", err)
}

thumb, err := u.MakeThumbnail(src, method, width, height)
thumb, err := metadata.MakeThumbnail(src, method, width, height)
if err != nil {
return nil, fmt.Errorf("jpg: error making thumbnail: %w", err)
}

thumb = u.ApplyOrientation(thumb, orientation)
thumb = metadata.ApplyOrientation(thumb, orientation)

pr, pw := io.Pipe()
go func(pw *io.PipeWriter, p image.Image) {
err = u.Encode(ctx, pw, p, u.JpegSource)
err = metadata.Encode(ctx, pw, p, metadata.JpegSource)
if err != nil {
_ = pw.CloseWithError(fmt.Errorf("jpg: error encoding thumbnail: %w", err))
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package u
package metadata

func AdjustProperties(srcWidth int, srcHeight int, desiredWidth int, desiredHeight int, wantAnimated bool, method string) (bool, int, int, string) {
aspectRatio := float32(srcHeight) / float32(srcWidth)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package u
package metadata

import (
"image"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package u
package metadata

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package u
package metadata

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package u
package metadata

import (
"errors"
Expand Down
16 changes: 6 additions & 10 deletions thumbnailing/preview/mp3.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/t2bot/matrix-media-repo/common/config"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
"github.com/t2bot/matrix-media-repo/thumbnailing/preview/metadata"
"github.com/t2bot/matrix-media-repo/util/readers"
)

Expand Down Expand Up @@ -49,11 +49,7 @@ func (d mp3Generator) GetOriginDimensions(b io.Reader, contentType string, ctx r

func (d mp3Generator) GenerateThumbnail(b io.Reader, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*Thumbnail, error) {
rd, err := newReadSeekerWrapper(b)
tags, err := u.GetID3Tags(rd)
if err != nil {
return nil, fmt.Errorf("error wrapping reader: %w", err)
}
tags, err = tag.ReadFrom(rd) // we don't care about errors in this process
tags, err := tag.ReadFrom(rd) // we don't care about errors in this process
if err != nil {
return nil, fmt.Errorf("mp3: error getting tags: %w", err)
}
Expand Down Expand Up @@ -81,7 +77,7 @@ func (d mp3Generator) GetAudioData(b io.Reader, nKeys int, ctx rcontext.RequestC

func (d mp3Generator) GetDataFromStream(audio beep.StreamSeekCloser, format beep.Format, nKeys int) (*AudioInfo, error) {
totalSamples := audio.Len()
downsampled, err := u.FastSampleAudio(audio, nKeys)
downsampled, err := metadata.FastSampleAudio(audio, nKeys)
if err != nil {
return nil, err
}
Expand All @@ -106,7 +102,7 @@ func (d mp3Generator) GenerateFromStream(audio beep.StreamSeekCloser, format bee
if meta != nil && meta.Picture() != nil {
artwork, _, _ := image.Decode(bytes.NewBuffer(meta.Picture().Data))
if artwork != nil {
artworkImg, _ = u.MakeThumbnail(artwork, "crop", sq, sq)
artworkImg, _ = metadata.MakeThumbnail(artwork, "crop", sq, sq)
}
}

Expand All @@ -130,7 +126,7 @@ func (d mp3Generator) GenerateFromStream(audio beep.StreamSeekCloser, format bee
defer f.Close()
tmp, _, _ := image.Decode(f)
if tmp != nil {
artworkImg, _ = u.MakeThumbnail(tmp, "crop", ax, ay)
artworkImg, _ = metadata.MakeThumbnail(tmp, "crop", ax, ay)
}
}
if artworkImg == nil {
Expand Down Expand Up @@ -199,7 +195,7 @@ func (d mp3Generator) GenerateFromStream(audio beep.StreamSeekCloser, format bee
// Encode to a png
pr, pw := io.Pipe()
go func(pw *io.PipeWriter, p image.Image) {
err = u.Encode(ctx, pw, p)
err = metadata.Encode(ctx, pw, p)
if err != nil {
_ = pw.CloseWithError(fmt.Errorf("beep-visual: error encoding thumbnail: %w", err))
} else {
Expand Down
9 changes: 4 additions & 5 deletions thumbnailing/preview/ogg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"io"

"github.com/dhowden/tag"
"github.com/faiface/beep"
"github.com/faiface/beep/vorbis"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
"github.com/t2bot/matrix-media-repo/util/readers"
)

Expand Down Expand Up @@ -38,14 +38,13 @@ func (d oggGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx r
}

func (d oggGenerator) GenerateThumbnail(b io.Reader, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*Thumbnail, error) {
tags, rc, err := u.GetID3Tags(b)
rd, err := newReadSeekerWrapper(b)
tags, err := tag.ReadFrom(rd) // we don't care about errors in this process
if err != nil {
return nil, fmt.Errorf("ogg: error getting tags: %v", err)
}
//goland:noinspection GoUnhandledErrorResult
defer rc.Close()

audio, format, err := d.decode(rc)
audio, format, err := d.decode(rd)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions thumbnailing/preview/png.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/disintegration/imaging"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
"github.com/t2bot/matrix-media-repo/thumbnailing/preview/metadata"
)

type pngGenerator struct{}
Expand Down Expand Up @@ -43,14 +43,14 @@ func (d pngGenerator) GenerateThumbnail(b io.Reader, contentType string, width i
}

func (d pngGenerator) GenerateThumbnailOf(src image.Image, width int, height int, method string, ctx rcontext.RequestContext) (*Thumbnail, error) {
thumb, err := u.MakeThumbnail(src, method, width, height)
thumb, err := metadata.MakeThumbnail(src, method, width, height)
if err != nil || thumb == nil {
return nil, err
}

pr, pw := io.Pipe()
go func(pw *io.PipeWriter, p image.Image) {
err = u.Encode(ctx, pw, p)
err = metadata.Encode(ctx, pw, p)
if err != nil {
_ = pw.CloseWithError(fmt.Errorf("png: error encoding thumbnail: %w", err))
} else {
Expand Down
9 changes: 4 additions & 5 deletions thumbnailing/preview/wav.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"fmt"
"io"

"github.com/dhowden/tag"
"github.com/faiface/beep"
"github.com/faiface/beep/wav"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
)

type wavGenerator struct{}
Expand Down Expand Up @@ -37,14 +37,13 @@ func (d wavGenerator) GetOriginDimensions(b io.Reader, contentType string, ctx r
}

func (d wavGenerator) GenerateThumbnail(b io.Reader, contentType string, width int, height int, method string, animated bool, ctx rcontext.RequestContext) (*Thumbnail, error) {
tags, rc, err := u.GetID3Tags(b)
rd, err := newReadSeekerWrapper(b)
tags, err := tag.ReadFrom(rd) // we don't care about errors in this process
if err != nil {
return nil, fmt.Errorf("wav: error getting tags: %w", err)
}
//goland:noinspection GoUnhandledErrorResult
defer rc.Close()

audio, format, err := d.decode(rc)
audio, format, err := d.decode(rd)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions thumbnailing/thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/t2bot/matrix-media-repo/common"
"github.com/t2bot/matrix-media-repo/common/rcontext"
"github.com/t2bot/matrix-media-repo/thumbnailing/preview"
"github.com/t2bot/matrix-media-repo/thumbnailing/u"
"github.com/t2bot/matrix-media-repo/thumbnailing/preview/metadata"
"github.com/t2bot/matrix-media-repo/util/readers"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func GenerateThumbnail(imgStream io.ReadCloser, contentType string, width int, h

// While we're here, check to ensure we're not about to produce a thumbnail which is larger than the source material
shouldThumbnail := true
shouldThumbnail, width, height, method = u.AdjustProperties(w, h, width, height, animated, method)
shouldThumbnail, width, height, method = metadata.AdjustProperties(w, h, width, height, animated, method)
if !shouldThumbnail {
return nil, common.ErrMediaDimensionsTooSmall
}
Expand Down

0 comments on commit 07ca3f2

Please sign in to comment.