Commit 33ba2fd2 by Akihiko Odaki Committed by Sid Raval

Reopen file even when copying instead of linking (#2545)

parent 2c0760b5
...@@ -57,15 +57,16 @@ module Paperclip ...@@ -57,15 +57,16 @@ module Paperclip
end end
def link_or_copy_file(src, dest) def link_or_copy_file(src, dest)
begin
Paperclip.log("Trying to link #{src} to #{dest}") Paperclip.log("Trying to link #{src} to #{dest}")
FileUtils.ln(src, dest, force: true) # overwrite existing FileUtils.ln(src, dest, force: true) # overwrite existing
@destination.close
@destination.open.binmode
rescue Errno::EXDEV, Errno::EPERM, Errno::ENOENT, Errno::EEXIST => e rescue Errno::EXDEV, Errno::EPERM, Errno::ENOENT, Errno::EEXIST => e
Paperclip.log( Paperclip.log(
"Link failed with #{e.message}; copying link #{src} to #{dest}" "Link failed with #{e.message}; copying link #{src} to #{dest}"
) )
FileUtils.cp(src, dest) FileUtils.cp(src, dest)
end
@destination.close @destination.close
@destination.open.binmode @destination.open.binmode
end end
......
...@@ -93,6 +93,34 @@ describe Paperclip::AbstractAdapter do ...@@ -93,6 +93,34 @@ describe Paperclip::AbstractAdapter do
end end
end end
context "#copy_to_tempfile" do
around do |example|
FileUtils.module_eval do
class << self
alias paperclip_ln ln
def ln(*)
raise Errno::EXDEV
end
end
end
example.run
FileUtils.module_eval do
class << self
alias ln paperclip_ln
undef paperclip_ln
end
end
end
it "should return a readable file even when linking fails" do
src = open(fixture_file("5k.png"), "rb")
expect(subject.send(:copy_to_tempfile, src).read).to eq src.read
end
end
context "#original_filename=" do context "#original_filename=" do
it "should not fail with a nil original filename" do it "should not fail with a nil original filename" do
expect { subject.original_filename = nil }.not_to raise_error expect { subject.original_filename = nil }.not_to raise_error
......
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