I've had a system thats been running fine for ages using Rails 3 & Paperclip 2.3.6 for members images.
class Image < ActiveRecord::Base
belongs_to :business
has_attached_file :data, :styles => {:normal => ["665x443#", :jpg],:thumb => ["104x50#", :jpg]}, :convert_options => {:all => "-channel RGB -strip -quality 80"}
end
Now for some reason the route for the images has changed to /system/data/ instead of previous /system/datas/
I cant figure out whats happened. All of the images still reside in /system/datas/ and a member uploaded some new images and Paperclip has put them in /system/data/
Anyone have any ideas whats happened? I cant figure it out. Thanks.
EDIT: Ok, I had a test box still on Rails 3.0.3. My Release version was on 3.0.7. If I roll back to Rails 3.0.3 it's working again - the path /system/data/ becomes /system/datas/ again.
Rails 3.0.5 introduced some additional inflections which meant 'datas' was no longer used as the plural of 'data', which broke the directory naming for Paperclip!
According to the paperclip documentation you can specify a path for saving images in your model using the :path option and providing a path.
You can find the documentation here.
Related
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
I am running Rails 3.2.8 with Paperclip 3.3.0 on a Windows 7 machine. I am getting the following error when I try to upload an image named "2012-10-26_17.49.13.png": Image There was an error processing the thumbnail for 2012-10-26_17.49.1320121027-1196-l5ejgs
The filename may not be important, but I want to point out that most of the questions I find related to this say error processing the thumbnail for stream instead. No 'stream' on my error. The end of the filename changes randomly each time I try to upload (Paperclip doing some collision avoidance I assume).
The image field is set up in the model like this:
has_attached_file :image, :path => ":rails_root/public/images/:hash_:style.:extension", :url => "/images/:hash_:style.:extension", :hash_secret => "sometext", :default_url => "", :styles => { :thumb => "100x100#" }
validates_attachment_content_type :image, :content_type => ['image/jpeg','image/jpg','image/pjpeg','image/png','image/x-png','image/gif']
The has_attached_file is using :hash at the moment, but I also tried with :basefilename or whatever it was and that didn't affect the results. The :thumb style has been tried as "100x100>" as well.
The field is like this in _form.html.erb:
<div class="field">
<%= f.label :image %><br />
<%= f.file_field :image %>
</div>
And the controller has nothing special in it regarding :image because Paperclip seems to take care of everything for me. At least, that's what I gathered from looking at example code.
I think it might be a problem with ImageMagick. Some of the solutions I found for this error involved the installation location of ImageMagick. I moved mine to C:/ImageMagick6, updated my system path, and added the following lines to development.rb:
Paperclip.options[:command_path] = "C:/ImageMagick6"
Paperclip.options[:swallow_stderr] = false
Paperclip.options[:whiny_thumbnails] = true
I'm not entirely sure if the latter two are needed, but I saw them suggested. Didn't notice any changes from including them. If I open a command prompt, I can access the ImageMagick commands, so the path should be correct.
I also tried to run this with the :styles removed, which allowed me to upload the original image file. I really want the thumbnails for my site, though.
So, what have I missed that is keeping this from working?
Despite having added Paperclip.options[:command_path] = 'C:/progra~2/imagem~1.0-q' to my config/environments/development.rb file, I had to add C:/progra~2/imagem~1.0-q before %SystemRoot%\system32 in the environment path.
I confirmed this by revising the environment path back and forth a few times.
In order to use Paperclip you need ImageMagick library installed.
ImageMagick provides two executables that are required by Paperclip: identify to identify the image format and convert that process the original image and generate the desired formats.
Now, ImageMagick does not come by default with Windows, so you need to install it manually.
Later, you will require to tell Paperclip where it will find the ImageMagick executables, which is all documented in Paperclip README
Please note that if you don't set Paperclip.options[:command_path] to the PATH where ImageMagick was installed, Paperclip will incorrectly attempt to use Windows' convert executable, which is a Filesystem conversion utility and not meant to process images.
I believe that is the error you're currently receiving about generating the thumbnails.
Hope that helps.
You might try using ImageMagick directly via the command line to manipulate an image. At the very least, this should confirm whether or not there's an issue with your ImageMagick installation.
I just upgraded to Rails 3.2 and I'm using Amazon S3 with Paperclip to upload photos to my app.
Before my Image Urls would be:
http://s3.amazonaws.com/dealphotos.website.com/photos/428/large/Sandisk120Drive?1334754504
Now my Image Urls on Localhost are:
http://s3.amazonaws.com/dealphotos.website.com/deals/photos/000/000/428/large/Sandisk120Drive?1334754504
Notice the additional 000/000's - even if I take them out and visit the link it says:
<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>"Long String of Numbers"</RequestId>
<HostId>
"Gives me a really long string of letters and numbers"
</HostId>
My Deal Model:
has_attached_file :photo, :styles => { :small =>"268x160>", :large =>"350x250>" },
:storage => :s3,
:bucket => 'dealphotos.website.com',
:s3_credentials => {
:access_key_id => ENV['S3_KEY_SPICY'],
:secret_access_key => ENV['S3_SECRET_SPICY']
}
And the images dont show up!
Before all I had to do was pull from Heroku and all my images and files would go to my development. What's going on?
You are most likely using the latest version of Paperclip (version >= 3).
Since this is a major version of paperclip there are documented incompatibilities with older versions of paperclip.
One MAJOR (and in my books good) change is that the default storage location of assets has changed.
Earlier it used to be /system/:attachment/:id/:style/:filename.:extension (from memory). This would be disastrous if you had 2 attachments with the same name (Company logo and Product logo) for example.
Now the new path used by paperclip is /system/:class/:attachment/:id_partition/:style/:filename.:extension
This means all your assets will be stored in the directory specified by above. :id_partitions are used so that the number of nodes (files/directories) in one directory doesnt exceed 1000.
I hope i've been able to explain the reason WHY you are seeing the problem. The default path has changed.
You have 2 options now -
Move old assets into the correct place in the new directory scheme (painful but recommended).
Move new assets into the old directory structure and add a :url + :path option in your attachment definition in the model to continue using the older scheme. (See https://github.com/thoughtbot/paperclip/blob/master/UPGRADING ).
Since it appears that you have images in BOTH the new structure and the old one. So no matter what you decide files will need to be moved from one structure to another. Unless the assets are bookmarked in some way, I suggest you move the older assets into the new directory structure.
Check out CarrierWave gem to file uploads. It works with 3.2 without any problems ;)
http://railscasts.com/episodes/253-carrierwave-file-uploads
I'm having a hard time figuring out how to prevent Paperclip from deleting the old version of an attachment (image).
I have a model, Site, which has an attachment, logo. I would like to keep the old logos around since I will be keeping track of changes to the model and would like to view the history of logos.
I'm keeping track of the changes in another model, which has a reference to file paths. My problem is that when updating a site with a new logo, Paperclip will flush the old logo first.
It surprises me that there's not an option you can switch to prevent Paperclip from flushing the old attachment before creating the new one.
Any ideas?
There's a new option that tells paperclip to preserve old attachments:
https://github.com/thoughtbot/paperclip/commit/65e8d4f6de50732d8e1b
https://github.com/thoughtbot/paperclip/issues/60
Simple to use:
has_attached_file => :attachment,
:styles => { :thumb => 100x100! },
:preserve_files => true
It's not documented yet and took some digging to find so I wanted to share it here.
Because attachments are defined at the class level, Paperclip interpolates the symbols in your strings using it's own interpolation library. You can create your own interpolations using this library.
I would add a field to the model called attachment_version or something similar, and then increment this version number each time the file is changed. Then, create an interpolation for it in an initializer file:
Paperclip.interpolates :version do |attachment, style|
attachment.instance.attachment_version
end
Now you can use :version in your strings:
class Model < ActiveRecord::Base
has_attached_file :something, :path => " :rails_root/public/somethings/etc/:version.:extension"
end
See the wiki documentation for more information.
[Update]
After some digging around (see the comments to this answer), I've come to the conclusion that Paperclip will still delete the old attachment due to code that's called in Paperclip::Atachment#attach. Probably the best way to deal with this is to create a new storage engine based on Paperclip::Storage::Filesystem and overwrite #flush_deletes. Note that there is no way in that method to tell if a file is being queued for deletion because of the model it belongs to being deleted or a new file is being uploaded in its place.
lib/paperclip_monkey_patch.rb:
module Paperclip
class Attachment
def clear
# nop
#raise "hell"
# op
instance_write(:file_name, nil)
instance_write(:content_type, nil)
instance_write(:file_size, nil)
instance_write(:updated_at, nil)
end
end
end
Then add this line at the top of any file that deleted attachments:
require 'paperclip_monkey_patch'
Thanks to Ruby Forum
I had a similar issue with Paperclip attachments at when working on a Rails blog last summer.
There is a patch that addresses this. I wasn't able to get it working for myself, but it's worth a shot!
http://github.com/alainravet/paperclip/tree/keep_old_files
I wanted to run the callback after_post_process but it doesn't seem to work in Rails 3.0.1 using Paperclip 2.3.8. It gives an error:
undefined method `_post_process_callbacks' for #<Class:0x102d55ea0>
I want to call the Panda API after the file has been uploaded. I would have created my own processor for this, but as Panda handles the processing, and it can upload the files as well, and queue itself for an undetermined duration I thought a callback would do fine. But the callbacks don't seem to work in Rails3.
after_post_process :panda_create
def panda_create
video = Panda::Video.create(:source_url => mp3.url.gsub(/[?]\d*/,''), :profiles => "f4475446032025d7216226ad8987f8e9", :path_format => "blah/1234")
end
I tried require and include for paperclip in my model but it didn't seem to matter.
Anyideas?
Solution...
I put the callback after the paperclip has_attached in the given model and it works beautifully. I was just so used to always putting the callback at the top of all models that this didn't occur to me til later.
Moving the has_attached_file attribute above the validates_presence_of and validates_attachment
in your model still needs to be done it seems. I just ran into the same problem in my Rails 4/Ruby 2 implementation of PaperClip and putting it above fixed it.
I ran into this problem because the name of my paperclip image property did not match the name I was validating against.
as_attached_file :image
validates_attachment_content_type: :not_image