开发者

Can't copy images with Paperclip gem using image.to_file for S3 images

I am having an issue that is only affecting this application in it's production environment. We have a single controller action that is used to "clone" article objects by creating a new object, setting the attributes equal to each other, then adding the images to the object.

Here is the portion of the controller that is doing that:

def clone_article
 ba = BlogArticle.find(params[:id])
 new_ba = BlogAr开发者_Python百科ticle.new(ba.attributes)

 ba.blog_article_images.each do |blog_img|
   new_ba.blog_article_images.build(:image => blog_img.image.to_file, :embedded => blog_img.embedded?)
 end

 new_ba.status = 'draft'
 new_ba.title = "Copy of #{ba.title}"

 if new_ba.save
     flash[:notice] = "Clone successful"
 else
    if new_ba.errors.empty?
        flash[:notice] = "Unknown error occurred while cloning the post"
    else
        error = 'Problem while cloning the post: <br>'
        new_ba.errors.each {|field, msg| error += field + " " + msg + "<br>"}
        flash[:error] = error
    end
 end
redirect_to admin_blog_articles_url

end

The issue is that this script works perfectly when referring to local files on the server. But in the production environment, which has the images on S3, we cannot get any of the images to copy over from the original posts images. I think this could be an issue with timing, like the controller not waiting for paperclip to finish loading the files into the app directory before finishing the process, but I cannot seem to get anything to work. Also, I'm not a rails expert so I'm getting a bit lost.

Thanks


I've run into the issue with filenames getting mangled by Tempfile as well, and ended up with this somewhat hackish solution:

ba.blog_article_images.each do |blog_img|
  new_img = blog_img.image.to_file
  new_img.instance_variable_set("@original_filename", blog_img.image.original_filename)
  def new_img.original_filename
    @original_filename
  end
  new_ba.blog_article_images.build(:image => new_img, :embedded => blog_img.embedded?)
end

it does work though. original_filename is a method that Paperclip adds to File, and we're just overriding it here.

I haven't tested this with a local storage, but from reading the Paperclip source, it should work.


I'm working on something similar. I've found that simply doing object_2.file = object_1.file, then saving object_2 works fine. Mostly. The S3 transfer works dandy but the filename is getting mangled for some unknown reason.


The file gets mangled because the file from object_1 is copied down from S3 and stored as a TempFile which makes it's own file name using the original file name at the start and extension at the end. I'm working on this same problem too and appreciate any thoughts on how to change the TempFile name before saving object_2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