Commit 52c286c6 by Daniel Mendler

Merge pull request #20 from scpike/extra-magic-overlays

Handle .docx, .xlsx, and .pptx with extra magic
parents a20166c3 7ba6377e
...@@ -15,6 +15,17 @@ Usage ...@@ -15,6 +15,17 @@ Usage
MimeMagic.by_magic(File.open('test.html')) MimeMagic.by_magic(File.open('test.html'))
# etc... # etc...
Extra magic overlay
=====
Microsoft Office 2007+ formats (xlsx, docx, and pptx) are not supported by the mime database at freedesktop.org. These files are all zipped collections of xml files and will be detected as "application/zip". Mimemagic comes with extra magic you can overlay on top of the defaults to correctly detect these file types. Enable it like this:
require 'mimemagic'
require 'mimemagic/overlay'
MimeMagic.by_magic(File.open('test.xlsx'))
You can add your own magic with `MimeMagic.add`. See `lib/mimemagic/overlay.rb`.
API API
=== ===
......
# Extra magic
[['application/vnd.openxmlformats-officedocument.presentationml.presentation', [[0..2000, 'ppt/']]],
['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', [[0..2000, 'xl/']]],
['application/vnd.openxmlformats-officedocument.wordprocessingml.document', [[0..2000, 'word/']]]].each do |magic|
MimeMagic.add(magic[0], magic: magic[1])
end
...@@ -50,9 +50,16 @@ describe 'MimeMagic' do ...@@ -50,9 +50,16 @@ describe 'MimeMagic' do
MimeMagic.by_path('').should.equal nil MimeMagic.by_path('').should.equal nil
end end
it 'should recognize xlsx as zip without magic' do
file = "test/files/application.vnd.openxmlformats-officedocument.spreadsheetml.sheet"
MimeMagic.by_magic(File.read(file)).should.equal "application/zip"
MimeMagic.by_magic(File.open(file, 'rb')).should.equal "application/zip"
end
it 'should recognize by magic' do it 'should recognize by magic' do
require "mimemagic/overlay"
Dir['test/files/*'].each do |file| Dir['test/files/*'].each do |file|
mime = file[11..-1].gsub('.', '/') mime = file[11..-1].sub('.', '/')
MimeMagic.by_magic(File.read(file)).should.equal mime MimeMagic.by_magic(File.read(file)).should.equal mime
MimeMagic.by_magic(File.open(file, 'rb')).should.equal mime MimeMagic.by_magic(File.open(file, 'rb')).should.equal mime
end end
......
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