Duplicate images uploaded with Dragonfly

220 views Asked by At

I would like to copy a record that has_many :pictures. Copying the record is a no brainer, but copying the Picture records is something else.

  • The Picture record has a link to a Post
  • Picture has an attribute (for Dragonfly) that is image_uid that contains a string like 2016/08/17/3chjxpz97o_tfss_05bbc7ac_a432_4408_bf6e_a0fa3dc4630d_animage.jpeg

The image is stored on an AWS S3 server. From the server perspective I think this does

  • Download the image (original record image_uid) to the server
  • Attach the image to the new Picture record
  • Reupload the image to AWS
  • Is there a method that just copies it on AWS, gives you the new image_uid so I can set it manually on the new record?

Thx

1

There are 1 answers

0
Yoko On BEST ANSWER

I have found how this is done. For people who might ever need this:

My original Picture object:

#<Picture:0x007f82570f8f58> {
            :id => 285,
     :image_uid => "2016/10/06/6tacpx09uq_large_0.jpeg",
        :number => nil,
          :main => true,
    :created_at => Thu, 06 Oct 2016 08:59:44 UTC +00:00,
    :updated_at => Thu, 06 Oct 2016 08:59:48 UTC +00:00,
       :user_id => 46,
    :company_id => 27,
        :public => true
}

Duplicating this is actually not that hard. I used the .dup method provided by Ruby. Copying multiple pictures:

pictures.each do |p|
  p2 = Picture.create(image:p.image, user:to_user, company:to_company, public:true, main: p.main)
end

The image:p.image is where you do the actual image duplication.