Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sir Trevor Image Uploader (Rails + Carrierwave)
# 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
# Upload gems
gem "carrierwave"
gem "mini_magick", "~> 3.3"
gem "fog", "~> 1.3.1"
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
# ... All your other routes
# This enables POST to /images
resources :images, only: [:create]
// Set the image upload path
SirTrevor.setDefaults({
uploadUrl: "/images"
});
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
@iscooptoday

This comment has been minimized.

Copy link

iscooptoday commented Oct 15, 2014

The image uploader dosent seem to work in my rails app
. It says there was a problem uploading this image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.