-
-
Save bertomartin/a55e7ee993774a7029b8 to your computer and use it in GitHub Desktop.
Sir Trevor Image Uploader (Rails + Carrierwave)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/carrierwave.rb | |
require 'carrierwave/processing/mime_types' | |
require 'securerandom' | |
CarrierWave.configure do |config| | |
if Rails.env.test? | |
config.enable_processing = false | |
elsif !Rails.env.development? | |
config.storage = :fog | |
config.fog_credentials = { | |
:provider => 'AWS', | |
:aws_access_key_id => ENV['S3_KEY'], | |
:aws_secret_access_key => ENV['S3_SECRET'], | |
:region => 'eu-west-1' | |
} | |
config.fog_directory = ENV['S3_BUCKET'] | |
config.fog_public = true | |
end | |
end | |
class CarrierWave::Uploader::Base | |
include CarrierWave::MiniMagick | |
include CarrierWave::MimeTypes | |
process :set_content_type | |
process :optimize | |
def store_dir | |
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}".tap do |s| | |
s.prepend "test_" if Rails.env.test? | |
end | |
end | |
def filename | |
return unless original_filename | |
@random_filename ||= [model ? model_random_id : random_id, | |
file.extension].join('.') | |
end | |
def optimize | |
manipulate! do |img| | |
return img unless img.mime_type.match /jpe?g/ | |
img.strip | |
img.combine_options do |c| | |
c.quality "80" | |
c.depth "8" | |
c.interlace "plane" | |
end | |
img | |
end | |
end | |
private | |
def model_random_id | |
var = :"@#{mounted_as}_random_id" | |
model.instance_variable_get(var) || model.instance_variable_set(var, random_id) | |
end | |
def random_id | |
SecureRandom.urlsafe_base64(32) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Upload gems | |
gem "carrierwave" | |
gem "mini_magick", "~> 3.3" | |
gem "fog", "~> 1.3.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImagesController < AdminController | |
def create | |
uploader = SirTrevorImageUploader.new | |
if uploader.store! params[:attachment][:file] | |
render json: uploader.as_json, status: 200 | |
else | |
render :json => uploader.errors, status: 500 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... All your other routes | |
# This enables POST to /images | |
resources :images, only: [:create] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set the image upload path | |
SirTrevor.setDefaults({ | |
uploadUrl: "/images" | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SirTrevorImageUploader < CarrierWave::Uploader::Base | |
def store_dir | |
"uploads/editor".tap do |s| | |
s.prepend "test_" if Rails.env.test? | |
end | |
end | |
# Provide a default URL as a default if there hasn't been a file uploaded: | |
def default_url | |
# For Rails 3.1+ asset pipeline compatibility: | |
"/assets/" + [version_name, "default.gif"].compact.join('_') | |
end | |
version :large do | |
process resize_to_limit: [850, nil] | |
end | |
version :medium do | |
process resize_to_limit: [640, nil] | |
end | |
version :small do | |
process resize_to_limit: [320, nil] | |
end | |
def extension_white_list | |
%w(jpg jpeg gif png) | |
end | |
def as_json(options = nil) | |
{ | |
file: super["uploader"] | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment