Skip to content

Commit

Permalink
allow file_io to be closed automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Jul 27, 2020
1 parent 93232df commit 16579fc
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 42 deletions.
17 changes: 15 additions & 2 deletions lib/wahwah/lazy_read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,28 @@ def initialize(file_io, *arg)
@file_io = file_io
super(*arg)
@position = @file_io.pos
@data = get_data if @file_io.is_a?(StringIO)
end

def data
@file_io.seek(@position)
@file_io.read(size)
if @file_io.closed? && @file_io.is_a?(File)
@file_io = File.open(@file_io.path)
@data = get_data
@file_io.close
end

@data ||= get_data
end

def skip
@file_io.seek(@position)
@file_io.seek(size, IO::SEEK_CUR)
end

private
def get_data
@file_io.seek(@position)
@file_io.read(size)
end
end
end
6 changes: 3 additions & 3 deletions lib/wahwah/mp3_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def id3_version
end

def is_vbr?
xing_header.valid? || vbri_header.valid?
mpeg_frame_header.valid? && (xing_header.valid? || vbri_header.valid?)
end

private
Expand All @@ -48,8 +48,8 @@ def parse
end

def parse_id3_tag
id3_v1_tag = ID3::V1.new(@file_io)
id3_v2_tag = ID3::V2.new(@file_io)
id3_v1_tag = ID3::V1.new(@file_io.dup)
id3_v2_tag = ID3::V2.new(@file_io.dup)

return id3_v2_tag if id3_v2_tag.valid?
id3_v1_tag if id3_v1_tag.valid?
Expand Down
2 changes: 1 addition & 1 deletion lib/wahwah/mp4/atom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def parse_children_atoms
atom = self.class.new(atoms_data_io)
children_atoms.push(atom)

atoms_data_io.seek(atom.size, IO::SEEK_CUR)
atom.skip
end

children_atoms
Expand Down
16 changes: 4 additions & 12 deletions lib/wahwah/ogg_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ class OggTag < Tag
:composer,
:sample_rate

def duration
@duration ||= parse_duration
end

def bitrate
@bitrate ||= parse_bitrate
end

def bit_depth
@bit_depth ||= parse_bit_depth
end

private
def packets
@packets ||= Ogg::Packets.new(@file_io)
Expand All @@ -51,6 +39,10 @@ def parse
when identification_packet.start_with?("\x7FFLAC")
Ogg::FlacTag.new(identification_packet, comment_packet)
end

@duration = parse_duration
@bitrate = parse_bitrate
@bit_depth = parse_bit_depth
end

def parse_duration
Expand Down
8 changes: 1 addition & 7 deletions lib/wahwah/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,10 @@ def initialize(file)
value = instance_variable_get("@#{attr_name}")&.to_i
instance_variable_set("@#{attr_name}", value)
end
end

def close
ensure
@file_io.close
end

def closed?
@file_io.closed?
end

def inspect
inspect_id = ::Kernel.format '%x', (object_id * 2)
inspect_attributes_values = INSPECT_ATTRIBUTES.map { |attr_name| "#{attr_name}=#{self.send(attr_name)}" }.join(' ')
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ module TestHelpers
def binary_data(file_path)
File.read(file_path).force_encoding('BINARY').strip
end

def file_io_closed?(tag)
tag.instance_variable_get(:@file_io).closed?
end
end

Minitest::Test.send(:include, TestHelpers)
1 change: 1 addition & 0 deletions test/wahwah/asf_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ def test_parse
assert_equal 192, tag.bitrate
assert_equal 44100, tag.sample_rate
assert_equal 16, tag.bit_depth
assert file_io_closed?(tag)
end
end
3 changes: 3 additions & 0 deletions test/wahwah/flac_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_vorbis_comment_tag_file
assert_equal 'image/jpeg', image[:mime_type]
assert_equal :cover_front, image[:type]
assert_equal binary_data('test/files/cover.jpeg'), image[:data].strip
assert file_io_closed?(tag)
end

def test_id3_header_tag_file
Expand All @@ -42,6 +43,7 @@ def test_id3_header_tag_file
assert_equal 705, tag.bitrate
assert_equal 16, tag.bit_depth
assert_equal 44100, tag.sample_rate
assert file_io_closed?(tag)
end

def test_invalid_tag_file
Expand All @@ -60,5 +62,6 @@ def test_invalid_tag_file
assert_equal 705, tag.bitrate
assert_equal 16, tag.bit_depth
assert_equal 44100, tag.sample_rate
assert file_io_closed?(tag)
end
end
18 changes: 18 additions & 0 deletions test/wahwah/lazy_read_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,22 @@ def test_skip_data
@tag.skip
assert_equal 38, @tag.instance_variable_get(:@file_io).send(:pos)
end

def test_get_data_when_string_io_closed
string_io = StringIO.new("\x00\x00\x00\"\x10\x00\x10\x00\x00\x00\x0E\x00\x00\x10\n\xC4B\xF0\x00\x05b d\xA9\xFD\x7Fl\xB0\xE1\xC9Z\xFE\xCD\xF3\xA3iqO".b)
tag = Tag.new(string_io)
string_io.close

