Created
April 24, 2013 01:57
-
-
Save dnagir/5449012 to your computer and use it in GitHub Desktop.
Uploading cloudinary files using DelayedJob without a shared file system. It stores the binary in the database along with DJ and will have some impact on that. So the DJ table needs to be updated to accomodate that (such as making it `UNLOGGED` etc)
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
module Jobs | |
class CloudinaryUpload < Struct.new(:uploader_class, :model, :field, :file_name, :content_type, :data) | |
include Logging | |
def upload | |
uploader = uploader_class.new(model, field) | |
store = Cloudinary::CarrierWave::Storage.new(uploader) | |
io = Cloudinary::Blob.new(data, original_filename: file_name, content_type: content_type) | |
uploader.cache!(io) | |
store.store!(uploader.file) | |
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
class FileUploader < CarrierWave::Uploader::Base | |
include Cloudinary::CarrierWave | |
class BackgroundStore < Cloudinary::CarrierWave::Storage | |
def store!(file) | |
job = Jobs::CloudinaryUpload.new(uploader.class, uploader.model, uploader.mounted_as, | |
file.original_filename, file.content_type, file.read) | |
# Ensure no binary data is logged, otherwise it'll clog the log and kill the app | |
without_query_logging { job.delay.upload } | |
end | |
def without_query_logging | |
prev = ActiveRecord::Base.logger | |
ActiveRecord::Base.logger = nil | |
yield | |
ensure | |
ActiveRecord::Base.logger = prev | |
end | |
end | |
# Comment this out to disable BG processing | |
storage BackgroundStore | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@TalLevAmi thanks, will be keen to see if you have better ideas.