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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)