diff --git a/lib/mini_mime.rb b/lib/mini_mime.rb index 566a5b1..2227e90 100644 --- a/lib/mini_mime.rb +++ b/lib/mini_mime.rb @@ -102,6 +102,16 @@ def initialize(path, sort_order) @file_length = File.size(@path) @rows = @file_length / @row_length + # Make sure the DB file is seekable, use a StringIO wrapper if it's not + is_seekable = (@file.tell == @row_length) + begin + @file.seek(0) + rescue IOError, SystemCallError + is_seekable = false + end + is_seekable = false unless @file.tell == 0 + @file = StringIO.new(File.read(@path)) unless is_seekable + @hit_cache = Cache.new(MAX_CACHED) @miss_cache = Cache.new(MAX_CACHED) diff --git a/test/mini_mime_test.rb b/test/mini_mime_test.rb index da8aac2..6eb3d69 100644 --- a/test/mini_mime_test.rb +++ b/test/mini_mime_test.rb @@ -12,7 +12,7 @@ def test_that_it_has_a_version_number end def test_extension - assert_equal "application/zip", MiniMime.lookup_by_extension("zip").content_type + assert_equal "application/zip", MiniMime.lookup_by_extension("zip")&.content_type end def test_mixed_case @@ -20,17 +20,17 @@ def test_mixed_case # irb(main):009:0> MIME::Types.type_for("TxT").first.to_s # => "text/plain" - assert_equal "application/vnd.groove-tool-message", MiniMime.lookup_by_filename("a.GTM").content_type - assert_equal "application/zip", MiniMime.lookup_by_extension("ZiP").content_type + assert_equal "application/vnd.groove-tool-message", MiniMime.lookup_by_filename("a.GTM")&.content_type + assert_equal "application/zip", MiniMime.lookup_by_extension("ZiP")&.content_type end def test_content_type - assert_equal "application/vnd.lotus-1-2-3", MiniMime.lookup_by_filename("a.123").content_type - assert_equal "application/x-compressed", MiniMime.lookup_by_filename("a.Z").content_type - assert_equal "application/vnd.groove-tool-message", MiniMime.lookup_by_filename("a.gtm").content_type - assert_equal "application/vnd.HandHeld-Entertainment+xml", MiniMime.lookup_by_filename("a.zmm").content_type - assert_equal "text/csv", MiniMime.lookup_by_filename("x.csv").content_type - assert_equal "application/x-msaccess", MiniMime.lookup_by_filename("x.mda").content_type + assert_equal "application/vnd.lotus-1-2-3", MiniMime.lookup_by_filename("a.123")&.content_type + assert_equal "application/x-compressed", MiniMime.lookup_by_filename("a.Z")&.content_type + assert_equal "application/vnd.groove-tool-message", MiniMime.lookup_by_filename("a.gtm")&.content_type + assert_equal "application/vnd.HandHeld-Entertainment+xml", MiniMime.lookup_by_filename("a.zmm")&.content_type + assert_equal "text/csv", MiniMime.lookup_by_filename("x.csv")&.content_type + assert_equal "application/x-msaccess", MiniMime.lookup_by_filename("x.mda")&.content_type assert_nil MiniMime.lookup_by_filename("a.frog") end @@ -69,11 +69,7 @@ def test_full_parity_with_mime_types type ||= types.detect(&:registered) type ||= types.first - # if type.content_type != MiniMime.lookup_by_filename("a.#{ext}").content_type - # puts "#{ext} Expected #{type.content_type} Got #{MiniMime.lookup_by_filename("a.#{ext}").content_type}" - # end - - assert_equal type.content_type, MiniMime.lookup_by_filename("a.#{ext}").content_type + assert_equal type.content_type, MiniMime.lookup_by_filename("a.#{ext}")&.content_type end end end