I am using Rails 5.x and cannot figure out how to automatically detect mime types. I saw this post here Detect MIME type of uploaded file in Ruby however, when I try to use require 'filemagic
, I get the following error in the console:
# irb 12:25PM/02.20
2.5.1 :001 > require 'filemagic'
Traceback (most recent call last):
13: from /usr/local/rvm/rubies/ruby-2.5.1/bin/irb:11:in `<main>'
12: from (irb):1
11: from /usr/local/rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in `require'
10: from /usr/local/rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
9: from /usr/local/rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `require'
8: from /usr/local/rvm/gems/ruby-2.5.1/gems/filemagic-0.4.0.0/lib/filemagic.rb:1:in `<top (required)>'
7: from /usr/local/rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
6: from /usr/local/rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
5: from /usr/local/rvm/gems/ruby-2.5.1/gems/filemagic-0.4.0.0/lib/filemagic/engine.rb:1:in `<top (required)>'
4: from /usr/local/rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
3: from /usr/local/rvm/rubies/ruby-2.5.1/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:70:in `require'
2: from /usr/local/rvm/gems/ruby-2.5.1/gems/refile-0.6.2/lib/refile/rails.rb:4:in `<top (required)>'
1: from /usr/local/rvm/gems/ruby-2.5.1/gems/refile-0.6.2/lib/refile/rails.rb:6:in `<module:Refile>'
NameError (uninitialized constant Refile::Rails)
2.5.1 :002 >
When I tried to manually install ruby-filemagic using gem install ruby-filemagic
(in case something was screwed up), I get the following error:
# gem install ruby-filemagic 12:25PM/02.20
Building native extensions. This could take a while...
ERROR: Error installing ruby-filemagic:
ERROR: Failed to build gem native extension.
current directory: /usr/local/rvm/gems/ruby-2.5.1/gems/ruby-filemagic-0.7.2/ext/filemagic
/usr/local/rvm/rubies/ruby-2.5.1/bin/ruby -r ./siteconf20200220-8004-1i08kxm.rb extconf.rb
checking for -lgnurx... no
checking for magic_open() in -lmagic... no
*** ERROR: missing required library to compile this module
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--with-make-prog
--without-make-prog
--srcdir=.
--curdir
--ruby=/usr/local/rvm/rubies/ruby-2.5.1/bin/$(RUBY_BASE_NAME)
--with-magic-dir
--without-magic-dir
--with-magic-include
--without-magic-include=${magic-dir}/include
--with-magic-lib
--without-magic-lib=${magic-dir}/lib
--with-gnurx-dir
--without-gnurx-dir
--with-gnurx-include
--without-gnurx-include=${gnurx-dir}/include
--with-gnurx-lib
--without-gnurx-lib=${gnurx-dir}/lib
--with-gnurxlib
--without-gnurxlib
--with-magiclib
--without-magiclib
To see why this extension failed to compile, please check the mkmf.log which can be found here:
/usr/local/rvm/gems/ruby-2.5.1/extensions/x86_64-linux/2.5.0/ruby-filemagic-0.7.2/mkmf.log
extconf failed, exit code 1
Gem files will remain installed in /usr/local/rvm/gems/ruby-2.5.1/gems/ruby-filemagic-0.7.2 for inspection.
Results logged to /usr/local/rvm/gems/ruby-2.5.1/extensions/x86_64-linux/2.5.0/ruby-filemagic-0.7.2/gem_make.out
Since this didn't really work out, I went to the next proposed solution which suggested using MIME::Types.type_for("filename.gif").first.content_type
for Ruby on Rails, but I cannot use this because I get the following error:
# rails c
Running via Spring preloader in process 8106
Loading development environment (Rails 5.2.4)
2.5.1 :001 > f = "/path/to/pdf"
=> "/path/to/pdf"
2.5.1 :002 > MIME::Types.type_for(f).first.content_type
Traceback (most recent call last):
1: from (irb):2
NameError (uninitialized constant MIME)
2.5.1 :003 >
To try to fix this error, I saw the suggestion to put require 'mime/types'
at the top, but I can't use that either:
2.5.1 :003 > require 'mime/types'
Traceback (most recent call last):
1: from (irb):3
LoadError (cannot load such file -- mime/types)
2.5.1 :004 >
All I'm trying to do is detect the mimetype so that I can use it with the slack-ruby-client
, but cannot seem to accomplish this.