Search code examples
ruby-on-railsrubyrails-activestorage

Find the binary of a file uploaded using ActiveStorage


I am creating a website where the user can upload a document as PDF. It is my understanding that a file, once uploaded, it is saved as a binary file in the database.

I want to find how this document is saved (in binary) by ActiveStorage. Basically, I need to retrieve in my controller the 0s and 1s of that specific file.

I want to know how I can retrieve this binary file (as a string).

I need the binary because I want to hash it in order to compare the hash of a PDF with a hash of another PDF and say if they are identical.

In addition: if a user upload the same PDF file two times. Is the binary in ActiveRecords the same?


Solution

  • You don't need to do that, Active Storage computes a Base64-encoded MD5 digest of your file, available via checksum method.

    ActiveStorage::Blob#checksum