diff --git a/thumbnailing/preview/apng.go b/thumbnailing/preview/apng.go index be62ce3f..ea27095c 100644 --- a/thumbnailing/preview/apng.go +++ b/thumbnailing/preview/apng.go @@ -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{} @@ -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) } diff --git a/thumbnailing/preview/flac.go b/thumbnailing/preview/flac.go index edd6cf08..a62254c0 100644 --- a/thumbnailing/preview/flac.go +++ b/thumbnailing/preview/flac.go @@ -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{} @@ -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 } diff --git a/thumbnailing/preview/gif.go b/thumbnailing/preview/gif.go index 97e1ee93..b9c92fba 100644 --- a/thumbnailing/preview/gif.go +++ b/thumbnailing/preview/gif.go @@ -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{} @@ -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) } @@ -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 { diff --git a/thumbnailing/preview/jpg.go b/thumbnailing/preview/jpg.go index 5baa38af..5f4bc113 100644 --- a/thumbnailing/preview/jpg.go +++ b/thumbnailing/preview/jpg.go @@ -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" ) @@ -33,7 +33,7 @@ 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) @@ -41,16 +41,16 @@ func (d jpgGenerator) GenerateThumbnail(b io.Reader, contentType string, width i 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 { diff --git a/thumbnailing/u/dimensions.go b/thumbnailing/preview/metadata/dimensions.go similarity index 97% rename from thumbnailing/u/dimensions.go rename to thumbnailing/preview/metadata/dimensions.go index 2b19a40a..dd8a06a3 100644 --- a/thumbnailing/u/dimensions.go +++ b/thumbnailing/preview/metadata/dimensions.go @@ -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) diff --git a/thumbnailing/u/encode.go b/thumbnailing/preview/metadata/encode.go similarity index 97% rename from thumbnailing/u/encode.go rename to thumbnailing/preview/metadata/encode.go index 9effd8e3..bb339226 100644 --- a/thumbnailing/u/encode.go +++ b/thumbnailing/preview/metadata/encode.go @@ -1,4 +1,4 @@ -package u +package metadata import ( "image" diff --git a/thumbnailing/u/exif.go b/thumbnailing/preview/metadata/exif.go similarity index 99% rename from thumbnailing/u/exif.go rename to thumbnailing/preview/metadata/exif.go index 5123911c..65756264 100644 --- a/thumbnailing/u/exif.go +++ b/thumbnailing/preview/metadata/exif.go @@ -1,4 +1,4 @@ -package u +package metadata import ( "errors" diff --git a/thumbnailing/u/framing.go b/thumbnailing/preview/metadata/framing.go similarity index 98% rename from thumbnailing/u/framing.go rename to thumbnailing/preview/metadata/framing.go index 18b11127..43ba044c 100644 --- a/thumbnailing/u/framing.go +++ b/thumbnailing/preview/metadata/framing.go @@ -1,4 +1,4 @@ -package u +package metadata import ( "errors" diff --git a/thumbnailing/u/sample.go b/thumbnailing/preview/metadata/sample.go similarity index 98% rename from thumbnailing/u/sample.go rename to thumbnailing/preview/metadata/sample.go index c6854575..75fcc449 100644 --- a/thumbnailing/u/sample.go +++ b/thumbnailing/preview/metadata/sample.go @@ -1,4 +1,4 @@ -package u +package metadata import ( "errors" diff --git a/thumbnailing/preview/mp3.go b/thumbnailing/preview/mp3.go index fc9e0cdd..b1ed1dd6 100644 --- a/thumbnailing/preview/mp3.go +++ b/thumbnailing/preview/mp3.go @@ -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" ) @@ -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) } @@ -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 } @@ -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) } } @@ -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 { @@ -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 { diff --git a/thumbnailing/preview/ogg.go b/thumbnailing/preview/ogg.go index 9d144046..b8b22b73 100644 --- a/thumbnailing/preview/ogg.go +++ b/thumbnailing/preview/ogg.go @@ -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" ) @@ -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 } diff --git a/thumbnailing/preview/png.go b/thumbnailing/preview/png.go index b9ba0a93..9835afc7 100644 --- a/thumbnailing/preview/png.go +++ b/thumbnailing/preview/png.go @@ -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{} @@ -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 { diff --git a/thumbnailing/preview/wav.go b/thumbnailing/preview/wav.go index 15c34739..5f1cdff8 100644 --- a/thumbnailing/preview/wav.go +++ b/thumbnailing/preview/wav.go @@ -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{} @@ -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 } diff --git a/thumbnailing/thumbnail.go b/thumbnailing/thumbnail.go index 82118e6e..15639236 100644 --- a/thumbnailing/thumbnail.go +++ b/thumbnailing/thumbnail.go @@ -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" ) @@ -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 }