2012-04-08 4 views
0

ich große Probleme haben ...Paperclip - Bild speichern Dimensionen Datenbank von bereits hochgeladenen Bilder zu S3

ich rund 500 Bilder zu S3 über meine Rails-Anwendung in ein paar verschiedenen Größen hochgeladen haben. Jetzt merke ich, dass ich diese Bildermaße in meiner Datenbank speichern muss. Mein Ansatz, dies zu tun ist mit dieser Migration:

class AddImageDimensionsToImages < ActiveRecord::Migration 
    def change 

    # add image columns 
    add_column :images, :small_width, :integer 
    add_column :images, :small_height, :integer 
    add_column :images, :medium_width, :integer 
    add_column :images, :medium_height, :integer 
    add_column :images, :large_width, :integer 
    add_column :images, :large_height, :integer 
    add_column :images, :original_width, :integer 
    add_column :images, :original_height, :integer 

    # loop all images 
    Image.all.each do |image| 

     # get sizes 
     geo_small = Paperclip::Geometry.from_file(image.image.to_file(:small)) 
     geo_medium = Paperclip::Geometry.from_file(image.image.to_file(:medium)) 
     geo_large = Paperclip::Geometry.from_file(image.image.to_file(:large)) 
     geo_original = Paperclip::Geometry.from_file(image.image.to_file(:original)) 

     # set sizes 
     image.small_width = geo_small.width if geo_small 
     image.small_height = geo_small.height if geo_small 

     image.medium_width = geo_medium.width if geo_medium 
     image.medium_height = geo_medium.height if geo_medium 

     image.large_width = geo_large.width if geo_large 
     image.large_height = geo_large.height if geo_large 

     image.original_width = geo_original.width if geo_original 
     image.original_height = geo_original.height if geo_original 

     # save image 
     image.save 

    end 

    end 

end 

Wenn ich diese Migration führen Sie es so geht:

... 

-- add_column(:images, :small_width, :integer) 
    -> 2.0866s 
-- add_column(:images, :small_height, :integer) 
    -> 0.0092s 
-- add_column(:images, :medium_width, :integer) 
    -> 0.0073s 

... 

Command :: identify -format %wx%h '/app/tmp/HayUnFinal_1620120408-4-qpzyym.jpg[0]' 
[AWS S3 200 0.106467] get_object(:bucket_name=>"aa",:key=>"original/34/HayUnFinal_16.jpg") 

Command :: identify -format %wx%h '/app/tmp/HayUnFinal_1620120408-4-1vqo71y.jpg[0]' 
[paperclip] Saving attachments. 
[AWS S3 200 0.152898] get_object(:bucket_name=>"aa",:key=>"small/64/Portrait_2.jpg") 

Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-i1ohlr.jpg[0]' 
[AWS S3 200 0.055378] get_object(:bucket_name=>"aa",:key=>"medium/64/Portrait_2.jpg") 

Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-viizzf.jpg[0]' 
[AWS S3 200 0.079235] get_object(:bucket_name=>"aa",:key=>"large/64/Portrait_2.jpg") 

Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-1d2u00o.jpg[0]' 
[AWS S3 200 0.066724] get_object(:bucket_name=>"aa",:key=>"original/64/Portrait_2.jpg") 

Command :: identify -format %wx%h '/app/tmp/Portrait_220120408-4-jq9wmg.jpg[0]' 
[paperclip] Saving attachments. 
[AWS S3 200 0.104891] get_object(:bucket_name=>"aa",:key=>"small/58/Bansah_24.jpg") 

Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-an2mpt.jpg[0]' 
[AWS S3 200 0.064148] get_object(:bucket_name=>"aa",:key=>"medium/58/Bansah_24.jpg") 

Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-tzmnup.jpg[0]' 
[AWS S3 200 0.051090] get_object(:bucket_name=>"aa",:key=>"large/58/Bansah_24.jpg") 

Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-1pa0rxh.jpg[0]' 
[AWS S3 200 0.063531] get_object(:bucket_name=>"aa",:key=>"original/58/Bansah_24.jpg") 

Command :: identify -format %wx%h '/app/tmp/Bansah_2420120408-4-kogk2g.jpg[0]' 
[paperclip] Saving attachments. 

rake aborted! 
An error has occurred, this and all later migrations canceled: 

can't convert nil into String 

Rake abgebrochen! Es scheint für ein paar Bilder zu funktionieren, aber dann schlägt die Migration fehl. Ich kann nicht wirklich herausfinden, woher das Nil kommt und wie ich es verhindern kann. Kann mir jemand dabei helfen?

Bonusfrage: Ist dies eine bevorzugte Methode, die Bildmaße bereits hochgeladener Bilder zu speichern?

Vielen Dank im Voraus!

+0

ich einen Anhaltspunkt gefunden: Wenn ich versuche, von S3 bis Transmit App bestimmte Dateien zugreifen, bekomme ich diese Fehlermeldung: circa 2012-SS12 „Es kann keine Attribute bekommen“ -06.jpg "." - Vielleicht versucht es eine dieser Dateien zu lesen, die entweder beschädigt oder gelöscht sind? Ich weiß immer noch nicht, wie ich den Nil-Fehler in meiner Frage verhindern kann ... –

Antwort

0

Einige Bildzeilen in der Datenbank hatten Nullbildwerte. Eine Prüfung wie folgt aus dem Trick:

Image.all.each do |image| 

    # check that we have an image 
    if image.image_file_size 

    ... 

    end 

end 
Verwandte Themen