blackwinter / ruby-filemagic Goto Github PK
View Code? Open in Web Editor NEWRuby bindings to the magic(4) library, revised.
Home Page: https://blackwinter.github.iom/ruby-filemagic
Ruby bindings to the magic(4) library, revised.
Home Page: https://blackwinter.github.iom/ruby-filemagic
= FileMagic Library Binding == VERSION This documentation refers to filemagic version 0.7.3 == DESCRIPTION FileMagic extension module. See also libmagic(3), file(1) and magic(4). === Constants <tt>MAGIC_NONE</tt>:: No flags <tt>MAGIC_DEBUG</tt>:: Turn on debugging <tt>MAGIC_SYMLINK</tt>:: Follow symlinks <tt>MAGIC_COMPRESS</tt>:: Check inside compressed files <tt>MAGIC_DEVICES</tt>:: Look at the contents of devices <tt>MAGIC_MIME</tt>:: Return a mime string <tt>MAGIC_CONTINUE</tt>:: Return all matches, not just the first <tt>MAGIC_CHECK</tt>:: Print warnings to stderr === Methods <tt>file(filename)</tt>:: Returns a textual description of the contents of the filename argument <tt>buffer(string)</tt>:: Returns a textual description of the contents of the string argument <tt>check(filename)</tt>:: Checks the validity of entries in the database file passed in as filename <tt>compile(filename)</tt>:: Compiles the database file passed in as filename <tt>load(filename)</tt>:: Loads the database file passed in as filename <tt>close()</tt>:: Closes the magic database and frees any memory allocated === Synopsis require 'filemagic' p FileMagic::VERSION # => "0.7.3" p FileMagic::MAGIC_VERSION # => "5.39" p FileMagic.new.flags # => [] FileMagic.open(:mime) { |fm| p fm.flags # => [:mime_type, :mime_encoding] p fm.file(__FILE__) # => "text/plain; charset=us-ascii" p fm.file(__FILE__, true) # => "text/plain" fm.flags = [:raw, :continue] p fm.flags # => [:continue, :raw] } fm = FileMagic.new p fm.flags # => [] mime = FileMagic.mime p mime.flags # => [:mime_type, :mime_encoding] === Environment The environment variable +MAGIC+ can be used to set the default magic file name. === Installation Install the gem: sudo gem install ruby-filemagic The file(1) library and headers are required: Debian/Ubuntu:: +libmagic-dev+ Fedora/SuSE:: +file-devel+ Alpine:: <tt>libmagic file file-dev</tt> Gentoo:: +sys-libs/libmagic+ OS X:: <tt>brew install libmagic</tt> === Build native extension rake docker:gem:native Requires Docker[https://docker.com] to be installed. == LINKS Homepage:: https://www.darwinsys.com/file/ Documentation:: https://blackwinter.github.io/ruby-filemagic Source code:: https://github.com/blackwinter/ruby-filemagic RubyGem:: https://rubygems.org/gems/ruby-filemagic Travis CI:: https://travis-ci.org/blackwinter/ruby-filemagic == AUTHORS * Travis Whitton <mailto:[email protected]> (Original author) * Jens Wille <mailto:[email protected]> == CREDITS * Martin Carpenter <mailto:[email protected]> for Ruby 1.9.2 compatibility and other improvements. * Pavel Lobashov (@ShockwaveNN) for Dockerfile to build cross-compiled Windows extension (pull request #26). == COPYING The filemagic extension library is copywrited free software by Travis Whitton <[email protected]>. You can redistribute it under the terms specified in the COPYING file of the Ruby distribution. == WARRANTY THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
I getting this random error on jpeg file
FileMagic::FileMagicError
failed lookup: JPEG image data, Exif standard: [TIFF image data, little-endian, direntries=13, height=3072, manufacturer=Google, model=Pixel 7 Pro, orientation=upper-left, xresolution=189, yresolution=197, resolutionunit=2, software=HDR+ 1.0.479933560zd, datetime=2022:10:14 16:52:27, GPS-Data, width=4080] name use count (30) exceeded
After some research looks like its happen with some linux distros, at least I can't reproduce on MAC. Looks like the solution is increase the name recursion level.
https://bugs.astron.com/view.php?id=152
file --parameter name=50 test.jpg
The magic_getparam() and magic_setparam() allow getting and setting
various limits related to the magic library.Parameter Type Default MAGIC_PARAM_INDIR_MAX size_t 15 MAGIC_PARAM_NAME_MAX size_t 30 MAGIC_PARAM_ELF_NOTES_MAX size_t 256 MAGIC_PARAM_ELF_PHNUM_MAX size_t 128 MAGIC_PARAM_ELF_SHNUM_MAX size_t 32768 MAGIC_PARAM_REGEX_MAX size_t 8192 MAGIC_PARAM_BYTES_MAX size_t 1048576
I would like to know if this gem allow me pass any parameters?
I get the following error when calling file.content_type
on 0.6.0. It works fine with the same file on 0.5.0.
>> f.content_type
TypeError: wrong argument type Hash (expected Fixnum or Symbol)
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/ruby-filemagic-0.6.0/lib/filemagic.rb:56:in `flags'
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/ruby-filemagic-0.6.0/lib/filemagic.rb:56:in `fm'
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/ruby-filemagic-0.6.0/lib/filemagic/ext.rb:63:in `file_type'
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/ruby-filemagic-0.6.0/lib/filemagic/ext.rb:24:in `mime_type'
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/ruby-filemagic-0.6.0/lib/filemagic/ext.rb:31:in `content_type'
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/ruby-filemagic-0.6.0/lib/filemagic/ext.rb:51:in `content_type'
from (irb):2
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/railties-3.2.19/lib/rails/commands/console.rb:47:in `start'
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/railties-3.2.19/lib/rails/commands/console.rb:8:in `start'
from /usr/local/Cellar/ruby/2.1.2/lib/ruby/gems/2.1.0/gems/railties-3.2.19/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
gem install ruby-filemagic
fails. Tried the usual trick of env ARCHFLAGS="-arch x86_64" gem install ruby-filemagic -- --with-magic-include=/usr/local/include --with-magic-lib=/usr/local/lib/
to no avail.
Confirmed that libmagic.1.dylib
and libmagic.dylib
are in /usr/local/lib/. What other dependencies does this gem have?
Building native extensions. This could take a while...
ERROR: Error installing ruby-filemagic:
ERROR: Failed to build gem native extension.
/Users/me/.rvm/rubies/ruby-2.2.0/bin/ruby -r ./siteconf20150206-87002-kyeyoq.rb extconf.rb
checking for main() in -lgnurx... *** 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
--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=/Users/me/.rvm/rubies/ruby-2.2.0/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
/Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:456:in try_do': The compiler failed to generate an executable file. (RuntimeError) You have to install development tools first. from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:541:in
try_link0'
from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:556:in try_link' from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:741:in
try_func'
from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:972:in block in have_library' from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:917:in
block in checking_for'
from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:351:in block (2 levels) in postpone' from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:321:in
open'
from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:351:in block in postpone' from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:321:in
open'
from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:347:in postpone' from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:916:in
checking_for'
from /Users/me/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/mkmf.rb:967:in have_library' from extconf.rb:9:in
extconf failed, exit code 1
Gem files will remain installed in /Users/me/.rvm/gems/ruby-2.2.0/gems/ruby-filemagic-0.6.2 for inspection.
Results logged to /Users/me/.rvm/gems/ruby-2.2.0/extensions/x86_64-darwin-14/2.2.0/ruby-filemagic-0.6.2/gem_make.out
Similar to #10, if fat binary gem's are fine by the maintainers, it'd be great to get x86_64 pre-compiled versions as well for those using the 64 bit releases of ruby.
irb(main):001:0> RUBY_PLATFORM
=> "x64-mingw32"
/Users/mw82/.rvm/rubies/ruby-2.2.2/bin/ruby -r ./siteconf20150501-29651-ub6bv6.rb extconf.rb
checking for magic_open() in -lmagic... yes
checking for magic.h... yes
checking for file/patchlevel.h... no
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
compiling filemagic.c
filemagic.c:73:55: warning: use of logical '||' with constant operand [-Wconstant-logical-operand]
"failed to initialize magic cookie (%d)", errno || -1);
^ ~~
filemagic.c:73:55: note: use '|' for a bitwise operation
"failed to initialize magic cookie (%d)", errno || -1);
^~
|
1 warning generated.
linking shared-object filemagic/ruby_filemagic.bundle
make "DESTDIR=" install
/usr/bin/install -c -m 0755 ruby_filemagic.bundle ./.gem.20150501-29651-1efwg0i/filemagic
osx ruby 2.2.2
when use
brew install libmagic
then it is OK
should we add this in readme?
hi,
i get error about filename to long and i search about it but i cant find any thing.can you help me about how can i handled this error?
**
FileMagic::FileMagicError: failed lookup: cannot open 'دریاچه_ی_چیتگر_یا_شهدای_خلیج_فارس_بزرگترین_دریاچه_ی_مصنوعی_ایران_است_که_در_شمال_غرب_شهر_تهران_واقع_شده_و_در_کنار_این_دریاچه_پارک_جنگلی_چیتگر_قرار_گرفته_است.docx' (File name too long)
**
It looks like you've committed 0.7.3, but it just needs to be tagged too.
To get github to render the readme as markdown, it needs to have a .md/.markdown extension.
Hi, I tried to install the gem but it blowed up with the native dependency.
ree-1.8.7-2010.02 [~/gitcodes/httparty (master)] ➔ gem install ruby-filemagic
Building native extensions. This could take a while...
ERROR: Error installing ruby-filemagic:
ERROR: Failed to build gem native extension.
/Users/millisami/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby extconf.rb
checking for magic_open() in -lmagic... no
*** 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=/Users/millisami/.rvm/rubies/ree-1.8.7-2010.02/bin/ruby
--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-magiclib
--without-magiclib
*** ERROR: missing required library to compile this module
Gem files will remain installed in /Users/millisami/.rvm/gems/ree-1.8.7-2010.02/gems/ruby-filemagic-0.3.0 for inspection.
Results logged to /Users/millisami/.rvm/gems/ree-1.8.7-2010.02/gems/ruby-filemagic-0.3.0/ext/gem_make.out
What do I have to install first? I am on Mac OS X.
installed
brew install libmagic
but just can't make
gem install 'ruby-filemagic'
it says ERROR: missing required library to compile this module
the first error in the log file says
ld: library not found for -lgnurx
clang: error: linker command failed with exit code 1 (use -v to see invocation)
and there no nothing like gnurx for OS X
em::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /usr/local/rvm/gems/ruby-2.4.1/gems/ruby-filemagic-0.7.2/ext/filemagic
/usr/local/rvm/rubies/ruby-2.4.1/bin/ruby -I /usr/local/rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0 -r ./siteconf20200303-50089-1k0qtpb.rb extconf.rb
checking for -lgnurx... no
checking for magic_open() in -lmagic... yes
checking for magic.h... 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
I listed this gem in my Gemfile and tried bundle install.
It broke telling me that extconf.rb failed with some missing header or library.
It failed with:
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
/Users/cjohnson/.rbenv/versions/2.1.5/bin/ruby extconf.rb
checking for main() in -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.
I think the README would be easier to understand if the example embedded the output of the p
commands. For example:
p fm.file(__FILE__, true) # => "text/plain"
I can run the example myself, of course, but the output isn't labeled, so not the easiest to parse:
ruby /tmp/foo.rb
"0.7.1"
"5.22"
[]
[:mime_type, :mime_encoding]
"text/plain; charset=us-ascii"
"text/plain"
[:continue, :raw]
[]
[:mime_type, :mime_encoding]
Would you accept a patch that embeds the example's output inline like I showed at the top of this issue?
The file command allows (using the "-m" flag) the use of different magic files than the default. I don't believe that this is currently available from ruby using this gem. This functionality does exist in libmagic, though. I'm not fully up on it, but it seems like the call is to magic_load.
Using Mac OS X 10.7.1, ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0], and rubygems 1.6.2:
Fetching: ruby-filemagic-0.4.2.gem (100%)
Building native extensions. This could take a while...
ERROR: Error installing ruby-filemagic:
ERROR: Failed to build gem native extension.
/usr/local/Cellar/ruby/1.9.2-p136/bin/ruby extconf.rb
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/Cellar/ruby/1.9.2-p136/bin/ruby
--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-magiclib
--without-magiclib
Gem files will remain installed in /Users/aresnick/.rvm/gems/ruby-1.9.2-p180/gems/ruby-filemagic-0.4.2 for inspection.
Results logged to /Users/aresnick/.rvm/gems/ruby-1.9.2-p180/gems/ruby-filemagic-0.4.2/ext/gem_make.out
$gem install ruby-filemagic
WARNING: ruby-filemagic-0.7.3 ships with a dangling symlink named test/pylink pointing to missing test/pyfile file. Ignoring
Building native extensions. This could take a while...
ruby-filemagic-0.7.3 [2022-01-07]:
* Dockerfile to build native extension (pull request #26 by Pavel Lobashov).
* Include paths for ARM-based Apple Macs (Apple Silicon) (pull request #35 by
@545ch4).
Successfully installed ruby-filemagic-0.7.3
Parsing documentation for ruby-filemagic-0.7.3
Installing ri documentation for ruby-filemagic-0.7.3
Done installing documentation for ruby-filemagic after 0 seconds
1 gem installed
Hi
For blank text file (ex layout.liquid), method file return "inode/x-empty; charset=binary"
All version since 0.62 to 0.71 had precompiled extension for mingw32
and mswin32-60
I thin there is need for those version for 0.72 too
Or at least a clear instruction how to do it by yourself on windows. I tryed but failed
Getting this on a specific file, which file --mime-type returns "text/plain" for currently.
This error occurs under MRI 2.0.0-p353 and Rubinius 2.2.6.
nsullivan:~ nsullivan$ file --mime-type /Users/nsullivan/.rbx/09/097e79c388cab23f8981958bea209efff565ae1b
/Users/nsullivan/.rbx/09/097e79c388cab23f8981958bea209efff565ae1b: text/plain
Any ideas?
Your markdown file is in plain text. Just need to edit the file to include '.md'.
Great Gem though Btw!
I recently updated from Ruby 3.0.2 to 3.3.0 and I started getting the following warning at the line where I call FileMatic.new
:
warning: undefining the allocator of T_DATA class FileMagic
I googled it but didn't find anything useful about this error message. Good luck!
The ruby-filemagic gem has several incompatibilities with RubyInstaller-2.4.
ruby_filemagic.so
load error in the binary gem can probably be fixed per RubyInstaller::Runtime.add_dll_directory
- see here.mingw-w64-file
package to support native builds. I already opened a PR to make it available in the MSYS2 repo./usr/include
or /usr/lib
on MINGW. These directories contain CYGWIN/MSYS2 related files, which can not be used together with RubyInstaller. For RubyInstaller-2.4 it's enough to use the predefined library paths, provided the file
package is installed. dir_config
is usually not necessary.This came up due to the following issue on RubyInstaller: oneclick/rubyinstaller2#64
gem install ruby-filemagic
Fetching: ruby-filemagic-0.7.2.gem (100%)
Temporarily enhancing PATH to include DevKit...
ERROR: While executing gem ... (Errno::EACCES)
Permission denied @ rb_file_s_symlink - (test/pyfile, C:/ruby/ruby-2.3.3-x64-mingw32/lib/ruby/gems/2.3.0/gems/ruby-filemagic-0.7.2/test/pylink)
Using ruby-filemagic I will have to inform all my users, that they have to install a Rails-version prior 5.x and all the stuff that follows.
Compiling ruby-filemagic on Windows caused me some problems due to the dependencies. Ended up requiring the following:
Download file-5.21.tar.gz from ftp://ftp.astron.com/pub/file/ and extract somewhere.
Download libgnurx 2.5.1 bin + dev from sourceforge and extract somewhere
Build file
LDFLAGS=-L/a/path/to/libgnurx-2.5.1/lib
CFLAGS=-I/a/path/to/libgnurx-2.5.1/include
./configure --prefix=/a/path/to/file-2.21
make install
From inside the DevKit msys shell
gem install ruby-filemagic -- --with-magic-dir=/a/path/to/file-2.21
It would be nice if there were precompiled libraries including the Magic for Windows platforms.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.