diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ca8f6043a..e0b91f001 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -836,6 +836,7 @@ en: crawl_images: "Retrieve images from remote URLs to insert the correct width and height dimensions." download_remote_images_to_local: "Convert remote images to local images by downloading them; this prevents broken images." download_remote_images_threshold: "Minimum disk space necessary to download remote images locally (in percent)" + download_remote_images_max_days_old: "Don't download remote images for posts that are more than n days old." disabled_image_download_domains: "Remote images will never be downloaded from these domains. Pipe-delimited list." editing_grace_period: "For (n) seconds after posting, editing will not create a new version in the post history." post_edit_time_limit: "The author can edit or delete their post for (n) minutes after posting. Set to 0 for forever." diff --git a/config/site_settings.yml b/config/site_settings.yml index dde76cecc..5e726a2fd 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -635,6 +635,10 @@ files: test: false default: true download_remote_images_threshold: 10 + download_remote_images_max_days_old: + default: 30 + min: 1 + max: 10000 disabled_image_download_domains: type: list default: '' diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 8c671920c..783ee3b29 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -347,6 +347,8 @@ class CookedPostProcessor return unless SiteSetting.download_remote_images_to_local? # have we enough disk space? return if disable_if_low_on_disk_space + # don't download remote images for posts that are more than n days old + return unless @post.created_at > (Date.today - SiteSetting.download_remote_images_max_days_old) # we only want to run the job whenever it's changed by a user return if @post.last_editor_id == Discourse.system_user.id # make sure no other job is scheduled diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index fef5750de..0d27ffd51 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -413,7 +413,7 @@ describe CookedPostProcessor do context ".pull_hotlinked_images" do - let(:post) { build(:post) } + let(:post) { build(:post, created_at: 20.days.ago) } let(:cpp) { CookedPostProcessor.new(post) } before { cpp.stubs(:available_disk_space).returns(90) } @@ -467,7 +467,7 @@ describe CookedPostProcessor do context ".disable_if_low_on_disk_space" do - let(:post) { build(:post) } + let(:post) { build(:post, created_at: 20.days.ago) } let(:cpp) { CookedPostProcessor.new(post) } before { cpp.expects(:available_disk_space).returns(50) } @@ -493,6 +493,34 @@ describe CookedPostProcessor do end + context ".download_remote_images_max_days_old" do + + let(:post) { build(:post, created_at: 20.days.ago) } + let(:cpp) { CookedPostProcessor.new(post) } + + before do + SiteSetting.download_remote_images_to_local = true + cpp.expects(:disable_if_low_on_disk_space).returns(false) + end + + it "does not run when download_remote_images_max_days_old is not satisfied" do + SiteSetting.download_remote_images_max_days_old = 15 + Jobs.expects(:cancel_scheduled_job).never + cpp.pull_hotlinked_images + end + + it "runs when download_remote_images_max_days_old is satisfied" do + SiteSetting.download_remote_images_max_days_old = 30 + + Jobs.expects(:cancel_scheduled_job).with(:pull_hotlinked_images, post_id: post.id).once + + delay = SiteSetting.editing_grace_period + 1 + Jobs.expects(:enqueue_in).with(delay.seconds, :pull_hotlinked_images, post_id: post.id, bypass_bump: false).once + + cpp.pull_hotlinked_images + end + end + context ".is_a_hyperlink?" do let(:post) { build(:post) }