FIX: `FileHelper#download` should return nil if max size is exceeded.

This commit is contained in:
Guo Xiang Tan 2018-08-17 16:17:58 +08:00
parent be89f593f9
commit a26ef7738f
2 changed files with 17 additions and 1 deletions

View File

@ -67,7 +67,11 @@ class FileHelper
tmp.write(chunk) tmp.write(chunk)
throw :done if tmp.size > max_file_size if tmp.size > max_file_size
tmp.close
tmp = nil
throw :done
end
end end
tmp&.rewind tmp&.rewind

View File

@ -71,6 +71,18 @@ describe FileHelper do
expect(Base64.encode64(tmpfile.read)).to eq(Base64.encode64(png)) expect(Base64.encode64(tmpfile.read)).to eq(Base64.encode64(png))
end end
describe 'when max_file_size is exceeded' do
it 'should return nil' do
tmpfile = FileHelper.download(
"//eviltrout.com/trout.png",
max_file_size: 1,
tmp_file_name: 'trouttmp'
)
expect(tmpfile).to eq(nil)
end
end
describe 'when url is a jpeg' do describe 'when url is a jpeg' do
let(:url) { "https://eviltrout.com/trout.jpg" } let(:url) { "https://eviltrout.com/trout.jpg" }