Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrierwave temp directory set to uploads/tmp folder

I started using Carrierwave, and I found out that it stores temporary files at multiple places depending on whether it is testing or delayed job.

If it is done during testing(rspec), the temp files will be at RAILS_ROOT/uploads/tmp directory.

If it is done during delayed job, the temp files will be at RAILS_ROOT/public/uploads/tmp

First, I was thinking that rails_root/tmp/uploads would be a more sensible place, or even the system temporary folder.

Second, the testing being different to normal run seems like a bug.

Is there a way to fix this (either by configuration or monkey patching)? And can I put things in the RAILS_ROOT/tmp folder?

like image 255
lulalala Avatar asked Mar 05 '12 04:03

lulalala


3 Answers

There is config.cache_dir option you can set in 'config/initializers/carrierwave.rb'. But it is relative to '/public'. Looks like you should do that in your uploaders:

class MyUploader < CarrierWave::Uploader::Base

  def cache_dir
    # should return path to cache dir
    Rails.root.join 'tmp/uploads'
  end
end
like image 55
Art Shayderov Avatar answered Nov 06 '22 01:11

Art Shayderov


Im using carrierwave 0.10.0 and It seems this behaviour was addressed. Now the cache_dir setting accepts a path that is outside the public directory:

CarrierWave.configure do |config|
  config.cache_dir = Rails.root.join 'tmp/uploads'
end
like image 34
Mariano Cavallo Avatar answered Nov 06 '22 01:11

Mariano Cavallo


@iKindred's answer worked for me, but I thought I'd expand on how I got it to work on Rails 4.2.3 with CarrierWave 0.10.0:

Where to put the configure block

As CarrierWave is not part of Rails, the best place to put the configure block, according to my reading of the Rails Guide, is an initializer file. The name can be chosen arbitrarily, but it needs to reside in Rails.root/config/initializers. I chose to name the file after the module I was configuring: Rails.root/config/initializers/carrier_wave.rb.

What to put in the initializer file

I find that I run tests while my Rails app is running in development mode. After the tests I clean up all files by unlinking the storage paths as described here. For that to work without messing with the files stored by the Rails app running in development mode, storage paths need to be separated by environment! Therefore, my modified version of @iKindred's answer is:

#config/initializers/carrier_wave.rb
CarrierWave.configure do |config|
  config.cache_dir = File.join(Rails.root, 'tmp', 'uploads', Rails.env)
end
like image 45
winni2k Avatar answered Nov 06 '22 03:11

winni2k