Skip to content

Instantly share code, notes, and snippets.

@kyriacos
Created June 29, 2012 04:13
Show Gist options
  • Save kyriacos/3015696 to your computer and use it in GitHub Desktop.
Save kyriacos/3015696 to your computer and use it in GitHub Desktop.
Carrierwave config
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
# 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