Ruby on rails bootsy + cloudinary upload image trouble - ruby-on-rails

I have RoR project, living on heroku. I have bootsy (editor with image upload funcs) and I have cloudinary.
Ive setup uploader, cloudinary api keys and initializers (can show you, if it needed). Now, when I try to upload image in bootsy - it creates database row, and create image in cloudinary. But in js window from bootsy, there empty <img>
ruby '2.3.1'
gem 'rails', '~> 5.1.1'
gem 'bootsy'
gem 'carrierwave'
gem 'fog'
gem 'cloudinary', '~> 1.8.1'
1) uploaders/bootsy/image_uploader.rb
module Bootsy
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# storage Bootsy.storage
include Cloudinary::CarrierWave
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :large do
process :eager => true
process resize_to_fit: [
700, 700
]
end
version :medium do
process :eager => true
process resize_to_fit: [
300, 300
]
end
version :small do
process :eager => true
process resize_to_fit: [
150, 150
]
end
version :thumb do
process :eager => true
process resize_to_fit: [
150, 150
]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
end
2) initializers/bootsy.rb
Bootsy.setup do |config|
config.image_versions_available = [:small, :medium, :large, :original]
config.storage = :fog
end
3) models/article.rb
class Article < ApplicationRecord
include Bootsy::Container
mount_uploader :image, Bootsy::ImageUploader
mount_uploader :main_image, ArticleImageUploader
mount_uploader :list_image, ArticleImageUploader
end
P.S Ok, I really have no idea - I just repeat this bug in public repository. https://bitbucket.org/dekakisalove/bootsy_tes/ I will add bounty to this question as soon as it will be possible.

This problem is due to an incorrect return value the method store! of class Cloudinary::CarrierWave::Storage
To work around this problem, you can use several variants, for example:
like this in config/initializers/cloudinary_store.rb
module CloudinaryStorage
def store!(file)
super || uploader.metadata
end
end
ActiveSupport.on_load :after_initialize do
Cloudinary::CarrierWave::Storage.prepend CloudinaryStorage
end
or like this inapp/uploaders/image_uploader.rb
module Bootsy
class ImageUploader < CarrierWave::Uploader::Base
after :store, :reload_data
def reload_data(file)
model.reload
end
# etc..

Related

carrierwave recreate_versions! corrupts original image

Love carrierwave.
When running the recreate_version! the quality of ORIGINAL image is dramatically reduced/corrupted.
I need to use carrierwave's recreate_version! to add a new "mobile" version to an existing Photo model via the mount_uploader :image.
Existing version: original, :card and :thumb
Again, when running the recreate_version! the quality of ORIGINAL image is dramatically reduced.
Mayor image corruption of the original to the point the image should be discarded and re-uploaded.
The :mobile version which is created new from the original is of excellent quality.
And the :card and :thumb versions remain the same, no difference.
Just the original is corrupted. Weird.
As a test, I re-ran the 'photo.image.recreate_versions!(:mobile)' several times to test different parameters trying to catch the corruption culprit (removed un-need gems, recreate :thumb version, etc.). But each time the quality of the original image got worst and worst.
And the higher the dpi the quicker the corruption: 300dpi first pass super ugly, 100dpi two passes yuck, and 72dpi after three passes is just criminal.
Corruption happens on localhost, as well as, Heroku (both staging and production).
Weird. I have used Carrierwave for years and this is the first time with such a problem.
ruby '2.4.1'
rails '5.1.7'
gem 'carrierwave', :git => 'https://github.com/carrierwaveuploader/carrierwave.git'
Here is the setup.
I pass the call to carrierwave recreate_version through Delayed_Job one user at a time
<% #user.photos.find_each do |photo| %>
<% photo.delay.recreate_mobile %>
<% end %>
and in the Photo model
class Photo < ApplicationRecord
def recreate_mobile
self.image.recreate_versions!(:mobile) if self.image?
end
end
and in the ImageUploader
class ImageUploader < CarrierWave::Uploader::Base
include ::CarrierWave::Backgrounder::Delay
include CarrierWave::MiniMagick
include CarrierWave::BombShelter
include CarrierWave::Processing::MiniMagick
storage :aws
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
process resize_to_limit: [1350, 1350]
process :store_dimensions
version :mobile do
process resize_to_limit: [400, 600]
end
version :card do
process resize_to_limit: [300, 460]
end
version :thumb, from_version: :card do
process resize_to_limit: [100, 150]
end
private
def store_dimensions
if file && model
model.width, model.height = ::MiniMagick::Image.open(file.file)[:dimensions]
end
end
def max_pixel_dimensions
[6024, 6024]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
and the gems
gem 'carrierwave-aws'
gem 'carrierwave', :git => 'https://github.com/carrierwaveuploader/carrierwave.git'
gem 'carrierwave_backgrounder'
gem 'carrierwave-bombshelter'
gem 'carrierwave-processing'
gem 'mini_magick'
gem 'remotipart', '~> 1.2'
Removed ALL of the unnecessary gems. Still a image corruption issue.
Quality of the original image and all versions are excellent.
It is just after running recreate_version the original image is corrupted.
Any ideas on how to prevent the original image quality from corrupting after recreate_version?
Solved it: switch processing to RMagick
Within the carriewave image_uploader.rb file
replace:
include CarrierWave::MiniMagick
include CarrierWave::Processing::MiniMagick
with:
include CarrierWave::RMagick
include CarrierWave::Processing::RMagick
MiniMagick is considered to have better memory management, but is rather outdated. Plus is corrupted the images. Fingers crossed RMagick has better memory management by now.
RMagick for the win!

