From 848e3bf155fbe7995fa88b02b43d2eda3fd2da97 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Sat, 2 Sep 2023 11:35:06 +0200 Subject: [PATCH] Fix error if tags contain null byte(s) (#471) 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. --- app/models/rescan_runner.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/models/rescan_runner.rb b/app/models/rescan_runner.rb index c20a05ec..dee5160c 100644 --- a/app/models/rescan_runner.rb +++ b/app/models/rescan_runner.rb @@ -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 @@ -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