I'm trying to add the audio file to Cloudinary storage. I got stuck when
error show:
CloudinaryException (Invalid resource type audio):
app/controllers/musics_controller.rb:23:in `create'
I've added configuration in my Music.rb file from another help in stack overflow:
has_attached_file :song, :storage => :cloudinary, path: 'muvent/musics/:filename', :cloudinary_resource_type => :audio
validates_with AttachmentSizeValidator, attributes: :song, less_than: 10.megabytes
validates_attachment_content_type :song, :content_type => [ 'application/octet-stream', 'audio/mpeg', 'audio/x-mpeg', 'audio/mp3', 'audio/x-mp3', 'audio/mpeg3', 'audio/x-mpeg3', 'audio/mpg', 'audio/x-mpg', 'audio/x-mpegaudio' ]
But, I still can't upload the file to Cloudinary. I also added permit for song column in my controller music_params which the error come. How can I fix this?
Update
I've fixed the error above. But why another error in logs server show file -b --mime in rails? Gemfile I use in here paperclip and paperclip-cloudinary.
Please make sure to use the right resource_type (a Cloudinary parameter) according to the file's type. The default resource type is image. Set it to video when uploading audio/video files or raw when uploading non-media files (such as doc,csv,css,js, etc).
Related
I am working on an ecommerce website using Solidus, Rails. The site allows you to order photo frames & prints from a variety of options.
To print a photo a user must upload the jpg file of the photo. So, to allow that I modified the orders table and added a paperclip attachment called 'attachment'
I ran the following command
rails generate paperclip SpreeOrder attachment
Which generated the migrations, then I ran rake db:migrate
Then I created a spree/order_decorator.rb file, and added has_attached_file
module Spree::OrderDecorator
has_attached_file :attachment, styles: {
:medium => {
:geometry => "640x480",
:format => 'jpeg'
},
:thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
}, :processors => [:transcoder]
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
Spree::Order.prepend self
end
After this I ran the server, and ended up getting this error
undefined method `has_attached_file' for Spree::OrderDecorator:Module (NoMethodError)
I have configured solidus for use with paperclip only, so I am really confused as to why I am getting this error, even later I manually went and generated a paperclip.rb file in the config/initializers directory, but still I get the same error.
Please help with this!!
Thank You!!
You should add those paperclip method at class level in the prepended module:
def self.prepended(base)
base.has_attached_file
end
1 error prohibited this listing from being saved:
Image has contents that are not what they are reported to be
That's the error that I get when I try to upload a picture for a listing. I've tried various types of validations and nothing is working.
This is what my Model looks like.
class Listing < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "200x", :thumb =>"100x100>" }, :default_url => "default.jpg"
validates_attachment :image, content_type: { content_type: /\Aimage\/.*\Z/ }
end
Can someone please explain to me what I did wrong, and what I can do to fix it. I really want to keep working on this application but I've hit a problem!
You need to change the following
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
Also make sure that you are indeed uploading images with the mentioned extensions
It may be caused because you don't have file program (probably you don't use Unix system?)
file is used by paperclip to check whether sent file is what it should be or not (based on your validator).
If you are on Windows, you should manually download and install file for Windows and add it to either
Windows PATH environment variable,
project environments (config/environments/development.rb, add line Paperclip.options[:command_path] = '{your_path_to_dir_with_file/bin}' )
After that, restart console, server and it should work.
This is what you can do only for windows :
Install the exe by
download the exe from this link file.exe
test if is well installed by running your cmd and put the following instructions convert logo: logo.miff then run ' imdisplay logo.miff '
you will get custom logo image,that will pop up on your windows screen.
From here now you can start configuring everything on rails app
Open config/environments/development.rb
Add the following line: Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
If your rails server is currently running,brake the server and then run again rails s.After that you should be ready to go.Upload image on your app
I am adding user uploaded videos to my RoRs site with the help of the paperclip gem and s3 storage. For some reason that I can't figure out, whenever a user uploads an mp4 file, s3 sets content-type for that file as application/mp4 instead of video/mp4.
Note that I have registered mp4 mime type in an initializer file:
Mime::Type.lookup_by_extension('mp4').to_s
=> "video/mp4"
Here is the relevant part of my Post model:
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:path => "/video/:id/:filename"
validates_attachment_content_type :video,
:content_type => ['video/mp4'],
:message => "Sorry, this site currently only supports MP4 video"
What am I missing in my paperclip and/or s3 set-up.
####Update#####
For some reason that is beyond my knowledge of Rails, my default mime types for mp4 contained files is the following:
MIME::Types.type_for("my_video.mp4").to_s
=> "[application/mp4, audio/mp4, video/mp4, video/vnd.objectvideo]"
So, when paperclip send an mp4 file to s3, it seems to identify the file's mime type as the first default, "application/mp4". That is why s3 identifies the file as having a content-type of "application/mp4". Because I want to enable streaming of these mp4 files, I need paperclip to identify the file as having a mime type of "video/mp4".
Is there a way to modify paperclip (maybe in a before_post_process filter) to allow for this, or is there a way to modify rails through an init file to identify mp4 files as being "video/mp4". If I could do either, which way is best.
Thanks for your help
Turns out that I needed to set a default s3 header content_type in the model. This isn't the best solution for me because at some point I might start allowing video containers other than mp4. But it gets me moving on to the next problem.
has_attached_file :video,
:storage => :s3,
:s3_credentials => "#{Rails.root.to_s}/config/s3.yml",
:path => "/video/:id/:filename",
:s3_headers => { "Content-Type" => "video/mp4" }
I did the following:
...
MIN_VIDEO_SIZE = 0.megabytes
MAX_VIDEO_SIZE = 2048.megabytes
VALID_VIDEO_CONTENT_TYPES = ["video/mp4", /\Avideo/] # Note: The regular expression /\Avideo/ will match anything that starts with "video"
has_attached_file :video, {
url: BASE_URL,
path: "video/:id_partition/:filename"
}
validates_attachment :video,
size: { in: MIN_VIDEO_SIZE..MAX_VIDEO_SIZE },
content_type: { content_type: VALID_VIDEO_CONTENT_TYPES }
before_validation :validate_video_content_type, on: :create
before_post_process :validate_video_content_type
def validate_video_content_type
if video_content_type == "application/octet-stream"
# Finds the first match and returns it.
# Alternatively you could use the ".select" method instead which would find all mime types that match any of the VALID_VIDEO_CONTENT_TYPES
mime_type = MIME::Types.type_for(video_file_name).find do |type|
type.to_s.match Regexp.union(VALID_VIDEO_CONTENT_TYPES)
end
self.video_content_type = mime_type.to_s unless mime_type.blank?
end
end
...
A few system details:
Mac OS X Lion 10.7.2, Rails 3, Paperclip gem.
ImageMagick 6.7.3-0, binaries installed using MacPorts in /opt/local/bin.
Using WEBrick in development environment.
I'm trying to upload photos that should get resized down into thumbnails. When I do so, the original uploaded file is saved and can be accessed via HTTP correctly. When I try to accessed the thumbnail version, I get this error:
Routing Error
No route matches "[file_URL]"
Before that, when uploading a valid PNG file, I see this error in my WEBrick log:
[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: /var/folders/n4/62q22gb52rjd0h13cx_j8vv40000gq/T/stream20111020-24984-17560xt-0.png is not recognized by the 'identify' command.>
Doing which identify outputs:
/opt/local/bin/identify
Calling identify with the path of the uploaded file correctly identifies the file as a PNG file.
I confirmed rails server starts the development environment. I added the following in config/environments/development.rb:
Paperclip.options[:command_path] = "/opt/local/bin"
My Photo model has the following:
class Photo < ActiveRecord::Base
has_attached_file :file, :default_style => :view, :styles => {
:view => { :geometry => '520x390>', :format => 'jpg' },
:preview => { :geometry => '160x120>', :format => 'jpg' } }
validates_attachment_content_type :file,
:content_type => [ 'image/jpeg', 'image/pjpeg', 'image/png' ]
end
My Photos controller:
class PhotosController < ApplicationController
# ...
def create
#photo = Photo.create params[:photo]
end
end
Like I said, I can access the original images in the URL path system/files/:id/original, but the resized versions I want aren't accessible and are not found in the filesystem. WEBrick's log suggest Paperclip can't even have ImageMagick identify the images before resizing them.
Any ideas? Thanks in advance!
This may be a duplicate question, as stated above, but this problem was caused by a recent bug in ImageMagick 6.7.3-0 fixed soon after in 6.7.3-1. MacPorts has also been updated with the new release.
In 6.7.3-0, stating the frame number in the file path (e.g. identify /path/to/file.png[0]) caused ImageMagick to segfault ("Segmentation fault 11"). Paperclip specified the frame number to ensure its processing the first frame or page of an image file.
The new release of ImageMagick solved this problem. The bug was mentioned on MacPorts:
https://trac.macports.org/ticket/31643
I use the following code to create Assets from the uploaded files:
def upload
uploader = User.find_by_id(params[:uploader_id])
params[:assets].each do |file|
new_asset = uploader.assets.build(:asset => file) # Here the error appears
new_asset.save
end
...
end
I noticed that when I upload non-image files, e.g. my.xlsx, I got the following error:
[paperclip] identify -format %wx%h "C:/temp/stream20110628-460-3vqjnd.xlsx[0]" 2>NUL
[paperclip] An error was received while processing:
#<Paperclip::NotIdentifiedByImageMagickError: C:/temp/stream20110628-460-3vqjnd.xlsx is
not recognized by the 'identify' command.>
(For image files everything works fine: a thumbnail is created, and there is no error.)
Is that because Paperclip tries to create a thumbnail from my.xlsx ?
What configuration will create thumbnails only for image files ?
Here is some relevant code:
class Asset < ActiveRecord::Base
belongs_to :uploader, :class_name => "User"
has_attached_file :asset, :styles => { :thumb => "80x80#" }
end
I used the following nice solution:
before_post_process :image?
def image?
(asset_content_type =~ SUPPORTED_IMAGES_REGEX).present?
end
where:
SUPPORTED_IMAGE_FORMATS = ["image/jpeg", "image/png", "image/gif", "image/bmp"]
SUPPORTED_IMAGES_REGEX = Regexp.new('\A(' + SUPPORTED_IMAGE_FORMATS.join('|') + ')\Z')
Change the has_attached_file line to read:
has_attached_file :asset, :styles => { :thumb=> "80x80#" }, :whiny_thumbnails => false
This will prevent it from raising an error when thumbnails are not created. Note though that it won't raise errors if one occurs when processing an image though.