Commit f4afc9ba by Rosa Gutierrez Committed by Alex Tambellini

Handle Office Open XML types for filed generated outside MSOffice

It turns out that the position of '[Content_Types].xml' is not fixed in
this format, and whereas MS Office adds it in the beginning in general,
when files are generated using Google Docs, or RubyXL, this is not
necessarily true. They might add it at the end, so all these would be
detected as application/zip.

This fixes #39
parent dd1f09fe
# Extra magic
[['application/vnd.openxmlformats-officedocument.presentationml.presentation', [[0, "PK\003\004", [[30, '[Content_Types].xml', [[0..5000, 'ppt/']]]]]]],
['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', [[0, "PK\003\004", [[30, '[Content_Types].xml', [[0..5000, 'xl/']]]]]]],
['application/vnd.openxmlformats-officedocument.wordprocessingml.document', [[0, "PK\003\004", [[30, '[Content_Types].xml', [[0..5000, 'word/']]]]]]]].each do |magic|
[['application/vnd.openxmlformats-officedocument.presentationml.presentation', [[0, "PK\003\004", [[0..5000, '[Content_Types].xml', [[0..5000, 'ppt/']]]]]]],
['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', [[0, "PK\003\004", [[0..5000, '[Content_Types].xml', [[0..5000, 'xl/']]]]]]],
['application/vnd.openxmlformats-officedocument.wordprocessingml.document', [[0, "PK\003\004", [[0..5000, '[Content_Types].xml', [[0..5000, 'word/']]]]]]]].each do |magic|
MimeMagic.add(magic[0], magic: magic[1])
end
......@@ -70,14 +70,17 @@ class TestMimeMagic < Minitest::Test
def test_recognize_xlsx_as_zip_without_magic
file = "test/files/application.vnd.openxmlformats-officedocument.spreadsheetml.sheet"
assert_equal "application/zip", MimeMagic.by_magic(File.read(file)).to_s
assert_equal "application/zip", MimeMagic.by_magic(File.open(file, 'rb')).to_s
%w(msoffice rubyxl gdocs).each do |variant|
file = "test/files/application.vnd.openxmlformats-officedocument.spreadsheetml{#{variant}}.sheet"
assert_equal "application/zip", MimeMagic.by_magic(File.read(file)).to_s
assert_equal "application/zip", MimeMagic.by_magic(File.open(file, 'rb')).to_s
end
end
def test_recognize_by_magic
load "mimemagic/overlay.rb"
Dir['test/files/*'].each do |file|
mime = file[11..-1].sub('.', '/')
mime = file[11..-1].sub('.', '/').sub(/\{\w+\}/, '')
assert_equal mime, MimeMagic.by_magic(File.read(file)).to_s
assert_equal mime, MimeMagic.by_magic(File.open(file, 'rb')).to_s
end
......@@ -85,9 +88,11 @@ class TestMimeMagic < Minitest::Test
def test_recognize_all_by_magic
load 'mimemagic/overlay.rb'
file = 'test/files/application.vnd.openxmlformats-officedocument.spreadsheetml.sheet'
mimes = %w[application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/zip]
assert_equal mimes, MimeMagic.all_by_magic(File.read(file)).map(&:type)
%w(msoffice rubyxl gdocs).each do |variant|
file = "test/files/application.vnd.openxmlformats-officedocument.spreadsheetml{#{variant}}.sheet"
mimes = %w[application/vnd.openxmlformats-officedocument.spreadsheetml.sheet application/zip]
assert_equal mimes, MimeMagic.all_by_magic(File.read(file)).map(&:type)
end
end
def test_have_add
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment