Skip to content

Instantly share code, notes, and snippets.

@joshuap
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save joshuap/8febfb9d14ab74fb436c to your computer and use it in GitHub Desktop.

Select an option

Save joshuap/8febfb9d14ab74fb436c to your computer and use it in GitHub Desktop.
CarrierWave
# encoding: utf-8
# config/initializers/carrier_wave.rb
CarrierWave.configure do |config|
config.storage = :fog
config.fog_credentials = {
:provider => 'AWS',
:region => 'us-east-1',
:aws_access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:aws_secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
config.fog_directory = ENV['FOG_DIRECTORY']
config.fog_public = false
config.asset_host = ENV['FOG_HOST']
end
# File uploads
gem 'carrierwave'
# Image processing
gem 'mini_magick'
# S3 file storage
gem 'fog'
# encoding: utf-8
# app/uploads/photo_uploader.rb
require 'carrierwave/processing/mime_types'
require 'carrierwave/processing/mini_magick'
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include CarrierWave::MimeTypes
# https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Store-the-uploaded-file-size-and-content-type
process :set_content_type
# If there will be a ton of files, see:
# https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Make-a-fast-lookup-able-storage-directory-structure
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def fog_public
true
end
version :large do
process :resize_to_limit => [420,260]
end
version :thumb do
process :resize_to_limit => [200,200]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment