I am using paperclip to upload files in my rail4 project. Now I need to apply some condition based on the name of controller action that is currently uploading the attachment file.
Can someone please help me get the name of currently used action in model.
Thanks in advance.
UPDATE:
Here is my code in modal file:
has_attached_file :attachment, :styles => lambda { |attachment| {:medium => attachment.instance.video? ? { :geometry => "236x236>", :format => 'jpg', :time => 10 } : { :geometry => "236x236>" }, :thumb => attachment.instance.video? ? { :geometry => "150x150>", :format => 'jpg', :time => 10 } : { :geometry => "150x150>" },:large => attachment.instance.video? ? { :geometry => "1000x700", :format => 'jpg', :time => 10 } : { :geometry => "1000x700" }} },:processors => lambda { |a| a.video? ? [ :transcoder ] : [ :cropper ] }
before_post_process :is_audio?
def is_audio?
return false if [ 'audio/mp3','audio/mpeg'].include?(attachment.content_type)
end
Now I want to skip processors and styles and keep only the original file (just like here in case of audio file) if file is uploaded from update action. Please suggest.
Related
I used paperclip background process with delay jobs with ffmpeg.
In paperclip processors folder i have ffmpeg.rb file also.
my model
class product < ActiveRecord::Base
has_attached_file :video, :styles => {
:mp4video => { :geometry => "640x480", :format => 'mp4', :convert_options => {:output => {:ar => 44100}} },
:webmvideo =>{ :geometry => "1024x576", :format => 'webm', :convert_options => {:output => {:ar => 44100}} },
:oggvideo => { :geometry => "1024x576", :format => 'ogg', :convert_options => {:output => {:ar => 44100}} },
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
},
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/aws.yml",
:processors => [:transcoder]
process_in_background :video
end
I'm using gem 'paperclip-av-transcoder', "0.6.2" for my video processing.
I simply need to write this code to have my video converted into many formats:
has_attached_file :video, :styles => {
:mp4 => { :format => 'mp4', :convert_options => { :output => { :vcodec => 'libx264', :acodec => 'copy' } } },
:ogg => { :format => 'ogg', :auto_rotate => true },
:webm => { :format => 'webm', :auto_rotate => true },
:flv => { :format => 'flv', :convert_options => { :output => { :ar => 44100 } }, :auto_rotate => true },
:thumb => { :geometry => "300x300#", :format => 'jpg', :time => 1, :auto_rotate => true }
}, :processors => [:transcoder]
But this code is synchronous and the user that upload the video is stuck during the conversion.
I would like to add an async wrapper (worker) around :transcoder so I can call:
has_attached_file :video, :styles => {... }, :processors => [:wrapper_transcoder_worker]
I have installed resque and redis and everything work.
Do you have an idea on how can I write the asynch worker around transcoder?
Have you tried delayed_paperclip gem? It pushes all processing into background and supports resque (among others).
lock paperclip at version 4.2.0
I want to update my styles in paperclip, but when i run the command, a error is returned. Here is the code of my styles:
image.rb
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
validates_presence_of :image
has_attached_file :image, :processors => [:ffmpeg],
:styles => { :video => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "100x100>", :format => 'jpg' },
:large => { :geometry => "400x300>", :format => 'jpg' },
:medium => { :geometry => "400x200>", :format => 'png' },
:icon => { :geometry => "36x36>", :format => 'jpg' },
:profile => { :geometry => "28x28>", :format => 'jpg' } },
:default_url => "/images/:style/missing.png"
end
Returned Error:
Regenerating Image -> image -> [:icon, :large, :medium, :profile, :thumb, :video]
rake aborted!
Cocaine::CommandNotFoundError
How i solve this error?
It seems to be that you have not installed the "paperclip-ffmpeg" gem.
See this link: https://github.com/owahab/paperclip-ffmpeg#requirements
When I try to upload a video using Paperclip, I get an error message can't dump File.
Model Video :
class Video < ActiveRecord::Base
has_attached_file :avatar,
:storage => :s3,
:styles => {
:mp4 => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "300x300>", :format => 'jpg', :time => 5 }
}, :processors => [:ffmpeg]
validates_attachment_presence :avatar
validates_attachment_content_type :avatar,
:content_type => /video/,
:message => "Video not supported"
end
I have the following paperclip setup. What happens is that I'm using a proc to set the sizes for various styles. However, the proc gets called on new and during the super call. I walked through the debugger and it seems like it processes the :photo parameter first so it initializes the attachment and calls the styles proc at which point the actual object (Photo) has not been initialized by the passed in params(particularly the photo.gallery_id so it doesn't set the styles correctly. I even tried reprocessing and it didn't help. I've spent a couple of days on this and still no luck. any help is appreciated!
class Photo < ActiveRecord::Base
has_and_belongs_to_many :staffs
has_attached_file :photo,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/assets/:id/:class/:style/:image_name.:extension",
:url => "/assets/:id/:class/:style/:image_name.:extension",
:styles => Proc.new { |clip| clip.instance.attachment_styles}
def attachment_styles
if self.gallery.nil?
{ :original => {
:processors => [:watermark],
:geometry =>"600x800!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "200x300!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
elsif self.photo.styles.empty?
gallery_type = GalleryType.find_by_id(self.gallery_id)
{ :original => {
:processors => [:watermark],
:geometry =>"#{gallery_type.width_max}x#{gallery_type.height_max}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "#{gallery_type.width_min}x#{gallery_type.height_min}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
else
self.photo.styles
end
end
def reprocess_att
self.photo.reprocess!
end
def initialize(galleryid, params = {})
begin
param.merge!({"gallery_id" => galleryid.to_s})
super(params)
rescue => e
puts e.message()
end
end
From what I can see, the order of the parameters is important. I had:
attachments.build(:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'),
:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]})
And this wasn't setting up the styles correctly. They they were nil. I changed it to:
attachments.build(:styles => {:small => ['100x100#', :jpg], :medium => ['250x250', :jpg]},
:upload => File.new(File.dirname(__FILE__) + '/../fixtures/test-image.jpg'))
And then the code:
:styles => lambda { |a| a.instance.styles || {} }
worked perfectly. Hope this helps.
Thanks for the answer!
I've been fighting with this for a few weeks now. I'm using Paperclip with FFMPEG to make thumbnails of uploaded videos. I have an option to set what frame to use as the thumbnail.
I'm also using a nested form (awesome nested forms) for my asset upload. So what I did was put the frame time param before the file browse button. This solved the problem for me since I'm not using builder.