0

Sometimes I am away from internet and still need to work on upload pages. Carrierwave Direct seems to force storage :fog; with no way of overriding in dev.
Is it possible to tell Carrierwave Direct to use local storage (:file) and simply fallback to Carrierwave's development config settings?

Setting storage :file in carrierwave initializer under development config settings doesnt work...carrierwave_direct errors with "is not a recognized provider" from "<%= direct_upload_form_for @uploader do |f| %>".

I have attempted to work around carrierwave direct, but between forcing :fog, expecting a redirect url and expecting the direct_upload_form_for form method...carrierwave_direct is pretty much in charge.

Using storage :file in development would be a welcome feature for the carrierwave_direct gem. Does anyone know how to cleanly do this?

hellion
  • 4,602
  • 6
  • 38
  • 77

1 Answers1

2

I think it can be done as follows:

CarrierWave.configure do |config|
  if Rails.env.development? || Rails.env.test?
    config.storage = :file
    config.asset_host = ENV["dev_url"]
  else
    config.fog_provider = 'fog/aws'                        # required
    config.fog_credentials = {
      provider:              'AWS',                        # required
      aws_access_key_id:     ENV["aws_id"],                        # required
      aws_secret_access_key: ENV["aws_key"],                        # required
      region:                ENV["aws_zone"]                  # optional, defaults to 'us-east-1'
    }
    config.fog_directory  = ENV["aws_bucket"]                          # required
    config.max_file_size  = 600.megabytes # defaults to 5.megabytes
    config.use_action_status = true
    config.fog_public     = false                                        # optional, defaults to true
    config.fog_attributes = { cache_control: "public, max-age=#{365.day.to_i}" } # optional, defaults to {}
  end

end

And in your Uploader add in:

class SomeUploader < CarrierWave::Uploader::Base
  if Rails.env.development? || Rails.env.test?
    def store_dir
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  end
end
vishrutJha
  • 21
  • 5