The following code preserves the file name between providers (where "obj" is the model, "source" is one uploader and "destination" is the other):
def copy_between_clouds(obj, src, dest)
tmp = File.new("/tmp/tmp", "wb")
begin
filename = src.file.url
File.open(tmp, "wb") do |file|
file << open(filename).read
end
t = File.new(tmp)
sf = CarrierWave::SanitizedFile.new(t)
dest.file.store(sf)
ensure
File.delete(tmp)
end
end
To use it:tmp = File.new("/tmp/tmp", "wb")
begin
filename = src.file.url
File.open(tmp, "wb") do |file|
file << open(filename).read
end
t = File.new(tmp)
sf = CarrierWave::SanitizedFile.new(t)
dest.file.store(sf)
ensure
File.delete(tmp)
end
end
copy_between_clouds(o, o.old_jpg, o.new_jpg)
You might need to change "src.file.url" to "src.file.public_url" for some cloud providers.
No comments:
Post a Comment