Caution: Carrierwave has your file three times (temporarily)

Updated . Posted . Visible to the public.

When storing a file with Carrierwave, it is always cached prior to actually storing it (to support form roundtrips 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!

Dominik Schöler
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2017-06-28 12:45)