Commit 9d1355b5 by Sebastien Guignot Committed by Prem Sichanugrist

Fix attachment.reprocess! when using storage providers fog and s3.

parent 02d99ec3
...@@ -30,6 +30,11 @@ module Paperclip ...@@ -30,6 +30,11 @@ module Paperclip
@tempfile.read(length, buffer) @tempfile.read(length, buffer)
end end
# We don't use this directly, but aws/sdk does.
def rewind
@tempfile.rewind
end
def eof? def eof?
@tempfile.eof? @tempfile.eof?
end end
...@@ -50,7 +55,11 @@ module Paperclip ...@@ -50,7 +55,11 @@ module Paperclip
def copy_to_tempfile(src) def copy_to_tempfile(src)
dest = Tempfile.new(src.original_filename) dest = Tempfile.new(src.original_filename)
dest.binmode 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 dest
end end
......
...@@ -135,6 +135,17 @@ module Paperclip ...@@ -135,6 +135,17 @@ module Paperclip
(creds[env] || creds).symbolize_keys (creds[env] || creds).symbolize_keys
end 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 private
def find_credentials(creds) def find_credentials(creds)
......
...@@ -307,6 +307,17 @@ module Paperclip ...@@ -307,6 +307,17 @@ module Paperclip
@queued_for_delete = [] @queued_for_delete = []
end 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 def find_credentials creds
case creds case creds
when File when File
......
require './test/helper' require './test/helper'
require 'aws' require 'aws'
unless ENV["S3_BUCKET"].blank? unless ENV["S3_BUCKET"].blank?
class S3LiveTest < Test::Unit::TestCase class S3LiveTest < Test::Unit::TestCase
...@@ -105,7 +104,11 @@ unless ENV["S3_BUCKET"].blank? ...@@ -105,7 +104,11 @@ unless ENV["S3_BUCKET"].blank?
assert_success_response @dummy.avatar.url assert_success_response @dummy.avatar.url
end end
should "be destoryable" do should "be reprocessable" do
assert @dummy.avatar.reprocess!
end
should "be destroyable" do
url = @dummy.avatar.url url = @dummy.avatar.url
@dummy.destroy @dummy.destroy
assert_not_found_response url assert_not_found_response url
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment