Skip to content

Commit

Permalink
Fix error if tags contain null byte(s) (#471)
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 authored Sep 2, 2023
1 parent f0c5c56 commit 848e3bf
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&.then { |s| clean_string(s) }
t_albumartist = tag.albumartist.present? ? tag.albumartist&.then { |s| clean_string(s) } : t_artist
t_composer = tag.composer&.then { |s| clean_string(s) }
t_title = tag.title&.then { |s| clean_string(s) }
t_number = tag.track || 0
t_album = tag.album&.unicode_normalize
t_album = tag.album&.then { |s| clean_string(s) }
t_year = convert_year(tag.year)
t_genre = tag.genre&.unicode_normalize
t_genre = tag.genre&.then { |s| clean_string(s) }
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, so we simply remove null bytes instead of
# trying to parse them. Otherwise our database will throw an error,
# since null bytes aren't allowed in strings.
s.delete("\000").unicode_normalize
end
end

0 comments on commit 848e3bf

Please sign in to comment.