having trouble solving problem with pictures on my project.
Summary: Rilas 4 hosted on Heroku using Paerclip with S3
Problem starts with having to use previously used custom uploading logic with S3. picture url looks something like this /profile_picture/:style_:image_hash. It works fine with images that are there but with images that are not present paperclip still trying to access image that is not there and the actual link look something like this: http://s3.amazonaws.com/project/profile_pictures/100h_.
has_attached_file :picture,
styles: { :'53h' => '', :'100h' => '' },
convert_options: {
:'100h' => '-gravity center -thumbnail 165x165^ -extent 165x165',
:'53h' => '-gravity center -thumbnail 45x45^ -extent 45x45'
},
path: 'profile_pictures/:style_:filename',
default_url: '/images/default-pp-large.jpg'
I am guessing that might be because of style inside the actual filename, but i am not sure, eather way defauly_url is not working and images all are broken, excluding the ones that are actually there.
Can you help please?
In the end i made a monkey pach to paperclip gem. Added this lines to config/initializers/paperclip.rb
module Paperclip
class Attachment
alias_method :original_url, :url
def url(style_name = default_style, options = {})
if #instance.public_send("#{#name.to_s}_file_name").blank?
'default-pp-large.jpg'
else
original_url(style_name, options)
end
end
end
end
i'm wondring how is the ID is not present in the path for the picture, as in this case if 2 differents Pictures with same name it will retrieve the first match as i think, path should be something like:
path: 'profile_pictures/:id_:style_:filename'
# OR
path: 'profile_pictures/:id/:style_:filename'
not sure if that should be totally solving the problem but that is a part of it.
Related
I am trying to embed an image from Rails' Active Storage into a Prawn PDF.
This is what I've got in my Prawn PDF class:
path = #view.rails_blob_url(#logo, :host => "localhost:3000", :protocol => "http", :locale => nil)
image(path, :vposition => :center)
When I try to open the PDF, I get this error:
ArgumentError in InvoicesController#show
http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGdz09IiwiZXhwIjxudWxsHCJwdXIiOiJibG9iX2lkIn19--06167c36c283f6d5de63ae306b721310af11f70e/Test-Logo.png
not found
When I copy that exact same URL into my browser, the image shows up as expected. 5 minutes later it expires (?).
What am I missing here?
How can I show the image in my PDF?
Why can the image be rendered in the browser but not in my PDF?
I've spent all day trying to get this to work today, but to no avail.
This is what worked for me:
def initialize_globals
logo_from_object = #organization.logo
#logo = StringIO.open(logo_from_object.download)
..
..
..
end
def print_header
image #logo, scale: 0.10
end
Give this a shot:
image ActiveStorage::Blob.service.send(:path_for, #company.logo_image.key), at: [X, Y], width: DESIRED_WIDTH
Where logo_image is the actual ActiveStorage::Attached::One object.
E.g
class Company
has_one_attached :logo_image
end
Also, I believe as of now, only 2 types are supported: png and jpg.
I'm trying to have a different URL definition for each style, something like:
has_attached_file :asset, styles: {
original: "1920x1920>",
cropped: {:geometry => "200x200#", :processors => [:cropper] }
},
urls: {
original: "/images/:hash_path/:filename",
cropped: "/images/:hash_path/cropped/:filename"`
}
The goal is to store the same image only once according to the MD5 checksum (fingerprint), but use the checksum of the current style (like thumb, cropped) instead of the original file.
Example: 10 students uploaded the same school photo and selected area of their face for avatar creation. The large school photo should be saved only once in the storage, but keep each avatar is necessary.
So far I haven't found a easy way how to do it with Paperclip which is able to save fingerprint just for the original image now. I'm curious if there is a way how to define url per style? That could particularly solve this.
Thanks for any other suggestions how to proceed.
Edit: By /:hash_path I mean some kind of interpolation like mentioned here http://jonathanng.com/ruby-on-rails/getting-around-ext3-inode-limitations-using-md5-file-paths-and-paperclip-interpolations/
One suggestion to proceed is to use interpolations https://github.com/thoughtbot/paperclip/wiki/Interpolations
Set url something like:
:url => "/attachments/:hash_path/:basename_:style.:extension",
then in the interpolations:
Paperclip.interpolates :hash_path do |attachment, style|
hash_path = "whatever_#{style}" # generate hash path here
end
this should put whatever hash_path interpolation returns into url replacing the :hash_path key.
I use paperclip to upload a users avatar. The image is stored correctly in the /public directory. However I cant figure out how I can get the image displayed. I played with the :url and :path settings for about an hour and cant match them in a way the image will be displayed in the browser.
There is always a 'images/localhost' in the GET-requests path that I can not get rid of.
Here is my code:
user.rb
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "missing.png",
:url => ':class/:id/:style.:extension',
:path => ':url'
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end
The path in the html-tag looked up by the request looks like this:
<img alt="Original" src="/images/localhost/users/1/original.png?1405249849" />
The correct request which returns the image would be
http://localhost:3000/users/1/original.png?1405248482.
How can I get the request match the correct file-system-path. Or: what is wrong with my settings?
I didnt change the application.rb or the development.rb
Best regards,
Kev
you should take url or path helpers of paperclip wherever possible. So to show the image use:
<%= image_tag #user.avatar.url(:medium) %>
Then:
the image url isn't a file system path. Depending on the storage you use in paperclip, images can reside in different places, see understanding storage of the paperclip gem.
If you use file storage, the files are store somewhere like
public/system/users/avatar/000/000/013/small/my_pic.png
Timestamp
What you're seeing is Paperclip's timestamp - the last time the file / object was updated.
Although I can't find any official reference to this number, it basically allows you to determine which files you're dealing with. According to the question referenced above, it's apparently there for if you want to ensure your visitors see the latest version of the file (IE never gets stored in the cache)
I'm not sure why there is a disparity between your stored image & your path. However, I would say the path is correct; you just need to be able to
--
Bottom line - if your image shows on the page, I don't think there's any systemic issue with your path; if it doesn't show on the page, can you provide logs / reference to the error?
This post, https://stackoverflow.com/a/26222093/1949363, which pointed to http://www.bwigg.com/2009/10/paperclip-customizing-paths-and-urls/ was the answer for me. I was having issues only in my test environment but I believe the fix should work in other environments as well.
Try the following settings:
:path => "public/system/:class/:id/:filename",
:url => "/system/:class/:id/:basename.:extension"
I am using paperclip and AWS for my Rails app to upload image. You can find it here: http://lit-stream-6263.herokuapp.com/
When I try to upload images, I don't get an error but for some reason the image doesn't show. When I go into the S3 bucket though, I'm able to see the image that gets uploaded...it's just not rendering in the html page. Any advice on how to fix this?
Update
From 9nonnatus, I'm seeing the picture if I change the URL. However in my rails view I have
<%= image_tag product.avatar.url(:medium) %>
to display the image. This is what I see in the documentation as well. How do I adjust this to fit the url you mention above?
class Product < ActiveRecord::Base
attr_accessible :blog_link, :blog_name, :description, :image_link, :name, :num_likes, :product_link, :avatar
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end
Looking at the source of the page you can copy the image link and try to access it in your browser. It gives an error telling you that your hyperlinks are incorrectly formatted. Instead of making the img src format something like:
http://s3.amazonaws.com/rockywolfugc/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
you have to use this format:
http://rockywolfugc.s3-us-west-2.amazonaws.com/products/avatars/000/000/003/medium/59577_10100258833612183_1508749_n.jpg?1386876682
In other words, remove /rockywolfugc from after .com and replace s3 with rockywolfugc.s3-us-west-2
Hope that helps.
i've been using Paperclip, when deleting is done, it is just deleting the attachments, but the images saved in the file system is not being deleted.can anybody plz tell how to delete the images from the file system using paperclip itself.
the code i've been using is
has_attached_file :image_o_filename,
:styles => {
:tn => ["100x100#",:jpg ,:name => :tn],
:w => ["640x480>",:jpg, :name => :w],
:l_tn => ["200x150#",:jpg, :name => :l_tn]
},
:path => "/places/hotels/:image_pre_path/images/:basename_:style.:extension"
when i delete the photos from the view, images are getting deleted from the database but not from the file system with the above path, images remain without getting deleted. what might be the reason ??
and no errors generated in console also..
Paperclip has a very nice built in delete method, I was stuck on a very similar problem and was very well informed in this solution:
I created a method inside the controller called delete_image then from the view I could call this method.
def delete_image
#user.image.destroy
end
I had the image attached to a user instance and this worked perfect, don't forget to define your method in the routes file in your config/routes.
Good luck also you can look up more information here:
See: Rails Paperclip how to delete attachment?