{"id":17832408,"url":"https://github.com/cybergizer-hq/libmagicrb","last_synced_at":"2025-08-13T22:31:02.243Z","repository":{"id":56881103,"uuid":"397499365","full_name":"cybergizer-hq/LibmagicRb","owner":"cybergizer-hq","description":"Check filetype with libmagic","archived":false,"fork":false,"pushed_at":"2024-09-27T12:54:03.000Z","size":410,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-10T11:50:12.804Z","etag":null,"topics":["libmagic","railsgems","ruby","rubygems"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cybergizer-hq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-18T06:39:45.000Z","updated_at":"2025-04-16T14:47:36.000Z","dependencies_parsed_at":"2022-08-20T23:40:14.047Z","dependency_job_id":null,"html_url":"https://github.com/cybergizer-hq/LibmagicRb","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cybergizer-hq/LibmagicRb","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cybergizer-hq%2FLibmagicRb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cybergizer-hq%2FLibmagicRb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cybergizer-hq%2FLibmagicRb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cybergizer-hq%2FLibmagicRb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cybergizer-hq","download_url":"https://codeload.github.com/cybergizer-hq/LibmagicRb/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cybergizer-hq%2FLibmagicRb/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270328953,"owners_count":24565769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-13T02:00:09.904Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["libmagic","railsgems","ruby","rubygems"],"created_at":"2024-10-27T19:56:48.163Z","updated_at":"2025-08-13T22:31:01.940Z","avatar_url":"https://github.com/cybergizer-hq.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LibmagicRb [![Gem Version](https://badge.fury.io/rb/libmagic_rb.svg)](https://rubygems.org/gems/libmagic_rb) ![Workflow Status](https://github.com/cybergizer-hq/libmagicrb/workflows/LibmagicRb%20Test/badge.svg)\n\nAdds ability to check mime-type of a file using the libmagic ([magic(4)](https://man7.org/linux/man-pages/man4/magic.4.html)).\nIt uses native extensions and it's quite performant.\n\n## Pre-Installation\nOn Linux, you need to install libmagic. The version we recommend is at least 5.36.\nBut if you have an older version of libmagic, it will compile and work flawlessly but with a few caveats:\n\n+ Many constants (modes and params) will be not defined\n+ LibmagicRb::MAGIC_VERSION constant will be set to 0.\n+ Few methods will not work.\n  some methods that can return a String or Integer will return nil (like setparam and getparam).\n\n[ Do note that the minimum tested version is 5.11, older than that may fail to compile the gem ]\n\nYou also need Ruby \u003e 1.9.0 in order to run this gem.\n\nWith that info in mind, let's continue to the installation part for your Linux distributions...\n\n#### Arch / Manjaro / Archlabs / Garuda Linux / Hefftor Linux \u0026 other Arch Based Linux\n\n```\n# pacman -S ruby file gcc make --needed\n```\n\n#### Debian / Ubuntu / Linux Mint / Deepin / Pop!_OS / RaspberryPi OS \u0026 other Debian Based Linux\n\n```\n# apt install ruby libmagic-dev ruby-dev gcc make\n```\n\n#### Fedora / Amazon Linux / CentOS \u0026 Other RedHat Based Linux\n\n```\n# yum install ruby file-devel ruby-devel gcc make\n```\n\n#### OpenSUSE\n\n```\n# zypper in ruby ruby-devel file-devel gcc make\n```\n\n#### Gentoo\n\nThere are already make, and magic.h available for Gentoo. You just need Ruby:\n\n```\n# emerge --ask dev-lang/ruby\n```\n\n#### DragonflyBSD / FreeBSD\n\nVersions upto 0.1.2 is tested on DragonflyBSD (Version 6.0-SYNTH).\n\n```\n# pkg install ruby devel/ruby-gems gcc gmake\n```\n\n#### MacOS\n\nYou can install libmagic with homebrew:\n\n```\n$ brew install libmagic\n\n# if the link is already created is going to fail, don't worry about that\n$ brew link libmagic\n```\n\n[ Source: [ eparreno/gist:1845561 ](https://gist.github.com/eparreno/1845561) ]\n\n#### Windows\n\nWindows is currently not tested, hence the support is unknown.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'libmagic_rb'\n```\n\nAnd then execute:\n\n```\n$ bundle install\n```\n\nOr install it yourself as:\n\n```\n$ gem install libmagic_rb\n```\n\nWe recommend getting the gem only from Rubygems.org. Do not download this repo as zip, compile to install the gem.\nA rubygem version is released after various tests. Any branch here including Master branch can be unstable can even segfault your app. You've been warned!\n\n## Usage\nThe target of this gem is to add mime-type checking easily.\n\nUsing the LibmagicRb class we can check the mime type, EXIF data and other information of a file.\n\nTo check a file, you need to pass a hash argument to the FilemagicRb.new():\n\n1. db: Path to the Database (String)\n2. file: A file to check (String)\n3. mode: Modes of the file (Integer) (Optional, defaults to `LibmagicRb::MAGIC_MIME | LibmagicRb::MAGIC_CHECK | LibmagicRb::MAGIC_SYMLINK`)\n\n### Example 1:\n\n```\nrequire 'libmagic_rb'\n\ncookie = LibmagicRb.new(\n    file: '/usr/share/backgrounds/myimage.webp',\n    mode: LibmagicRb::MAGIC_MIME | LibmagicRb::MAGIC_CHECK | LibmagicRb::MAGIC_SYMLINK\n)\n\np cookie # =\u003e    #\u003cLibmagicRb:0x000055cf96d8f868 @db=\"/home/sourav/.gem/ruby/3.0.0/gems/libmagic_rb-0.1.0/data/magic.mgc\", @file=\"/usr/share/backgrounds/myimage.webp\", @mode=1106, @closed=false\u003e\n\n# In case The file path needs to be changed\ncookie.file = '/usr/share/backgrounds/vienna-5164602.jpg'\n\np cookie.file    # =\u003e \"/usr/share/backgrounds/vienna-5164602.jpg\"\np cookie.db    # =\u003e nil\n\ncookie.check()    # =\u003e image/jpeg; charset=binary\ncookie.close()    # =\u003e #\u003cLibmagicRb:0x000055fa77699818 @closed=true, @db=\"/home/sourav/.gem/ruby/3.0.0/gems/libmagic_rb-0.1.0/data/magic.mgc\", @file=\"/usr/share/backgrounds/vienna-5164602.jpg\", @mode=1106\u003e\ncookie.closed?() # =\u003e true\n```\n\nWhen the `db:` key is `nil`, it will use NULL as the database file - which gets the database automatically from the system.\nYou can omit `db: nil` as well for a nil value.\n\n### Example 2:\n```\nrequire 'libmagic_rb'\n\ncookie = LibmagicRb.new(\n    db: '/usr/share/file/magic.mgc',\n    file: '/usr/share/backgrounds/vienna-5164602.jpg',\n    mode: LibmagicRb::NONE\n)\n\ncookie.check()    # =\u003e \"JPEG image data, JFIF standard 1.01, resolution (DPI), density 300x300, segment length 16, Exif Standard: [TIFF image data, big-endian, direntries=4, manufacturer=NIKON CORPORATION, model=NIKON D5300], baseline, precision 8, 5959x3973, components 3\"\"\ncookie.close()    # =\u003e #\u003cLibmagicRb:0x000055fa77699818 @closed=true, @db=\"/home/sourav/.gem/ruby/3.0.0/gems/libmagic_rb-0.1.0/data/magic.mgc\", @file=\"/usr/share/backgrounds/vienna-5164602.jpg\", @mode=1106\u003e\ncookie.closed?() # =\u003e true\n```\nSurely, you can automatically get db path if you pass nil to it.  But If you happen to remove the database file or you are unsure, `db: nil` will not work (so will linux's file command).\nSpecifying the path gives you the privilege to pass a locally stored database.\n\nDo note that there should be version match for the magic files, otherwise it will raise `LibmagicRb::InvalidDBError`.\n\n### Example 3:\nLibmagicRb also provides a handy singleton method `:check`. You just need to pass the filename and modes:\n\n```\nrequire 'libmagic_rb'\n\nLibmagicRb.check(file: '/dev/zero', mode: LibmagicRb::MAGIC_NONE)    #=\u003e \"character special (1/5)\"\nLibmagicRb.check(file: '/', mode: LibmagicRb::MAGIC_NONE)    # =\u003e \"directory\"\nLibmagicRb.check(file: '/')    # =\u003e \"inode/directory; charset=binary\"\n```\n\nOptional:\n+ You can use the db: keyword for a custom path. By default it's set to nil. And as mentioned above, nil = automatically find the db from the system.\n+ The `mode:` key is optional, by default it's set to `LibmagicRb::MAGIC_MIME | LibmagicRb::MAGIC_CHECK | LibmagicRb::MAGIC_SYMLINK`.\n\nNotes:\n+ It's really **mandatory** to close the cookie (`cookie.close()`) when you are done with the cookie. Otherwise, you rely on the GC and that can cause various problems.\n  You can't also use the file after it's closed.\n+ You can change the file and db on the fly. But you can't change the mode. The mode can be assigned only with LibmagicRb.new(db: ..., file: ..., mode: ...)\n+ To list all the modes, please refer to the [man page](https://man7.org/linux/man-pages/man3/magic_getflags.3.html).\n\n### Open Modes\nFiles can be opened in various modes. You can use this short hand to see the supported modes:\n\n```\nLibmagicRb.lsmodes    # =\u003e {:MAGIC_NONE=\u003e0, :MAGIC_DEBUG=\u003e1, :MAGIC_SYMLINK=\u003e2, :MAGIC_COMPRESS=\u003e4, :MAGIC_DEVICES=\u003e8, :MAGIC_MIME_TYPE=\u003e16, :MAGIC_MIME_ENCODING=\u003e1024, :MAGIC_MIME=\u003e1040, :MAGIC_CONTINUE=\u003e32, :MAGIC_CHECK=\u003e64, :MAGIC_PRESERVE_ATIME=\u003e128, :MAGIC_RAW=\u003e256, :MAGIC_ERROR=\u003e512, :MAGIC_APPLE=\u003e2048, :MAGIC_EXTENSION=\u003e16777216, :MAGIC_COMPRESS_TRANSP=\u003e33554432, :MAGIC_NO_CHECK_APPTYPE=\u003e32768, :MAGIC_NO_CHECK_CDF=\u003e262144, :MAGIC_NO_CHECK_COMPRESS=\u003e4096, :MAGIC_NO_CHECK_ELF=\u003e65536, :MAGIC_NO_CHECK_ENCODING=\u003e2097152, :MAGIC_NO_CHECK_SOFT=\u003e16384, :MAGIC_NO_CHECK_TAR=\u003e8192, :MAGIC_NO_CHECK_TEXT=\u003e131072, :MAGIC_NO_CHECK_TOKENS=\u003e1048576, :MAGIC_NO_CHECK_CSV=\u003e524288, :MAGIC_NO_CHECK_JSON=\u003e4194304}\n```\n\nNote:\n+ Some keys can be missing or can have modified values, depending on your libmagic version.\n\n### Parameters\n\n#### Listing parameters\n\n```\nLibmagicRb.lsparams    # =\u003e {:MAGIC_PARAM_INDIR_MAX=\u003e0, :MAGIC_PARAM_NAME_MAX=\u003e1, :MAGIC_PARAM_ELF_NOTES_MAX=\u003e4, :MAGIC_PARAM_ELF_PHNUM_MAX=\u003e2, :MAGIC_PARAM_ELF_SHNUM_MAX=\u003e3, :MAGIC_PARAM_REGEX_MAX=\u003e5, :MAGIC_PARAM_BYTES_MAX=\u003e6}\n```\n\nNote:\n+ Some keys can be missing or can have modified values, depending on your libmagic version.\n\n#### Getting parameters\n\n```\ncookie.getparam(LibmagicRb::MAGIC_PARAM_REGEX_MAX) # =\u003e 8192; but can differ. Returns nil on error.\n```\n\n#### Setting parameters\n```\ncookie.setparam(LibmagicRb::MAGIC_PARAM_REGEX_MAX, 2 ** 14) # =\u003e 16384; but can differ. Returns nil on error.\n```\n\n#### Notes:\n\n+ To get the parameters, you can run `LibmagicRb.lsparams()` for description, please refer to the [man page](https://man7.org/linux/man-pages/man3/magic_getflags.3.html).\n+ `cookie.setparam()` returns the value after getting the param as well. So you don't need to confirm by calling getparam() again.\n+ The maximum size depends on the parameter. But the value that can be passed should not be more than 2 ** 32.\n+ On older versions of libmagic, where the function isn't available, both getparam() and setparam() will perform no operations, and return nil!\n\n## Errors\nThe following errors are implemented and raised on appropriate situation:\n\n1. `LibmagicRb::FileNotFound`: When the file is not found.\n2. `LibmagicRb::FileUnreadable`: When the file is unreadable.\n3. `LibmagicRb::InvalidDBError`: When the database given is invalid.\n4. `LibmagicRb::IsDirError`: When the database path is a directory.\n5. `LibmagicRb::FileClosedError`: When the file is already closed (closed?()) but you are trying to access the cookie.\n\n## Development\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at [https://github.com/cybergizer-hq/LibmagicRb](https://github.com/cybergizer-hq/LibmagicRb) This project is intended to be a safe, welcoming space for collaboration.\n\n## License\n\nThe gem is available as open-source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcybergizer-hq%2Flibmagicrb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcybergizer-hq%2Flibmagicrb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcybergizer-hq%2Flibmagicrb/lists"}