Paperclip - Using custom watermark with dynamic path - ruby-on-rails

I'm trying to create a custom path using this:
has_attached_file :avatar,
:processors => [:watermark],
:styles => lambda { |attachment| {
:medium => {
:geometry => "300x300>",
:watermark_path => Customer.find(attachment.instance.customer_id).signature.path
},
:thumb => "100x100>",
}
}
This is working fine with Customer.find(attachment.instance.customer_id).signature.path with a static path to image, but in this case it is throwing an exception
Paperclip::Errors::InfiniteInterpolationError in Api::Ipad::V1::ImagesController#create
Can anyone tell how to ignore this exception ?

Usually that error comes from not explicity defining the :url and :path options for the attachment, might try setting those options and see if the interpolation error goes away.

Related

URL issue: paperclip-av-transcoder

I am trying to implement paperclip-av-transcoder gem. I have checked everything but not able to find what I am doing wrong here. I'm writing steps which I have followed.
Added into gemfile
--> gem 'paperclip-av-transcoder'
Added into my model
--> has_attached_file :video_file, :styles => {
:medium => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "100x100#", :format => 'jpg', :time => 10 }
}, :processors => [:transcoder]
--> validates_attachment_content_type :video_file, :content_type => /\Avideo\/.*\Z/
created schema to add column name
"video_file_meta"
In my view file
video_tag(video.video_file.url, controls: true, autobuffer: true, size: "320x240")
I have checked video in public/system folder it is properly saved I am able to see that video there but I am not able to see that in my view file.
Video Url -> /system/videos/video_files/000/000/003/original/tingtong_464.mp4?1497851104
I am sharing screens to show how it looks in the browser.
Everything from my point is correct, except Paperclip requires at least one field in database - *_file_name (for you it's video_file_file_name), but you didn't added it and Paperclip can't construct url properly. Read more https://github.com/thoughtbot/paperclip#usage
Currently working model file code:
has_attached_file :video_file, :styles => {
:medium => { :geometry => "500x500", :format => 'jpg' },
:thumb => { :geometry => "100x100", :format => 'jpg' }
}, :processors => [:transcoder]
validates_attachment_content_type :video_file,
:content_type => [
"video/mp4",
"video/quicktime",
"video/3gpp",
"video/x-ms-wmv",
"video/mov",
"video/flv",
],
:message => "Sorry! We do not accept the attached file type"
I guess I am not resizing my video file in any other video format so it is working properly.

Rails and Paperclip storing images in a specific path sets wrong URL

I want to store my images using the normal file storage adapter.
This is my PAPERCLIP_STORAGE_OPTS:
PAPERCLIP_STORAGE_OPTS = {
:styles => { :thumb => '170x170!#', :medium => '450x300!>', :large => '600x400!>',:desktop => '750x300!>'},
:convert_options => { :all => '-quality 100' },
:processor => [ :papercrop ],
:path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename"
}
This is my model :
class User < ActiveRecord::Base
attr_accessor :PAPERCLIP_STORAGE_OPTS
has_attached_file :user_photo, PAPERCLIP_STORAGE_OPTS_THUMB
When a user uploads a photo - it actually does store the image in the correct location on my system:
/opt/www/myapp/images/users/user_photos/000/000/050/original/picture
However when I go to show the image, like this :
<%=image_tag current_user.user_photo.url(:thumb), :height=> "30", :width=> "30" %>
The image is not found, and in my logs I see the image request at this URL:
ActionController::RoutingError (No route matches [GET] "/system/users/user_photos/000/000/050/thumb/picture"):
And the full URL created is :
https://www.myapp.com/system/users/user_photos/000/000/050/thumb/picture?1460285803 - which doesnt resolve.
How can I configure paperclip to allow my images to be stored in this particular url /opt/www/myapp/images/ and still be accessed and linked to correctly through Paperclip in my rails app?
You will have to set URL option:
for me it was:
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>", :small=>"60x60>" },
:path => ':rails_root/public/system/:class/:id/:style/:filename',
:url => '/system/:class/:id/:style/:filename'
Not sure for your case as you store images in the app folder directly so you may try(test it from console and modify it):
:path => "/opt/www/myapp/images/:class/:attachment/:id_partition/:style/:filename",
:url => '/images/:class/:attachment/:id_partition/:style/:filename'

Paperclip change default path and hash image names

I'm new to Rails and using Paperclip, I have it set up on my model already without issue. I just don't like the path it's generating for my images right now (:root_path/system/users/avatars/000/000/001) I really don't even understand it. How can I modify this default path for my images to something more friendly? And how can I hash the image names?
In your model, you can set the default path, styles and url as so:
has_attached_file :avatar,
:styles => { :large => "500x500>", :medium => "300x300>", :thumb => "100x100>" },
:path => ":rails_root/public/images/:id/:style/:filename",
:url => "/images/:id/:style/:filename"
you can setup the hash on the paperclip initializer file (config/initializers/paperclip_defaults.rb)
Quoting from paperclip wiki:
Paperclip::Attachment.default_options.update({
:path => ":class/:attachment/:hash/:style.:extension",
:hash_secret => "SOME_RANDOM_SECRET"
})
The :hash part is generated from :hash_secret and the pattern given by the :hash_data option, which by default is ":class/:attachment/:id/:style/:updated_at".

Paperclip is not saving the original file when a style is defined

I'm working to make Paperclip store a thumbnail in addition to the original image. The original image had always saved file and now I'm trying to add a thumbnail.
The problem is, once I defined the style thumbnail, the original is no longer being saved. How can I get the original to save along with the thumb?
has_mongoid_attached_file :attachment,
:storage => :s3,
:s3_credentials => "s3.yml",
:s3_protocol => 'https',
:s3_permissions => :private,
:use_timestamp => true,
:default_style => :original,
:default_url => '/images/:attachment/default_:style.png',
:path => "/:rails_env/private/:basename.:extension",
:styles => {
:thumb => "100x100#" },
:convert_options => {
:thumb => "-quality 75 -strip" }
Install Imagemagic (https://github.com/thoughtbot/paperclip) and then you can rezize your original sized photo into any size by using like this
<img src="#", size: '150x150'>

Paperclip change images path after upgrade to rails 3.2

i have a problem with paperclip (3.0.2) after upgrade to rails 3.2 (from 3.0.10).
Originally the path of one image was:
"http://localhost:3000/system/photos/94/small/AudiLogo.jpg?1335392139"
and after the upgrade this kind of images never show again!, but if i upload a new picture this will display fine on page, but the new path that use is:
"localhost:3000/system/products/photos/000/000/094/smal/AudiLogo.jpg?1335392139"
Whats happend in the upgrade ? There's any solution for convert the olds path to new ?
I try with "rake paperclip:refresh:missing_styles" but dosen't works.
The paperclip config section it's this.
has_attached_file :photo,
:processors => lambda { |a|
if a.external?
[:thumbnail]
else
[:thumbnail,:watermark]
end
},
:styles => {
:slider => { :geometry => "350x312#", :format => :jpg, :watermark_path => "#{Rails.root}/public/images/watermark.png", :position => "NorthEast" },
:small => "100x50>",
:medium => "200>x200",
:thumb => "100x100>",
:big => { :geometry => "640x480>", :format => :jpg, :watermark_path => "#{Rails.root}/public/images/watermark.png" }
},
:default_url => "/images/noimage.png"
Thanks in advance.
I had the same problem. You can fix this by creating a file like config/initializers/paperclip.rb and put
Paperclip::Attachment.default_options.merge!(
:path => ":rails_root/public/system/:attachment/:id/:style/:basename.:extension",
:url => "/system/:attachment/:id/:style/:basename.:extension"
)
I just had a similar upgrade and routed around my problem this way:
has_attached_file :image,
:url => "/images/photos/:id/:basename_:style.:extension",
:path => ":rails_root/public/images/photos/:id/:basename_:style.:extension",
Assuming the "small" vs "smal" difference between original and current path is a typo, the other obvious change is the addition of the two numeric segments after the "/photos/".
".../photos/000/000/094/smal/AudiLogo.jpg?1335392139"
I suspect this is coming from an id_partition being used for the path. Are you setting a different default path interpolation in some other place?
Looking at Paperclip's code I see the id_partition method that would be responsible for this but still have not found any documentation pointing in the direction of a change in the default behavior. I did't get to follow the code in the gem to determine if it is a bug or undocumented change.

Resources