diff --git a/zipserver/music_analyzer.go b/zipserver/music_analyzer.go index 0f5d461..3c182ce 100644 --- a/zipserver/music_analyzer.go +++ b/zipserver/music_analyzer.go @@ -6,7 +6,6 @@ import ( "io" "github.com/dhowden/tag" - "github.com/go-errors/errors" ) // MusicAnalyzer uses rules according to music albums. @@ -15,7 +14,7 @@ import ( type MusicAnalyzer struct{} func (m MusicAnalyzer) Analyze(r io.Reader, key string) (AnalyzeResult, error) { - res := AnalyzeResult{Key: key} + res := AnalyzeResult{} // TODO: The music tag library requires a ReadSeeker (understandably), // which a Reader does not satisfy. Here is a naive implementation that @@ -25,13 +24,17 @@ func (m MusicAnalyzer) Analyze(r io.Reader, key string) (AnalyzeResult, error) { b, err := io.ReadAll(r) if err != nil { - return res, errors.Wrap(err, 0) + return res, fmt.Errorf("read bytes: %w", err) } rs := bytes.NewReader(b) md, err := tag.ReadFrom(rs) if err != nil { - return res, errors.Wrap(err, 0) + // Tag parser expects at least 11 bytes. + if err == io.ErrUnexpectedEOF { + return res, fmt.Errorf("%w: file too short", ErrSkipped) + } + return res, fmt.Errorf("new tag reader: %w", err) } // Package tag provides these already, but let's set them explicitly @@ -71,6 +74,7 @@ func (m MusicAnalyzer) Analyze(r io.Reader, key string) (AnalyzeResult, error) { Lyrics: md.Lyrics(), Comment: md.Comment(), } + res.Key = key return res, nil } diff --git a/zipserver/music_analyzer_test.go b/zipserver/music_analyzer_test.go new file mode 100644 index 0000000..81c880d --- /dev/null +++ b/zipserver/music_analyzer_test.go @@ -0,0 +1,97 @@ +package zipserver + +import ( + "bytes" + "embed" + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +//go:embed testdata/music/* +var testAudioFiles embed.FS + +func TestMusicAnalyzer(t *testing.T) { + testCases := []struct { + name string + err error + res AnalyzeResult + }{ + { + name: "tone.thisisflac", + res: AnalyzeResult{ + Key: "key", + ContentType: "audio/flac", + Metadata: TrackInfo{ + FileType: "FLAC", + Title: "Long drone", + Album: "Fall asleep", + Artist: "Smith", + AlbumArtist: "Smith", + Composer: "Smith", + Year: 2005, + Track: 4, + TrackTotal: 6, + Disc: 2, + DiscTotal: 2, + }, + }, + }, + { + name: "tone.thisisopus", + res: AnalyzeResult{ + Key: "key", + ContentType: "audio/ogg", + Metadata: TrackInfo{ + FileType: "Ogg", + Title: "Halloween song 2", + Album: "Don't listen to any of these songs", + Artist: "Creepy Weirdo", + Genre: "horror", + Year: 2022, + Track: 12, + TrackTotal: 666, + Lyrics: "pumpkins yeah\\ndecorate your house", + Comment: "spooky tune", + }, + }, + }, + { + name: "tone.thisismp3", + res: AnalyzeResult{ + Key: "key", + ContentType: "audio/mpeg", + Metadata: TrackInfo{ + FileType: "MP3", + Title: "Boring", + Album: "Wat?", + Artist: "Small Dude", + Genre: "Acoustic", + Year: 1994, + Track: 2, + TrackTotal: 10, + Comment: "wrote this when I was bored", + }, + }, + }, + { + name: "garbage.dat", + err: ErrSkipped, + }, + } + + var analyzer MusicAnalyzer + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + b, err := testAudioFiles.ReadFile("testdata/music/" + tc.name) + require.NoError(t, err) + buf := bytes.NewBuffer(b) + res, err := analyzer.Analyze(buf, "key") + require.Truef(t, errors.Is(err, tc.err), "error %q does not wrap %q", err, tc.err) + assert.Equal(t, tc.res, res) + }) + } +} diff --git a/zipserver/testdata/music/README.txt b/zipserver/testdata/music/README.txt new file mode 100644 index 0000000..91b0194 --- /dev/null +++ b/zipserver/testdata/music/README.txt @@ -0,0 +1 @@ +Data files are renamed to test content detection. diff --git a/zipserver/testdata/music/garbage.dat b/zipserver/testdata/music/garbage.dat new file mode 100644 index 0000000..02e5641 --- /dev/null +++ b/zipserver/testdata/music/garbage.dat @@ -0,0 +1 @@ +þýDˆÌ` \ No newline at end of file diff --git a/zipserver/testdata/music/tone.thisisflac b/zipserver/testdata/music/tone.thisisflac new file mode 100644 index 0000000..212aad6 Binary files /dev/null and b/zipserver/testdata/music/tone.thisisflac differ diff --git a/zipserver/testdata/music/tone.thisismp3 b/zipserver/testdata/music/tone.thisismp3 new file mode 100644 index 0000000..8c83599 Binary files /dev/null and b/zipserver/testdata/music/tone.thisismp3 differ diff --git a/zipserver/testdata/music/tone.thisisopus b/zipserver/testdata/music/tone.thisisopus new file mode 100644 index 0000000..cbc729b Binary files /dev/null and b/zipserver/testdata/music/tone.thisisopus differ