我有一个主题模式,其中包含许多资产。资产使用回形针和储存在我的亚马逊AWS-S3系统的文件内容。我还使用deep_clone,因为我的客户必须复制内置的主题,然后对其进行修改,以他们的心内容的能力。所有deep_clone的东西是伟大的工作,但是当我deep_clone资产,旧的文件内容不被添加到我的S3桶。该记录会保存到数据库中,但由于文件内容不被保存与新ID file.url属性指向一个死文件。
What I have is a Theme model, which contains many Assets. Assets are using Paperclip and storing their file content in my Amazon AWS-S3 system. I'm also using deep_clone because my customers have the ability to copy built in Themes and then modify them to their hearts content. All the deep_clone stuff is working great, but when I deep_clone the assets, the old file contents don't get added to my S3 buckets. The record gets saved to the database, but since the file-contents don't get saved with the new ID the file.url property points to a dead file.
我已经打过电话回形针的保存和手动创建方法,但我想不出如何让回形针推的文件回,因为它现在有一个新的ID桶,等等...
I've tried calling paperclip's save and create method manually but I can't figure out how to get paperclip to "push" the file back to the bucket since it now has a new ID, etc....
require 'open-uri'
class Asset < ActiveRecord::Base
belongs_to :theme
attr_accessor :old_id
has_attached_file :file,
:storage => "s3",
:s3_credentials => YAML.load_file("#{RAILS_ROOT}/config/aws.yml")[RAILS_ENV],
:bucket => "flavorpulse-" + RAILS_ENV,
:path => ":class/:id/:style.:extension"
validates_attachment_presence :file
validates_attachment_size :file, :less_than => 5.megabytes
before_save :delete_assets_in_same_theme_with_same_name
after_create :copy_from_cloned_asset
private
def delete_assets_in_same_theme_with_same_name
Asset.destroy_all({:theme_id => self.theme_id, :file_file_name => self.file_file_name})
end
def copy_from_cloned_asset
if (!old_id.blank?)
if (old_id > 0)
old_asset = Asset.find(old_id)
if (!old_asset.blank?)
self.file = do_download_remote_image(old_asset.file.url)
self.file.save
end
end
end
end
def do_download_remote_image (image_url)
io = open(URI.parse(image_url))
def io.original_filename; base_uri.path.split('/').last; end
io.original_filename.blank? ? nil : io
rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
end
end
这是我如何能得到回形针推文件中的任何想法?我也不会反对使用Amazon的AWS-S3宝石这样做,但我不能似乎得到这工作的。
Any ideas on how I can get paperclip to push the file? I also wouldn't be opposed to doing this using Amazon's aws-s3 gem but I couldn't seem to get that to work either.
推荐答案
据的这前一个问题/答案,应该有可能这个简单的一行code:
According to this former question/answer, it should be possible with this simple line of code:
self.file = old_asset.file
相关推荐
最新文章