Read more

Caution: Carrierwave has your file three times (temporarily)

Dominik Schöler
June 28, 2017Software engineer at makandra GmbH

When storing a file with Carrierwave, it is always cached prior to actually storing it (to support form roundtrips Show archive.org snapshot ).

Illustration book lover

Growing Rails Applications in Practice

Check out our e-book. Learn to structure large Ruby on Rails codebases with the tools you already know and love.

  • Introduce design conventions for controllers and user-facing models
  • Create a system for growth
  • Build applications to last
Read more Show archive.org snapshot

Carrierwave by default 1) copies the file to the cache and then 2) copies it again to its final destination (deleting the cached file immediately after storing). This means there are 3 (three) instances of a file on the disk at some point in time, and still 2 instances after Carrierwave is done if you do not remove the original file manually.

Usually this is not an issue, because a file upload from a browser will immediately write to the cache, and most files are of a size that is still ok when tripled. However, this can become an issue with large files, regarding both time and disk usage.

Suggested solution for large files

You can easily configure Carrierwave to move the files instead of copying them. Simply define two methods in your uploader class:

def move_to_cache
  true
end

def move_to_store
  true
end

Profit!

Posted by Dominik Schöler to makandra dev (2017-06-28 14:45)