From 9c22c68d394ef0fa525bd5695f0ad8ce94294098 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 16 Nov 2017 15:13:15 +1100 Subject: [PATCH] FIX: only save custom fields if they actually change --- app/jobs/regular/pull_hotlinked_images.rb | 13 +++++++++++-- spec/jobs/pull_hotlinked_images_spec.rb | 11 +++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb index b84c573156e..b3d2bbdb009 100644 --- a/app/jobs/regular/pull_hotlinked_images.rb +++ b/app/jobs/regular/pull_hotlinked_images.rb @@ -49,6 +49,12 @@ module Jobs start_raw = raw.dup downloaded_urls = {} large_images = post.custom_fields[LARGE_IMAGES].presence || [] + + # recover from bad custom field silently + unless Array === large_images + large_images = [] + end + broken_images, new_large_images = [], [] extract_images_from(post.cooked).each do |image| @@ -108,8 +114,11 @@ module Jobs end - post.custom_fields[LARGE_IMAGES] = large_images - post.save! + if new_large_images.length > 0 + post.custom_fields[LARGE_IMAGES] = large_images + post.save_custom_fields + end + post.reload if start_raw == post.raw && raw != post.raw diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb index 708298429f4..395b7e53a8e 100644 --- a/spec/jobs/pull_hotlinked_images_spec.rb +++ b/spec/jobs/pull_hotlinked_images_spec.rb @@ -20,6 +20,17 @@ describe Jobs::PullHotlinkedImages do SiteSetting.download_remote_images_threshold = 0 end + describe "#nochange" do + it 'does saves nothing if there are no large images to pull' do + post = Fabricate(:post, raw: 'bob bob') + orig = post.updated_at + + freeze_time 1.week.from_now + Jobs::PullHotlinkedImages.new.execute(post_id: post.id) + expect(orig).to be_within(1.second).of(post.reload.updated_at) + end + end + describe '#execute' do before do FastImage.expects(:size).returns([100, 100]).at_least_once