Copying s3-based attachments #9

Closed
qrush opened this Issue Jun 5, 2009 · 11 comments

Projects

None yet

7 participants

Contributor
qrush commented Jun 5, 2009

Reported by Dylan Markow

Using S3, it doesn't seem that copying attachments from one record to another works. The comments in attachment.rb state that "In addition to form uploads, you can also assign another Paperclip attachment: new_user.avatar = old_user.avatar", but when I try this, the second record's attachment remains empty and nothing gets uploaded to amazon's servers.

a = Post.find(1)
b = Post.find(2)
a.attachment_file_name
# => "Attachment.pdf"
b.attachment_file_name
# => nil
b.attachment = a.attachment
b.save
b.attachment_file_name
# => nil

came across this too. here is a failing test:

context "photo" do
  setup {add_photo_to_person; @person.save}
  should "be reasignable" do
    assert @person.photo.file?
    other_dude = people(:turk)
    other_dude.photo = @person.photo
    other_dude.save!
    assert other_dude.photo.file?
end

end
other_dude.photo.file? fails when paperclip is configured for s3.

hkelsey commented Nov 30, 2010

This looks to be fixed as of v2.3.6. Give it a try.

Interestingly, I've found that copying a file from one model to another changes the filename. I get a seemingly random series of numbers and underscores at the end of the name. Have any of you experienced this, or have any suggestions as to what might be causing it?

hkelsey commented Dec 13, 2010

This is due to Ruby's TempFile class which gives the file a new name. I'm trying to work around this as well.

I wonder if a before_post_process in the target model that modifies filename would work, or if that would duplicate the whole copy or not propagate to S3. Might have to try it out.

Initial tests were unsuccessful. I tried the following:

before_save :copy_image

def copy_image
  source_image = OtherModel.image
  self.image = source_image
  self.image.instance_write(:file_name, "foo.jpg") 
end

This processes the image twice: once to copy it and another time to rename it. There's got to be a simple way of copying files with filename intact.

Member
jyurek commented Jun 19, 2011

Annoyingly, I can reproduce this on the console, but not in a test. i would welcome a failing test, if someone can supply it.

Contributor
sikachu commented Jul 15, 2011

I can't reproduce this one locally either. Please notify me if you still see this problem so I can reopen the ticket.

@sikachu sikachu closed this Jul 15, 2011

Unless something changed in the last few point releases, it'll still be an issue. I'll try to write some stuff up when time permits (which is almost never). :(

@sikachu sikachu added a commit to jyurek/paperclip that referenced this issue Sep 23, 2011
@sikachu sikachu Ensure that we cache attachment's file name before we perform assignment
There was an issue on assignment another S3 attachment object to another object whereas calling #to_file on S3 attachment will create a Tempfile, which would then having a different file name than original version.

By caching the filename first before calling #to_file, we're ensuring that filename is the original one.

Fixes #9
522a53e
kule commented Nov 14, 2011

I'm not sure if it's related however I have an issue with 2.4.5 where if you clone an object that doesn't have an attachment you get a "can't convert nil to string" exception. I don't understand why it works ok in the console but fails when you try in the browser?

def duplicate
  post = self.clone
  post.image = self.image.to_file 
  post
end

The attachment you are handed in a request is an instance of a different class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment