Commit af86725c by Stephen Pike

Handle .docx, .xlsx, and .pptx with extra magic

The freedesktop magic doesn't work for the Office 2007+ files, it
detects them all as application/zip. I have magic which detects them,
but it requires looking at 2,000 byte ranges, too much for their
project (which has to support things like file browsers). Since you
already warn that `by_magic` is slow, it'd be nice to support it here
at least.

This would allow the paperclip project to use mimemagic and stop
relying on the `file` binary:

  - https://github.com/thoughtbot/paperclip/issues/1530#issuecomment-43375497

Support is enabled through an overlay, required with

    require 'mimemagic/overlay'

Documentation is in the README
parent a20166c3
......@@ -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
===
......
# 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
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
......
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