Fix attachment.reprocess! when using a remote files provider (Fog & S3) #827

Closed
wants to merge 1 commit into
from
@@ -30,6 +30,11 @@ def read(length = nil, buffer = nil)
@tempfile.read(length, buffer)
end
+ # We don't use this directly, but aws/sdk does.
+ def rewind
+ @tempfile.rewind
+ end
+
def eof?
@tempfile.eof?
end
@@ -50,7 +55,11 @@ def cache_current_values
def copy_to_tempfile(src)
dest = Tempfile.new(src.original_filename)
dest.binmode
- FileUtils.cp(src.path(:original), dest.path)
+ if src.respond_to? :copy_to_local_file
+ src.copy_to_local_file(:original, dest.path)
+ else
+ FileUtils.cp(src.path(:original), dest.path)
+ end
dest
end
@@ -135,6 +135,17 @@ def parse_credentials(creds)
(creds[env] || creds).symbolize_keys
end
+ def copy_to_local_file(style, local_dest_path)
+ log("copying #{path(style)} to local file #{local_dest_path}")
+ local_file = ::File.open(local_dest_path, 'wb')
+ file = directory.files.get(path(style))
+ local_file.write(file.body)
+ local_file.close
+ rescue Fog::Errors::Error => e
+ warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
+ false
+ end
+
private
def find_credentials(creds)
@@ -307,6 +307,17 @@ def flush_deletes #:nodoc:
@queued_for_delete = []
end
+ def copy_to_local_file(style, local_dest_path)
+ log("copying #{path(style)} to local file #{local_dest_path}")
+ local_file = ::File.open(local_dest_path, 'wb')
+ file = s3_object(style)
+ local_file.write(file.read)
+ local_file.close
+ rescue AWS::Errors::Base => e
+ warn("#{e} - cannot copy #{path(style)} to local file #{local_dest_path}")
+ false
+ end
+
def find_credentials creds
case creds
when File
@@ -1,7 +1,6 @@
require './test/helper'
require 'aws'
-
unless ENV["S3_BUCKET"].blank?
class S3LiveTest < Test::Unit::TestCase
@@ -105,7 +104,11 @@ class S3LiveTest < Test::Unit::TestCase
assert_success_response @dummy.avatar.url
end
- should "be destoryable" do
+ should "be reprocessable" do
+ assert @dummy.avatar.reprocess!
+ end
+
+ should "be destroyable" do
url = @dummy.avatar.url
@dummy.destroy
assert_not_found_response url