carrierwave content_type always nil - ruby-on-rails

I am developing Rails 3.2.9 app and using Carrierwave as file uploader. The Carriverwave readme point out the way to get correct content_type:
Add require 'carrierwave/processing/mime_types' to an initializer or your uploader(s).
Add include CarrierWave::MimeTypes to your uploader.
Add process :set_content_type to your uploader(s).
Base on this, My uploader is below:
# encoding: utf-8
require 'carrierwave/processing/mime_types'
class AttachmentUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
storage :file
def store_dir
"#{base_store_dir}/#{model.id}"
end
process :set_content_type
end
In my model, mount the uploader as file:
mount_uploader :file, AttachmentUploader
However, I always got content_type nil after upload file:
1.9.3-p327 :013 > a.file.class
=> AttachmentUploader
1.9.3-p327 :010 > a.file.file
=> #<CarrierWave::SanitizedFile:0x00000004046330 #file="uploads/course/000/000/026/attachment_file/6/myIcon.png", #original_filename=nil, #content_type=nil>
Any suggestion? Thanks.
PS: I already added gem "mime-types", "~> 1.19" in my Gemfile.

You will need to follow the instructions laid out here: https://github.com/carrierwaveuploader/carrierwave#setting-the-content-type
Add the mime-types gem, then setup your uploader file like so
require 'carrierwave/processing/mime_types'
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::MimeTypes
process :set_content_type
end

Had the same problem tried this in my Model file where I mounted the Uploader
before_save :set_mime_type
def set_mime_type
self.mimetype = Mime::Type.lookup_by_extension(File.extname(self.cf_filename.to_s)[1..-1])
end
Note: You need to have a mimetype field in the table

I just hit the exact same problem and couldn't find an easy fix.
My workaround was to add a content_type column to the model and set it in the create/update process with
#model.content_type = params[:file_upload][:attachment].content_type
This works, though hopefully the issue gets fixed.

Related

Issue with Carrierwave_Direct + Rails

video_uploader.rb
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
alias_method :extension_white_list, :extension_whitelist
def will_include_content_type
true
end
default_content_type 'video/mp4'
allowed_content_types %w(video/mpeg video/mp4 video/ogg)
end
Model.rb
class Video < ActiveRecord::Base
mount_uploader :videosub, VideoUploader
end
Parameters:
"videosub"=>#<ActionDispatch::Http::UploadedFile:0x007f87201e28 #tempfile=#<Tempfile:/tmp/RackMultipart20170509-4704-1mjrwq.mp4>, #original_filename="168C7704-4337-A870-007B2CB22519.mp4", #content_type="video/mp4
Error showing is:
Validation failed: Videosub is invalid.
And if I replace the code inside VideoUploader as:
class VideoUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
alias_method :extension_white_list, :extension_whitelist
storage :fog
end
Then the file is being successfully being uploaded to S3. But in my case I need to upload the video in background and directly to S3 bypassing the server on which the app is hosted.
Please help!
Found the solution:
Earlier I was including the gem by defining the github path of the repo. Later on I just eliminated the path. And it worked.

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.

cant see the aws-s3 url saved in rails database

I have successfully implemented image uploading using carrierwave, fog and Amazon S3. In my imageuploader am using only fog as storage. But when i check my database i can see that just the file name is written instead of the amazon url. In my views its fetching correctly from aws without any issues.
Is it supposed to be like this?
If so how the application figure out the exact url to s3?
imageuploader.rb`
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
include CarrierWave::MiniMagick
#Include the sprockets-rails helper for Rails 4+ compatibility:
include Sprockets::Rails::Helper
storage :fog
version :index_size do
process :resize_to_fill => [258, 173]
end
version :thumb_size do
process :resize_to_fill => [100, 100]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
Your config has the bucket name and the database has the filename. These are the only two pieces of information that are actually required to construct a filename (and it can be done without other API calls). The urls are actually pretty regular, so it is fairly straightforward for the server to do this. Hope that helps!

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.

How can I use Carrierwave with cloudinary in seeds file

I want to upload multiple images to cloudinary by association of a carrierwave active record. How can I do this in a seeds file, using an array of remote urls?
Ive read countless articles how to use carrierwave/cloudinary helper tags to target an image upload within an html form, but nothing on doing this directly within code, any ideas?
That's not so hard to do at all.
So what I did:
# Gemfile
gem 'cloudinary'
gem 'carrierwave'
#config/cloudinary.yml
development:
cloud_name: "carrierwave-example"
api_key: "YOUR CLOUDINARY CREDENTIALS"
api_secret: "YOUR CLOUDINARY CREDENTIALS"
# in your uploader
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave #include cloudinary lib
# storage :file - comment this out
# def store_dir - comment this too
# "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
# end
end
# You model, that uses uploader
class Picture < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
After writting this simple staff you can create Pictures, that will store images in clodinary like this:
Picture.create(image: "/path/to/image")
or if you have remote links of images, U just itterate through them
["http://image1.jpg","http://image2.jpg","http://image3.jpg"].each do |link|
Picture.create(remote_image_url: link)
end
Just remember to use remote_#{your_column_name}_url if you have remote link

Resources