将回形针附件的所有样式复制到新对象 (S3) [英] Copy All Styles of a Paperclip Attachment to New Object (S3)

查看:51
本文介绍了将回形针附件的所有样式复制到新对象 (S3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Paperclip 处理图像的模型.当图像上传时,会对一些 javascript 裁剪进行预览,然后根据所选裁剪制作缩略图和预览大小.在 S3 上总共给我们 3 张图片:

I have a model that uses Paperclip to handle images. When the image gets uploaded a preview is made for some javascript cropping and then a thumbnail and preview sizes are made from the chosen cropping. Giving us 3 images on S3 total:

  • 原始图片
  • 预览(来自用户选择的裁剪)
  • 拇指(来自用户选择的裁剪)

附件模型中的代码是:

has_attached_file :picture, ASSET_INFO.merge(
  :whiny => false,
  :styles       => { :thumb => '200>x200#', :preview => '400x300>' },
  :processors   => [:jcropper],
  :preserve_files => true
)

我们有一些功能允许用户为自己的目的制作对象的副本,我们希望复制图像.我以为只是做一个简单的

We have some functionality that allows a user to make a copy of an object for their own purposes and we want to copy the images over. I thought that just doing a simple

new_my_model.picture = original_my_model.picture if original_my_model.picture_file_name #no file name means no picture

会完成工作,而且确实如此,但只是有点.

would get the job done, and it does, but only kind of.

它正在复制图片,然后根据模型中的设置重新处理预览和缩略图.

It's copying the picture and then reprocessing the preview and thumbnails based on what's set up in the model.

我想做的是将所有 3 个现有图像(原始图像、拇指图像和预览图像)复制到新对象,因为它们是原始图像,然后将它们保存在 S3 上的适当位置,跳过调整大小/裁剪.

What I would like to do instead is copy all 3 existing images (original, thumb, and preview) to the new object as they are for the original one and then save them in the appropriate location on S3, skipping the resizing/cropping.

谁能指出我正确的方向?我在网上搜索过,似乎找不到任何东西,我尝试的一切似乎都不起作用.对原图做一个.dup会导致异常,所以这个想法不行了.

Can anyone point me in the right direction? I've searched online and can't seem to find anything and everything I try doesn't seem to work. Doing a .dup on the original picture causes an exception, so that idea is out.

推荐答案

手动裁剪打破了 Paperclip 的自动裁剪/调整大小方案.没关系,直到您想将附件从一个模型复制到另一个模型.您有两个选择:将每种样式的裁剪参数保存在数据库中,并调用重新处理!"复制后(基于这个问题).

Manual cropping breaks Paperclip's automatic cropping/resizing scheme. That's okay until you want to duplicate an attachment from one model to another. You have two options: Save the cropping parameters for each style in database, and call "reprocess!" after duplicating (based on the this question).

我无意将裁剪数据保存在数据库中,这完全没用.我决定自己盲目复制图像,直接调用S3.不是最佳的,但有效:

I have no intention to save cropping data in database, it's totally useless. I decided to blindly duplicate the images myself, directly calling S3. Not optimal, but works:

module Customizable

  def duplicate copy_args
    new_model = self.dup
    copy_args.each {|key, val| new_model[key] = val}
    new_model.save

    s3 = AWS::S3.new(self.image.s3_credentials)
    bucket = s3.buckets[self.image.s3_credentials[:bucket]]

    styles = self.image.styles.keys.insert(0, :original)

    begin
      styles.each do |style|
        current_url = self.image.path(style)
        current_object = bucket.objects[current_url]
        if current_object.exists?
          # actually asking S3 if object exists
          new_url = new_model.image.path(style)
          new_object = bucket.objects.create(new_url)
          # this is where the copying takes place:
          new_object.copy_from(current_object)
          new_object.acl = current_object.acl
        end
      end
    rescue Exception => err
      return err
    end

    return true
  end

end

在我的模型中:

class Product < ActiveRecord::Base

  # ...
  has_attached_file :image, ...
  # ...
  include Customizable

  def customize product_id
    return self.duplicate({:in_sale => false}) #resetting some values to the duplicated model
  end

  # ...
end

这篇关于将回形针附件的所有样式复制到新对象 (S3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