I use paperclip for the attachments on my models. I've recently added a fingerprint to the attachments, it works fine with the new attachments but it ws not added to the old ones, because of this I'm having trouble reprocessing attachments to add a new style, because paperclip tries to find the attachments with fingerprint and not all the attachments have one.
Is there a way to add the fingerprint to the old attachments?
This is the error I'm getting from the reprocessing on the old attachments:
Errno::ENOENT: No such file or directory - /tmp/paperclip-reprocess20140422-1036-nl9gxk
[edit]
Have you tried?
$ rake paperclip:refresh class=Model_Name_Goes_Here
[/edit]
if you don't have too many images. simplest way, I can think of is:
Dir.foreach('/path/to/dir/that/contains/old/images') do |item|
next if item == '.' or item == '..'
file = File.join(File.expand_path(File.dirname(__FILE__)), item)
thing.image = file
thing.save
end
you can run this script, using runner:
$ rails runner /path/to/script
Does something like this work for you?
After manually try and examine the error several times I found that the problem is not that the attachment had no fingerprint. The problem was that the fingerprint was nil. For some reason paperclip does not handle this case.
I manually gave every object an empty string as a fingerprint through the database (this saved me from renaming the file on disk) and then reprocess every one of theme.
That solved it!
Related
I would like to migrate from Paperclip to Carrier Wave or Refile because of this. The solution written here is impressive, but strikes me as complex and perhaps brittle.
My Rails4 app has 100's of images in production that were uploaded with Paperclip. Files are stored on production server. I have looked for a complete set of steps to follow to migrate, but keep coming up empty.
Is there a set of steps one can follow that allows for migration without necessitating application code re-write?
Alternatively, is there another way to persist uploaded files in Paperclip when form validation fails?
What am I missing here?
UPDATE:
Tried the solution detailed here by https://stackoverflow.com/users/646389/galatians . My paperclip :path and :url interpolations make use of :id_partition. I don't see a way this can be reconciled with an uploaded Image that is staged, but not yet saved.
I migrated to Carrierwave. Here are the relevant stats:
Time to work on and fail at coding a solution for persistent files
across form reloading with Paperclip - 4 hours. See OP update for the issue I could not overcome.
Time to Migrate to carrierwave, adjust the relevant models, controllers, and forms, and test. - 2 hours. Not so bad.
This key info helped me adjust the paths correctly. Keeping the path identical was important to me for avoiding having to move images to a new location in production:
Carrierwave code for generating paperclip-like :path and :url info here.
Paperclip interpolation info here.
This link got me on the right track, although my default :path made use of :id_partition not :id.
UPDATE:
Migration breaks this paradigm:
#protocol.images.each do |i|
tmp=i.dup
tmp.avatar = File.open(i.avatar.current_path)
tmp.save!
#dest.images << tmp
end
See: Duplicating a record that contains a carrierwave avatar : Getting "can't convert nil into Integer" error
You don't need to do anything because the only data needed to get images is already stored in DB.
I'm uploading multiple files with Carrierwave gem as pointed out here: https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads
I can delete them all in one shot with no problem with user.remove_photos! being photos an array or json in the db; however, how do I delete selectively one or two out of the complete set of files previously uploaded?
#user.remove_photos! <--- removes all the photos
#user.save
#user.remove_photos[0]! <--- does not work
By the way, I'm using sqlite3 and in order to make it work you need to do the following tweaks:
rails g migration add_photos_to_users photos:string <-- note is string
and in your model definition, after mounting the uploader:
serialize :photos, JSON
both, not yet shown on the Readme, although the commit was already done by rusikf here: https://github.com/rusikf/carrierwave/commit/dedc9c341e34f2e0e57d47c3eec65347fef398bd
It looks like Paperclip generates a fingerprint for an attachment only when the original style is changed. I want to keep the original style unchanged but crop one of my other styles (let's call it "listing"). Is there a way to force Paperclip to regenerate a fingerprint if the "listing" style is cropped but the "original" style is not?
I found generate_fingerprint no longer works with the latest Paperclip (4.1.1) but the refresh rake task does: e.g. rake paperclip:refresh CLASS=Dog.
In my case, I wanted the fingerprint to be created automatically when I ran the the migration that added the fingerprint, so I dug into the code and found you can call reprocess! on the attachment and that'll also do the trick.
I recently added an image_fingerprint column to an existing model that has Paperclip images attached.
I forced Paperclip to generate fingerprints for the existing images with the generate_fingerprint method on Paperclip::Attachment:
class Dog
has_attached_file :image
...
end
Dog.all.each do |dog|
dog.image_fingerprint = dog.image.generate_fingerprint(dog.image)
...
end
I'm trying to re-create all thumbs. I'm not sure why is saying the key does not exist. I have AWS-S3 configured properly and it's working well (I can upload pictures with no problems.)
>> Attachment.all.each {|x|x.attachment.reprocess!}
AWS::S3::NoSuchKey: The specified key does not exist.
/app/d999782b-a789-4763-ac86-e8c65fa781eb/home/.bundle/gems/ruby/1.8/gems/aws-s3- 0.6.2/lib/aws/s3/error.rb:38:in `raise'
/app/d999782b-a789-4763-ac86-e8c65fa781eb/home/.bundle/gems/ruby/1.8/gems/aws-s3-0.6.2/lib/aws/s3/base.rb:72:in `request'
/app/d999782b-a789-4763-ac86-e8c65fa781eb/home/.bundle/gems/ruby/1.8/gems/aws-s3-0.6.2/lib/aws/s3/base.rb:88:in `get'
/app/d999782b-a789-4763-ac86-e8c65fa781eb/home/.bundle/gems/ruby/1.8/gems/aws-s3-0.6.2/lib/aws/s3/object.rb:134:in `value'
When I tried to do the same to a single object seems to do it well, so the problem seems to be related to generating with a collection.
>> Attachment.last.attachment.reprocess!
=> true
UPDATE: I'm pretty sure it's related to the fact that there are uploaded files such as .htm that should be valid image files. Any idea how to skip them?
Though I am not sure, but I hope this might help you.
Attachment.all.each { |x| x.attachment.reprocess! if ['.jpeg','.jpg','.png','.gif'].include?(File.extname(file_name))}
where file_name => Name of the uploaded file
Best of Luck
Not sure where you've put your key for the AWS-S3, but you may have to specify that you want to run this under the production environment.
heroku rake paperclip:refresh CLASS=Attachment RAILS_ENV=production
I don't know how your validations are set up but is it possible that some attachment objects can have a blank attachment? If so, try:
Attachment.all.each { |x| x.attachment.reprocess! rescue nil }
This error can also refer to when an object(key) no longer exists on S3 but you have a record pointing to it in your database. This only happens if someone has made changes to the S3 bucket that don't mesh with what you have in your DB.
If that's the case you can use the ".exists?" method on the attachment to check if that key exists on Amazon's server's first, not that this will issue a read request.
This would change your reprocess command to something like this:
Attachment.all.each { |x| x.attachment.reprocess! if x.attachment.exists? }
Have you considered using:
rake paperclip:refresh
Instead?
I've just installed this plugin, created the migrations, added everything I needed to make it work(I didn't install ImageMagick yet).
The problem is when I get the upload control parameter to save it in my controller, I get something like this:
#<File:C:\Users\Brian\AppData\Local\Temp\RackMultipart.2560.6677>
instead of a simple string, like
C:\Users\Brian\AppData\Local\Temp\RackMultipart.2560.6677
And if I try to read it I get the following exception:
TypeError backtrace must be Array of
String
What am I doing wrong? How do I read it or simply get rid of the # and <> symbols?
Paperclip defaults to storing uploads in the file system, not the database. Uploads are stored in the public/system directory. Have you checked there?
Robin