Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check if a Ruby variable contains binary data?

I'm using Ruby 2.4 and Rails 5. I have file content in a variabe named "content". The content could contain data from things like a PDF file, a Word file, or an HTML file. Is there any way to tell if the variable contains binary data? Ultimately, I would like to know if this is a PDf, Microsoft Office, or some other type of OpenOffice file. This answer -- Rails: possible to check if a string is binary? -- suggests that I can check the encoding of the variable

content.encoding

and it would produce

ASCII-8BIT

in the case of binary data, however, I've noticed there are cases where HTML content stored in the variable could also return "ASCII-8BIT" as the content.encoding, so using "content.encoding" is not a foolproof way to tell me if I have binary data. Does such a way exist and if so, what is it?

like image 351
Dave Avatar asked Apr 11 '26 00:04

Dave


1 Answers

If your real question is not about binary data per se but about determining the file type of the data, I'd recommend to have a look at the ruby-filemagic gem which will give you this information much more reliably. The gem is a simple wrapper around the libmagic library which is standard on unix-like systems. The library works by scanning the content of a file and matching it against a set of known "magic" patterns in various file types.

Sample usage for a string buffer (e.g. data read form the database):

require "ruby-filemagic"

content = File.read("/.../sample.pdf") # just an example to get some data

fm = FileMagic.new
fm.buffer(content)    
#=> "PDF document, version 1.4"

For the gem to work (and compile) you need the file utility as well as the magic library with headers installed on your system. Quoting from the readme:

The file(1) library and headers are required:

Debian/Ubuntu:: +libmagic-dev+
Fedora/SuSE:: +file-devel+
Gentoo:: +sys-libs/libmagic+
OS X:: brew install libmagic

Tested to work well under Rails 5.

like image 95
Matouš Borák Avatar answered Apr 13 '26 10:04

Matouš Borák