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.
Related
I'm using carrierwave (0.10.0) with Fog for S3 uploads.
Implementing simple uploader:
class AvatarUploader < CarrierWave::Uploader::Base
def default_url(*_args)
"#{Rails.configuration.cdn_url}/no-image.png"
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
and mount it here
class User
include Mongoid::Document
field :name, type: String
mount_uploader :avatar, AvatarUploader
end
When Im trying to set remote image even if no image is presented it doesn't fallback to default_url and simulates generation of file in S3 which is actually not there.
Example:
u = User.find('1234567')
u.remote_avatar_url = 'http://non-existing-url.com/avatar.jpg'
u.save!
And when I access after reloading document
u.reload
u.avatar.url # output https://ourcdn.url/somebucket/avatar.jpg
But when access it in the browser it's showing 404.
Any ideas?
I installed carrierwave gem in my rails app.
My uploader class
class VoiceUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
My model class
require 'carrierwave/orm/activerecord'
class PulseFeedback < ActiveRecord::Base
belongs_to :tablet
mount_uploader :voice, VoiceUploader
end
But whenever I use store method in my rails console I get this error.
2.2.0 :001 > v = VoiceUploader.new
=> #<VoiceUploader:0x000000038dc8b0 #model=nil, #mounted_as=nil>
2.2.0 :002 > v.store!("/home/raghu/Music/ajeeb.mp3")
CarrierWave::FormNotMultipart: CarrierWave::FormNotMultipart
from /home/raghu/.rvm/gems/ruby-2.2.0/gems/carrierwave-0.10.0/lib/carrierwave/uploader/cache.rb:120:in `cache!'
from /home/raghu/.rvm/gems/ruby-2.2.0/gems/carrierwave-0.10.0/lib/carrierwave/uploader/store.rb:56:in `store!'
I also don't understand why model and mounted are nil even though I have defined mounter in my model class
You should store a file, not a string
class VoiceUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads"
end
end
v = VoiceUploader.new
File.open("/home/raghu/Music/ajeeb.mp3") do |f|
v.store!(f)
end
Or try to use model for that
PulseFeedback.create({
voice: File.open("/home/raghu/Music/ajeeb.mp3")
})
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.
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
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.