Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Carrierwave config

View carrierwave_init.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS',
aws_access_key_id: ENV['S3_KEY'],
aws_secret_access_key: ENV['S3_SECRET']
}
config.fog_directory = "client-" + Rails.env
config.fog_public = true
config.fog_attributes = {'Cache-Control' => 'max-age=315576000'}
 
config.permissions = 0600 # ??
#config.directory_permissions = 0777
config.enable_processing = false # no processing photos are ready / true otherwise
 
if Rails.env.test? or Rails.env.cucumber?
config.storage = :file
config.enable_processing = false
end
 
end
View carrierwave_init.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
# encoding: utf-8
 
class ImageUploader < CarrierWave::Uploader::Base
# permissions 0600
 
before :store, :remember_cache_id
after :store, :delete_tmp_dir
 
# Include RMagick or ImageScience support:
# include CarrierWave::MiniMagick
 
# Choose what kind of storage to use for this uploader:
if Rails.env.production?
storage :fog
else
storage :file
end
 
def cache_dir
"#{Rails.root}/tmp/uploads"
end
 
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.pluralize.underscore}/#{mounted_as}/#{model.id}"
end
 
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
 
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
 
# Create different versions of your uploaded files:
# version :thumb do
# process :resize_to_fill => [112, 112]
# end
 
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg png)
end
 
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
 
# store! nil's the cache_id after it finishes so we need to remember it for deletition
def remember_cache_id(new_file)
@cache_id_was = cache_id
end
 
def delete_tmp_dir(new_file)
# make sure we don't delete other things accidentally by checking the name pattern
if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
FileUtils.rm_rf(File.join(cache_dir, @cache_id_was))
end
end
 
# remove the empty directory after the 'id' gets deleted
# todo recurse upwards and remove all the empty directories
def remove!
path = self.path
super
FileUtils.rmdir(File.dirname(path))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.