-
-
Save m4tm4t/e12a64598ac507683ff2 to your computer and use it in GitHub Desktop.
Mongoid + carrierwave backgrounder + sidekiq
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 Admin::Image | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
## Carrierwave ## | |
mount_uploader :file, AdminImageUploader | |
process_in_background :file | |
## Fields ## | |
field :file, type: String | |
field :file_processing, type: Boolean | |
field :illustrable, type: Boolean, default: nil | |
## Validations ## | |
validates :file, presence: true | |
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
class Admin::ImagesController < AdminController | |
before_filter :load_resource | |
respond_to :js, :html, :json | |
def create | |
resource_param = @resource.to_s.underscore.split("/").join("_") | |
@image = @resource.new(params[resource_param.to_sym]) | |
@image.process_file_upload = true | |
if @image.save | |
respond_with(@image) | |
else | |
end | |
end | |
private | |
def load_resource | |
allowed = {"images" => Admin::Image, "screenshots" => Ga::GameShot} | |
if params[:what] && allowed.has_key?(params[:what]) | |
@resource = allowed[params[:what]] | |
else | |
# Error | |
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
# encoding: utf-8 | |
class AdminImageUploader < CarrierWave::Uploader::Base | |
include CarrierWave::Backgrounder::Delay | |
include CarrierWave::RMagick | |
storage :file | |
def store_dir | |
if Rails.env == "test" | |
"specs/uploads/redaction/images/#{model.id}" | |
else | |
"uploads/redaction/images/#{model.id}" | |
end | |
end | |
version :thumbnail do | |
process :resize_to_fill => [70, 50] | |
end | |
version :content_thumb do | |
process :resize_to_fill => [140, 80] | |
end | |
version :actu, if: :is_illustrable? do | |
version :big_slider do | |
process :resize_to_fill => [651, 334] | |
end | |
version :tophome do | |
process :resize_to_fill => [250, 150] | |
end | |
version :big_banderolle do | |
process :resize_to_fill => [610, 200] | |
end | |
version :banderolle do | |
process :resize_to_fill => [300, 100] | |
end | |
end | |
def extension_white_list | |
%w(jpg jpeg gif png) | |
end | |
private | |
def is_illustrable?(file) | |
return model.illustrable unless model.illustrable.nil? | |
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
CarrierWave.configure do |config| | |
config.cache_dir = "#{Rails.root}/tmp/uploads" | |
if Rails.env == "test" | |
config.storage = :file | |
# config.enable_processing = false | |
else | |
config.storage = :fog | |
config.fog_credentials = { | |
:provider => 'AWS', | |
:aws_access_key_id => '***', | |
:aws_secret_access_key => '***', | |
:region => 'eu-west-1' | |
} | |
config.fog_directory = 'static.playdoria.net' | |
config.fog_host = 'http://static.playdoria.net' | |
# config.fog_host = proc do |file| | |
# identifier = (rand(100)%4) | |
# "http://static#{identifier}.playdoria.net" | |
# end | |
config.fog_public = true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have exaclty the same setup and unfortunately the same bug. My sidekiq doesnt get called.
I've also tried to use the after_create callback to enque to sidekiq. But this doesn't help since the file isn't uploaded when processing the file.
Another idea is to use the carrierwave uploader callbacks (https://github.com/jnicklas/carrierwave/wiki/How-to%3A-use-callbacks)
But in this case you are in the uploader. So self is not your model. You may find a way how to get to a reference to this?
I've already openend an issue for this (carrierwaveuploader/carrierwave#856). If you find a way please post it there :)
I let you know if I find a way to solve this. Please let me know if you do ;)