heroku with carrierwave dropbox storage and rails showing always same image

so I have a web application that runs fine in development, and with carrierwave and imagemagick I make some changes on the photos that I need to upload. The problem is that when on heroku when i ask for the main version of the photo it still gives me the thumb version
class BannerUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
if Rails.env.development?
storage :file
else
storage :dropbox
end
def store_dir
if version_name.nil?
"uploads/#{model.class.to_s.underscore}/#{mounted_as}"
else
"uploads/#{model.class.to_s.underscore}/#{mounted_as}_#{version_name}"
end
end
process convert: :jpg
process :crop
process resize_to_limit: [1200, 260]
def crop
if model.crop_x.present?
manipulate! do |img|
x = model.crop_x.to_i
y = model.crop_y.to_i
w = model.crop_w.to_i
h = model.crop_h.to_i
r = model.crop_r.to_i
img.rotate r
img.crop([[w, h].join('x'), [x, y].join('+')].join('+'))
end
end
end
version :thumb do
process resize_to_fill: [640, 320]
end
def extension_whitelist
%w(jpg jpeg png)
end
def filename
if original_filename
"#{secure_token}.#{file.extension}"
elsif file
file.filename
end
end
protected
def secure_token
var = :"##{mounted_as}_secure_token"
model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
end
end
this is my uploader
then in my views i have image_tag(model.banner_url(:thumb) to get the thumb version and image_tag #model.banner_url to get the large version. The problem is that the second one on my local machine runs just fine, but when on heroku it gives me the same image of the first one. It does create the right folders and files, and it does crop them right, but it doesn't retrieve the correct one. I am using the
gem 'dropbox-sdk-v2', '~> 0.0.3'
gem 'carrierwave-dropbox', '~> 2.0'
as heroku storage, with obviously a dropbox account
Often you'll want to add different versions of the same file. The
classic example is image thumbnails. There is built in support for
this*
I think the problem is the way you define the store_dir method.
Define it this way:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
And also CarrierWave documentation recommends to use the uploader this way:
uploader = AvatarUploader.new
uploader.store!(my_file) # size: 1024x768
uploader.url # => '/url/to/my_file.png' # size: 800x800
uploader.thumb.url # => '/url/to/thumb_my_file.png' # size: 200x200
Source: https://github.com/carrierwaveuploader/carrierwave

Rails4: CKEditor gsub error

I have a project in rails 4 that uses ckeditor with cloudinary. The uplaod to Cloudinary works fine, but after the upload, when the editor should lad the image, I get the error:
NoMethodError - undefined method `gsub' for nil:NilClass:
My CKEditor Picture Uploader is:
# encoding: utf-8
class CkeditorPictureUploader < CarrierWave::Uploader::Base
include Ckeditor::Backend::CarrierWave
include Cloudinary::CarrierWave
include CarrierWave::MiniMagick
[:extract_content_type, :set_size, :read_dimensions].each do |method|
define_method :"#{method}_with_cloudinary" do
send(:"#{method}_without_cloudinary") if self.file.is_a?(CarrierWave::SanitizedFile)
{}
end
alias_method_chain method, :cloudinary
end
process :read_dimensions
# Create different versions of your uploaded files:
version :thumb do
process :resize_to_fill => [118, 100]
end
version :content do
process :resize_to_limit => [800, 800]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
Ckeditor.image_file_types
end
end
When I go to upload and click on "find image on server" the image is there and I can load the image on editor, but not when I upload image to server
Someone got this problem before?
Thanks!
This error occurs because gsub could not find url for Uploaded image in ckeditor. You have to create ckeditor asset_response.rb file in your lib/ckeditor folder of your App and put this line of code
def asset_url(relative_url_root)
#ckeditor_assets = Ckeditor::Picture.last.url_content
puts #ckeditor_assets.inspect
#return nil if asset.url_content.nil?
url = #ckeditor_assets #Ckeditor::Utils.escape_single_quotes(asset.url_content)
if URI(url).relative?
"#{relative_url_root}#{url}"
else
url
end
end
put the whole code and replace asset_url method with this.
This is just a hack to include Ckeditor::Picture model.
Put this code for cloudinary in CkeditorPictureUploader file
include Ckeditor::Backend::CarrierWave
include Cloudinary::CarrierWave
process :tags => ["photo_album_sample"]
process :convert => "jpg"
version :thumbnail do
eager
resize_to_fit(200, 200)
cloudinary_transformation :quality => 80
end
From #t-s 's answer, I found that in Ckeditor::AssetResponse#asset_url
method, the asset object is not reloaded, so asset.content_url will always be nil thus caused the error. I fixed it like this:
class Ckeditor::Picture < Ckeditor::Asset
...
def url_content
url(:content) || begin
if persisted?
reload
url(:content)
end
end
end
end
And similarly for Ckeditor::AttachmentFile class if you have it.

carrierwave .store method throwing errors unknown regexp options #model=nil #mounted_as=nil

I have a brand model
class Brand < ActiveRecord::Base
attr_accessible :activities, :attendees, :date, :description, :name, :place, :requirements_on_event, :requirements_other, :requirements_post_event, :requirements_pre_event, :target_students, :target_universities, :type, :image
mount_uploader :image, ImageUploader
end
I have the following in my brands table
t.string "image"
and here is my image_uploader.rb
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::MiniMagick
include CarrierWave::RMagick
# include ::CarrierWave::Backgrounder::Delay
# Choose what kind of storage to use for this uploader:
storage :file
#storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
I have these in my gemfile
#Picture Upload and Storage
gem 'carrierwave'
gem 'carrierwave_backgrounder'
gem 'fog'
#gem 'aws-s3'
gem 'rmagick'
gem 'mini_magick
Now the problem is per the doc on carrierwave's github page
I try
bundle exec rails c and rails c
uploader = ImageUploader.new
I am getting this returned message in green
=> #<ImageUploader:0x007fcf2450bf68 #model=nil, #mounted_as=nil>
why is the #model=nil and #mounted_as=nil?
when I try to this
uploader.store!(/Users/judyngai/Desktop/brandspictures/circle_accupass.png)
I am getting this error
SyntaxError: (eval):2: unknown regexp options - jdyga
and if I try this
uploader.store!('/Users/judyngai/Desktop/brandspictures/circle_accupass.png')
carrierwave won't let me do it
CarrierWave::FormNotMultipart: You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.
I feel like I installed everything correctly. Its a rails 3.2.13 app. I commented out my carrierwave.rb because I am having trouble with fog and aws.
I just tried adding this to my model but still getting the same thing.
require 'carrierwave/orm/activerecord'
You should be calling store! method as below:
uploader.store!(File.open('/Users/judyngai/Desktop/brandspictures/circle_accupass.png'))
You should be passing an instance of File rather than a String. Hence, the error You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.

exif image rotation issue using carrierwave and rmagick to upload to s3

I've got a photo upload feature in my rails app. The app uploads direct to s3 through carrierwave via rmagick and fog. The issue I am having is when a photo is uploaded via mobile through the "take a photo option" in portrait (note this is with iphone but I believe android has the same issue). Once uploaded the image appears fine on mobile, however when viewed on desktop the image appears rotated 90 degrees.
Through my research it looks to be an issue with exif. This stackoverflow responder outlines 2 potential solutions. This gist also looks promising as well.
So far I have found a few solutions posted but none have worked. Ideally I would like the photo to be saved to s3 as a portrait, then just display the image as is.
Any suggestions are well appreciated.
Below is my code
app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
include CarrierWave::RMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
include CarrierWave::MimeTypes
process :fix_exif_rotation
process :set_content_type
version :thumb do
process resize_to_fill: [200, 200]
end
def extension_white_list
%w(jpg jpeg png)
end
def fix_exif_rotation #this is my attempted solution
manipulate! do |img|
img = img.auto_orient!
end
end
end
app/models/s3_image.rb
class S3Image < ActiveRecord::Base
attr_accessible :image, :name, :user_id
mount_uploader :image, ImageUploader
belongs_to :user
def image_name
File.basename(image.path || image.filename) if image
end
class ImageWorker
include Sidekiq::Worker
def perform(id, key)
s3_image = S3Image.find(id)
s3_image.key = key
s3_image.remote_image_url = s3_image.image.direct_fog_url(with_path: true)
s3_image.save!
s3_image.update_column(:image_processed, true)
end
end
end
config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: " ... ",
aws_secret_access_key: " ... "
}
config.fog_directory = " ... "
end
btw I used this Railscast as a guide for setting up my s3 upload.
Well I got this working using fog instead or carrierwave_direct.
Below is the code that ended up working for me:
app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def fix_exif_rotation #this is my attempted solution
manipulate! do |img|
img.tap(&:auto_orient)
end
end
process :fix_exif_rotation
end
app/models/s3_image.rb
class S3Image < ActiveRecord::Base
attr_accessible :image, :name, :user_id, :image_cache
mount_uploader :image, ImageUploader
belongs_to :user
end
initializers/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: " ... ",
aws_secret_access_key: " ... ",
region: 'us-west-2'
}
config.fog_directory = " ... "
end
I had a similar problem and fixed it with an approach nearly identical to yours.
# In the uploader:
def auto_orient
manipulate! do |img|
img = img.auto_orient
end
end
(Note that I am not calling auto_orient! - just auto_orient, without the bang.)
Then I have process :auto_orient as the first line of any version I create. For example:
version :square do
process :auto_orient
process :resize_to_fill => [600, 600]
end
My solution (quite similar to Sumeet) :
# painting_uploader.rb
process :right_orientation
def right_orientation
manipulate! do |img|
img.auto_orient
img
end
end
It's really important to return an image. Otherwise, you'll get an
NoMethodError (undefined method `write' for "":String):
Lando2319's answer was not working for me.
I am using RMagick.
I managed to make ImageMagick apply the correct orientation (and to reset the EXIF rotation data in order to avoid a double rotation by the viewer) by using :
def fix_exif_rotation # put this before any other process in the Carrierwave uploader
manipulate! do |img|
img.tap(&:auto_orient!)
end
The difference between my solution & Lando's is the bang (!). In my case it was absolutely necessary.

Resources