Using CarrierWave and S3 with Mercury Editor
I've been using Mercury Editor in a new project to get editable text directly in the browser. By default, it stores images locally, but we wanted to put them on Amazon S3 instead. Here's what that took using CarrierWave:
Add a model to hold the images
class BlogImage < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
Not much to see here, just a single string attribute to hold the image info.
Add an uploader
require 'carrierwave/processing/mime_types'
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
include CarrierWave::MimeTypes
process :set_content_type
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
Obviously you could process extra versions or limit extensions here, but all Mercury needs is someplace to dump what it takes in.
Replace the Mercury Images Controller
To hook everything up, you just need to replace the images controller in the engine with app/controllers/mercury/images_controller.rb in your project:
class Mercury::ImagesController < MercuryController
respond_to :json
def create
@blog_image = BlogImage.new(params[:image])
@blog_image.save!
render :json => @blog_image.to_json(:only => :image)
end
def destroy
@image = BlogImage.find(params[:id])
@image.destroy
respond_with @image
end
end