Sorry for maybe dumb question, but I have some misunderstanding about how Paperclip works. If I use it with aws-sdk gem, where image urls are stored?
My model has only fields like image_name, image_size, but not something like image_url. I also tried to list all tables from my db in sql console, but there wasn't any special image-related tables. I know what I can access url by calling my_model.image.url, but I need understanding, where this url is located.
Thanks.
The urls are auto generated based on the ruleset you have in your paperclip config file. They usually include the name and id of your model unless you have changed the defaults. Set the :default_url option when you call has_attached_file if you want to change it:
:default_url => '/images/:attachment/missing_:style.png',
:path => "avatars/:id/:style/:filename"
More background: rails paperclip default image with S3
Related
I have a Rails 4 application running on MongoDB and I use Paperclip (4.3) for the uploads. Everything works fine.
Now I'm building a REST API with Node.js and Mongoose to serve some of the data and I'm having trouble understanding how I can generate the URL for the paperclip attachments with Node.js.
Here's the relevant part of one of the Rails models:
has_mongoid_attached_file :thumb,
:url => "/assets/images/templates/:hash.:extension",
:path => ":rails_root/public/assets/images/templates/:hash.:extension",
:hash_secret => "somesecret"
The fields generated in the MongoDB document end up looking something like this:
"thumb_file_name" : "test-19103515.jpg",
"thumb_content_type" : "image/jpeg",
"thumb_file_size" : 76864,
"thumb_fingerprint" : "00dab4127977a69ab264d79202f5f799",
"thumb_updated_at" : ISODate("2018-03-28T03:32:36.454Z"),
and the URL of the file looks like this:
/assets/images/templates/3666b3ef97b492b27b108fd215c397d2435d5fcb.jpg?1522207956
Basically what I am asking is if anyone know how to generate the hash that paperclip generates so that given the hash_secret i can get in this case the resulting 3666b3ef97b492b27b108fd215c397d2435d5fcb hash.
Note that I am aware that there is a node-paperclip npm package but I only see there upload examples while what i need is to be able to generate the full URL of an attachment already uploaded by the Rails app. If by any chance one of you knows how to do that with that package I'll be happy to use it.
I have a paperclip instance that I am migrating my files to a different area. Originally the files were stored on my server and just given a filename based on the id of the record created and the original id. Now I'm moving them to s3 and want to update the filenames to work appropriately. I setup my paperclip config like so:
:path => ":class/:attachment/:hash-:style.:extension",
:url => ":s3_domain_url",
:hash_secret => SECRET,
:hash_data => ":class/:attachment/:id/:updated_at"
I updated the original records filenames for my files to be unique and moved them over to my s3 instance. Unfortunately now I am unable to pull down the files from s3 and I think it is because paperclip is using the wrong path for the filenames. One that is based off the path default that is now set using my config file. I want to be able to update my files file_name field so that the path is correct for the new files and I am able to download them appropriately. Is there a way to call paperclips hashing function based on my secret and hash_data directly so I can update those file_name fields and be able to pull those records now? Everything that has been uploaded since the move from my original servers seems to work appropriately.
Say you have a model User with an attachment named profile_pic;
Go into the rails console eg. rails c and then get an object for the model you have the attachment on, eg. u = User.find(100).
Now type u.profile_pic.url to get the url or u.profile_pic_file_name to get the filename.
To see the effect of other options (for example your old options) you can do;
p = u.profile_pic # gets the paperclip attachment for profile_pic
puts p.url # gets the current url
p.options.merge!(url: '/blah/:class/:attachment/:id_partition/:style/:filename')
puts p.url # now shows url with the new options
Similarly p.path will show the local file path with whatever options you pick.
Long story short, something like;
User.where('created_at < some_date').map do |x|
"#{x.id} #{x.profile_pic_file_name} #{x.profile_pic.path}"
end
should give you what you want :)
I am in the process of upgrading my rails app from 2.3 to 3.2, and I am having a problem with paperclip. My app was previously using paperclip v2.3.5 and now I am using rails 3.0.20 with paperclip v3.3.1
The problem happens when I try to upload an avatar to S3. It looks like paperclip is escaping the path before sending it to S3, but when I ask for the url of a specific style, the url uses an unescaped version of the path, and this leads to a "NoSuchKey" error from S3 (more like a 404 not found)
In my model I have
Paperclip.interpolates :last_modified do |attachment, style|
attachment.instance.updated_at.to_i
end
:path => "folder/:id/:style.:extension?:last_modified",
:url => ":s3_domain_url",
So the old version of my app was using urls from S3 like:
http://my-bucket.s3.amazonaws.com/folder/123/thumbnail.png?123456789
But now, everytime I upload an avatar, S3 will store the url like this:
http://my-bucket.s3.amazonaws.com/folder/123/thumbnail.png%3F123456789
and asking my model for the url of thumbnail style, will return:
http://my-bucket.s3.amazonaws.com/folder/123/thumbnail.png?123456789
which S3 can't find.
I know it seems to be an easy to fix issue, but my main concern is that the production app has a lot of users with many pictures and updating all of their images is a delicate task, and manually escaping the question mark is not a solution.
I just figured out how to fix a problem that was similar to this. There is a new escape_url option. Maybe try turning that to false?
:path => "folder/:id/:style.:extension?:last_modified",
:url => ":s3_domain_url",
:escape_url => false
With paperclip, how can you get paperclip to use full urls like http://mysite.com/image/asdasd.png
versus /image/asdad.png
thanks
Once you configure the :url interpolation string to your satisfaction, you can link to attachments with the full URL using something like:
def attachment_path(attachment)
attachment.url
end
def attachment_url(attachment)
"#{root_url}#{attachment.url.gsub(/^\//, '')}"
end
Assuming you want the behavior to work like the _url helpers on the controller/view level, its a little complicated as paperclip functions without the benefit of knowing the host from the request. An easy way to get around this is to define the constant HOST in the config/environments/ENV.rb and then passing the url parameter to has_attachment like
:url => "http://#{HOST}/:path"
or whatever your url rules are.
You can also side step this issue by using S3, which is kind of a life saver
If you are uploading files to amazon S3 then s3.url gives the full image path. But in case of local file storage, you can set :url option also
I am currently using paperclip to upload images to my rails app. This is probably a very simple fix but how or where do I save the missing images to? This is the error that is produced from not having any missing images. How do I change this?
ActionController::RoutingError (No route matches "/photos/normal/missing.png"):
If you don't need any control over the default image, which I don't think you need, you can place it in any folder under RAILS_ROOT/public/images/
Just make sure you point it out in the attachment model with the :default_url parameter. So for example if you place the image in RAILS_ROOT/public/images/normal/missing.png you need t specify the path like this:
has_attached_file :photo, :default_url => "/images/normal/missing.png"