Skip to content

Commit

Permalink
inline GetID3Tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mpldr committed Mar 2, 2024
1 parent 6b3a125 commit b946e88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 56 deletions.
11 changes: 7 additions & 4 deletions thumbnailing/preview/mp3.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ 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) {
tags, rc, err := u.GetID3Tags(b)
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
if err != nil {
return nil, fmt.Errorf("mp3: 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
42 changes: 42 additions & 0 deletions thumbnailing/preview/readSeeker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package preview

import "io"

type readSeekerWrapper struct {
data []byte
offset int64
}

func (r *readSeekerWrapper) Read(p []byte) (n int, err error) {
if r.offset >= int64(len(r.data)) {
return 0, io.EOF
}
n = copy(p, r.data[r.offset:])
r.offset += int64(n)
return
}

func (r *readSeekerWrapper) Seek(offset int64, whence int) (int64, error) {
var absOffset int64
switch whence {
case io.SeekStart:
absOffset = offset
case io.SeekCurrent:
absOffset = r.offset + offset
case io.SeekEnd:
absOffset = int64(len(r.data)) + offset
}
if absOffset < 0 || absOffset > int64(len(r.data)) {
return 0, io.EOF
}
r.offset = absOffset
return absOffset, nil
}

func newReadSeekerWrapper(reader io.Reader) (*readSeekerWrapper, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
return &readSeekerWrapper{data: data}, nil
}
52 changes: 0 additions & 52 deletions thumbnailing/u/metadata.go

This file was deleted.

0 comments on commit b946e88

Please sign in to comment.