Skip to content

Commit

Permalink
Fix error if tags contain null byte(s)
Browse files Browse the repository at this point in the history
No test, because I couldn't figure out how to create a file with such tags. I
did test locally that the problem files I had scanned without issue.
  • Loading branch information
chvp committed Sep 2, 2023
1 parent f0c5c56 commit 6618f2f
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions app/models/rescan_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ def process_file(codec, path)
return if AudioFile.exists?(location:, filename: relative_path.to_s)

tag = WahWah.open(path)
t_artist = tag.artist&.unicode_normalize
t_albumartist = tag.albumartist.present? ? tag.albumartist&.unicode_normalize : t_artist
t_composer = tag.composer&.unicode_normalize
t_title = tag.title&.unicode_normalize
t_artist = tag.artist&.yield_self(&:clean_string)
t_albumartist = tag.albumartist.present? ? tag.albumartist&.yield_self(&:clean_string) : t_artist
t_composer = tag.composer&.yield_self(&:clean_string)
t_title = tag.title&.yield_self(&:clean_string)

Check warning on line 92 in app/models/rescan_runner.rb

View check run for this annotation

Codecov / codecov/patch

app/models/rescan_runner.rb#L92

Added line #L92 was not covered by tests
t_number = tag.track || 0
t_album = tag.album&.unicode_normalize
t_album = tag.album&.yield_self(&:clean_string)

Check warning on line 94 in app/models/rescan_runner.rb

View check run for this annotation

Codecov / codecov/patch

app/models/rescan_runner.rb#L94

Added line #L94 was not covered by tests
t_year = convert_year(tag.year)
t_genre = tag.genre&.unicode_normalize
t_genre = tag.genre&.yield_self(&:clean_string)

Check warning on line 96 in app/models/rescan_runner.rb

View check run for this annotation

Codecov / codecov/patch

app/models/rescan_runner.rb#L96

Added line #L96 was not covered by tests
length = tag.duration
bitrate = tag.bitrate || 0
sample_rate = tag.sample_rate || 0
Expand Down Expand Up @@ -204,4 +204,12 @@ def convert_year(tag)
rescue Date::Error
Date.new(0)
end

def clean_string(s)
# ID3v2 tags sometimes include null bytes to "split" fields. It doesn't
# occur very often though, so remove null bytes instead of trying to parse
# that. Database errors out on null bytes, so we can't just do nothing.
s.delete("\000").unicode_normalize

Check warning on line 212 in app/models/rescan_runner.rb

View check run for this annotation

Codecov / codecov/patch

app/models/rescan_runner.rb#L212

Added line #L212 was not covered by tests
end

end

0 comments on commit 6618f2f

Please sign in to comment.