assert_equal "\x10\x00\x10\x00\x00\x00\x0E\x00\x00\x10\n\xC4B\xF0\x00\x05b d\xA9\xFD\x7Fl\xB0\xE1\xC9Z\xFE\xCD\xF3\xA3iqO".b, tag.data
assert string_io.closed?
end

def test_get_data_when_file_io_closed
file_io = File.open('test/files/id3v1.mp3')
tag = Tag.new(file_io)
file_io.close

assert_equal "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Xi".b, tag.data
assert file_io.closed?
end
end
11 changes: 11 additions & 0 deletions test/wahwah/mp3_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def test_id3v1_tag_file
assert_equal 'Joint Stereo', tag.channel_mode
assert_equal 44100, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_id3v22_tag_file
Expand Down Expand Up @@ -54,6 +55,7 @@ def test_id3v22_tag_file
assert_equal 'Stereo', tag.channel_mode
assert_equal 44100, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_id3v23_tag_file
Expand Down Expand Up @@ -85,6 +87,7 @@ def test_id3v23_tag_file
assert_equal 'Joint Stereo', tag.channel_mode
assert_equal 44100, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_id3v24_tag_file
Expand Down Expand Up @@ -116,6 +119,7 @@ def test_id3v24_tag_file
assert_equal 'Joint Stereo', tag.channel_mode
assert_equal 44100, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_id3v2_with_extented_tag_file
Expand All @@ -132,6 +136,7 @@ def test_id3v2_with_extented_tag_file
assert_equal 'Single Channel', tag.channel_mode
assert_equal 22050, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_vbri_header_file
Expand Down Expand Up @@ -159,6 +164,7 @@ def test_vbri_header_file
assert_equal 'Stereo', tag.channel_mode
assert_equal 44100, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_invalid_id3_file
Expand Down Expand Up @@ -187,13 +193,15 @@ def test_invalid_id3_file
assert_equal 'Stereo', tag.channel_mode
assert_equal 44100, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_compressed_image_file
tag = WahWah.open('test/files/compressed_image.mp3')
image = tag.images.first

assert_equal binary_data('test/files/compressed_cover.bmp'), image[:data].strip
assert file_io_closed?(tag)
end

def test_incomplete_file
Expand Down Expand Up @@ -222,6 +230,7 @@ def test_incomplete_file
assert_nil tag.channel_mode
assert_nil tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_invalid_encoding_string_file
Expand Down Expand Up @@ -250,6 +259,7 @@ def test_invalid_encoding_string_file
assert_nil tag.channel_mode
assert_nil tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_utf16_string_file
Expand Down Expand Up @@ -278,5 +288,6 @@ def test_utf16_string_file
assert_nil tag.channel_mode
assert_nil tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end
end
1 change: 1 addition & 0 deletions test/wahwah/mp4_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ def test_parse
assert_equal 128, tag.bitrate
assert_equal 44100, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end
end
3 changes: 3 additions & 0 deletions test/wahwah/ogg_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_vorbis_tag_file
assert_equal 192, tag.bitrate
assert_equal 44100, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_opus_tag_file
Expand All @@ -39,6 +40,7 @@ def test_opus_tag_file
assert_equal 2, tag.bitrate
assert_equal 48000, tag.sample_rate
assert_nil tag.bit_depth
assert file_io_closed?(tag)
end

def test_flac_tag_file
Expand All @@ -58,5 +60,6 @@ def test_flac_tag_file
assert_equal 705, tag.bitrate
assert_equal 44100, tag.sample_rate
assert_equal 16, tag.bit_depth
assert file_io_closed?(tag)
end
end
4 changes: 0 additions & 4 deletions test/wahwah/riff/chunk_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ def test_odd_size_chunk
content = StringIO.new("IART\t\x00\x00\x00Iggy".b)
chunk = WahWah::Riff::Chunk.new(content)

assert_equal 9, chunk.instance_variable_get(:@size)

# If the chunk's length is not even, add one pad byte,
# so the real size should be even.
assert_equal 10, chunk.size
end

Expand Down
2 changes: 2 additions & 0 deletions test/wahwah/riff_tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_id3_tag_file
assert_equal 'Stereo', tag.channel_mode
assert_equal 44100, tag.sample_rate
assert_equal 16, tag.bit_depth
assert file_io_closed?(tag)
end

def test_riff_info_tag_file
Expand All @@ -43,5 +44,6 @@ def test_riff_info_tag_file
assert_equal 'Stereo', tag.channel_mode
assert_equal 44100, tag.sample_rate
assert_equal 16, tag.bit_depth
assert file_io_closed?(tag)
end
end
15 changes: 2 additions & 13 deletions test/wahwah/tag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,7 @@ def test_inspect
end

def test_closed
io = StringIO.new()
io.close
tag = SubTag.new(io)

assert tag.closed?
end

def test_close
io = StringIO.new()
tag = SubTag.new(io)
tag.close

assert io.closed?
tag = SubTagWithParse.new('test/files/id3v1.mp3')
assert file_io_closed?(tag)
end
end

0 comments on commit 16579fc

Please sign in to comment.