Skip to content

Commit

Permalink
Merge pull request #20 from scpike/extra-magic-overlays
Browse files Browse the repository at this point in the history
Handle .docx, .xlsx, and .pptx with extra magic
  • Loading branch information
minad committed Mar 21, 2015
2 parents a20166c + 7ba6377 commit 52c286c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -15,6 +15,17 @@ Usage
MimeMagic.by_magic(File.open('test.html'))
# 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
===

Expand Down
7 changes: 7 additions & 0 deletions lib/mimemagic/overlay.rb
@@ -0,0 +1,7 @@
# 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
Binary file not shown.
9 changes: 8 additions & 1 deletion test/mimemagic_test.rb
Expand Up @@ -50,9 +50,16 @@
MimeMagic.by_path('').should.equal nil
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
require "mimemagic/overlay"
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.open(file, 'rb')).should.equal mime
end
Expand Down

0 comments on commit 52c286c

Please sign in to comment.