From 16c0ebe8a8d5a7f2685d69df30d9575981518905 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 17 Aug 2018 16:52:55 +0800 Subject: [PATCH] Fix the build. --- app/jobs/regular/pull_hotlinked_images.rb | 1 + lib/file_helper.rb | 10 +++++++--- spec/components/file_helper_spec.rb | 19 +++++++++++++++++++ spec/jobs/pull_hotlinked_images_spec.rb | 12 ++++++------ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index b28daaceb2e..1acefc21fc2 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -20,6 +20,7 @@ module Jobs downloaded = FileHelper.download( src, max_file_size: @max_size, + retain_on_max_file_size_exceeded: true, tmp_file_name: "discourse-hotlinked", follow_redirect: true ) diff --git a/lib/file_helper.rb b/lib/file_helper.rb index c44f2b20dcf..21c0cd79bce 100644 --- a/lib/file_helper.rb +++ b/lib/file_helper.rb @@ -25,7 +25,8 @@ class FileHelper follow_redirect: false, read_timeout: 5, skip_rate_limit: false, - verbose: false) + verbose: false, + retain_on_max_file_size_exceeded: false) url = "https:" + url if url.start_with?("//") raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\// @@ -68,8 +69,11 @@ class FileHelper tmp.write(chunk) if tmp.size > max_file_size - tmp.close - tmp = nil + unless retain_on_max_file_size_exceeded + tmp.close + tmp = nil + end + throw :done end end diff --git a/spec/components/file_helper_spec.rb b/spec/components/file_helper_spec.rb index adb3730b989..d892b9016c5 100644 --- a/spec/components/file_helper_spec.rb +++ b/spec/components/file_helper_spec.rb @@ -59,6 +59,8 @@ describe FileHelper do ) expect(Base64.encode64(tmpfile.read)).to eq(Base64.encode64(png)) + ensure + tmpfile&.close end it "works with a protocol relative url" do @@ -69,6 +71,8 @@ describe FileHelper do ) expect(Base64.encode64(tmpfile.read)).to eq(Base64.encode64(png)) + ensure + tmpfile&.close end describe 'when max_file_size is exceeded' do @@ -81,6 +85,19 @@ describe FileHelper do expect(tmpfile).to eq(nil) end + + it 'is able to retain the tmpfile' do + tmpfile = FileHelper.download( + "//eviltrout.com/trout.png", + max_file_size: 1, + tmp_file_name: 'trouttmp', + retain_on_max_file_size_exceeded: true + ) + + expect(tmpfile.closed?).to eq(false) + ensure + tmpfile&.close + end end describe 'when url is a jpeg' do @@ -98,6 +115,8 @@ describe FileHelper do ) expect(File.extname(tmpfile)).to eq('.png') + ensure + tmpfile&.close end end end diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index 38a0f766bad..12641895c43 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -103,12 +103,12 @@ describe Jobs::PullHotlinkedImages do end it 'all combinations' do - post = Fabricate(:post, raw: " - -#{url} - - - ") + post = Fabricate(:post, raw: <<~BODY) + + #{url} + + + BODY Jobs::ProcessPost.new.execute(post_id: post.id) Jobs::PullHotlinkedImages.new.execute(post_id: post.id)