New paperclip fingerprint after modifying style? - imagemagick

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

Related

Paperclip - Upload from URL rather than a form

I'm in the process of migrating a set of files from an old Drupal application to a Rails app.
Using paperclip, I want to upload a file to this model:
class Video < ActiveRecord::Base
has_attached_file :video_file
end
But I want to upload the file from a URL in code rather than using a form.
Apparently since Paperclip 2.1.4, you are able to do this like so:
video.video_file = URI.parse('http://path/to/video.mp4')
When I run this, there is a noticeable delay while the file is downloaded but none of the fields for the file are populated and the file has not been uploaded. What step am I missing?
You can do this
video.video_file = File.open("http://path/to/video.mp4")

Add fingerprint to old paperclip attachments

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!

how to remove existing style dimensions in paperclip

I know you can add new styles to paper clip and then use process! method to force paperclip to fill in the missing styles. But what if you want to ditch the old styles that you are not using any more.
For example I have styles iphone and ipad, and I changed my mind and would instead like :large and :medium with slightly different dimensions instead of :iphone and :ipad.
In particular I'm hosting my images on s3 since my app is on heroku. I would like to remove all the folders on s3 pertaining to :iphone and :ipad styles. Is there a rake task for removing select styles?
If you are referring to removing invalid files, try:
rake paperclip:clean # Cleans out invalid attachments.
You can view all paperclip tasks in your console by typing:
rake -T paperclip
run in your terminal
rake -T paperclip
you can see like this
...
rake paperclip:clean # Cleans out invalid attachments.
rake paperclip:refresh # Refreshes both metadata and thumbnails.
rake paperclip:refresh:metadata # Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT).
rake paperclip:refresh:missing_styles # Regenerates missing thumbnail styles for all classes using Paperclip.
rake paperclip:refresh:thumbnails # Regenerates thumbnails for a given CLASS (and optional ATTACHMENT and STYLES splitted by comma).
...
for refreshing your thumbnail try this
rake paperclip:refresh:thumbnails class= Xyz # xyz replace with your class name
for removing
rake paperclip:clean # Cleans out invalid attachments.
You can use the Attachment#clear method.
If you wanted to delete the thumb and display styles, you could run something like:
YourModel.find_each do |ym|
ym.attachment.clear(:display, :thumb)
ym.save!
end
You will have to write a script to accomplish this, but paperclip does have a s3_object convenience method that makes this easy:
obj = User.first.avatar.s3_object(:unwanted_style)
obj.delete if obj.exists?
You could place something like this within a loop for any objects you want to operate on.
Sources:
Paperclip::Storage::S3 documentation
aws-sdk Object documentaion

How to assign a remote file to Carrierwave?

I have video model with the following definition:
class Video
require 'carrierwave/orm/activerecord'
mount_uploader :attachment, VideoUploader
mount_uploader :attachment_thumbnail, VideoThumbnailUploader
...
end
When I upload a video file. It also sends the file to our encoding service Zencoder, which encodes the video file and creates a thumbnail for it.
Normally, I could do something like #video.attachment.url, which will return the path of the video file. I'd like to do the same thing with the thumbnail. i.e. #video.attachment_thumbnail.url
However, since the attachment is created by our encoding service, which also uploads it to a specified S3 bucket. How do I assign the attachment to the attachment_thumbnail column for the record?
Can I simply do something like:
#video.update_attributes(
:attachment_thumbnail => 'https://bucket_name.s3.amazonaws.com/uploads/users/1/video/1/thumb.png'
)
Is it possible to assign files like this to Carrierwave?
You can do the following:
#video.remote_attachment_thumbnail_url = 'https://bucket_name.s3.amazonaws.com/uploads/users/1/video/1/thumb.png'
But that will cause Carrierwave to download + reprocess the file rather than just make it the thumbnail. If you're not going to use Carrierwave's processing, then it might make more sense to just store the URL to the thumbnail on the model rather than even using Carrierwave.
This worked for me, with CarrierWave 0.5.8
model.update_attributes(:remote_uploader_url => "http://path/to/image.jpg")
Of course, you need to set remote_uploader_url to be attr_accessible for this.
I was looking for this as well.
The blocking point in the zencoder case would be that Carrierwave doesn't track different different file type versions for the original file. It only references the original file.
So having the original file as an .mp4 a a thumbnail version as a .png doesn't work.
While you can have an 'image.png' and also track 'thumb_png_image.png', you can't also create a 'thumb_jpg_image.jpg' for the same file.
Otherwise you could create a dummy version and using conditional versioning tell CW not to process it.
Since CW would create the dummy version anyway but not upload it, you could have it reference a path matching the file returned by Zencoder. But oh well...
At the end of this episode (7:35), Ryan Bates adds a remote_image_url in a file form upload:
http://railscasts.com/episodes/253-carrierwave-file-uploads

paperclip callbacks or simple processor?

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

Resources