FIX: generate_topic_thumbnails job infinitely running for corrupted images

FIX: generate_topic_thumbnails job infinitely running for corrupted images

It’s possible that the original topic image is broken in some form, so we shouldn’t try and generate a topic thumbnail for it. The fix will prevent the generate_topic_thumbnails job being enqueued every time the topic is viewed.

diff --git a/app/models/topic.rb b/app/models/topic.rb
index e02c2bb..e665125 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -115,6 +115,8 @@ class Topic < ActiveRecord::Base
         image_upload &&
         SiteSetting.create_thumbnails &&
         image_upload.filesize < SiteSetting.max_image_size_kb.kilobytes &&
+        image_upload.read_attribute(:width) &&
+        image_upload.read_attribute(:height) &&
         enqueue_if_missing &&
         Discourse.redis.set(thumbnail_job_redis_key([]), 1, nx: true, ex: 1.minute)
       Jobs.enqueue(:generate_topic_thumbnails, { topic_id: id })
diff --git a/spec/fabricators/upload_fabricator.rb b/spec/fabricators/upload_fabricator.rb
index 62272ec..b63eee2 100644
--- a/spec/fabricators/upload_fabricator.rb
+++ b/spec/fabricators/upload_fabricator.rb
@@ -35,6 +35,13 @@ Fabricator(:image_upload, from: :upload) do
   end
 end
 
+Fabricator(:upload_no_dimensions, from: :upload) do
+  width nil
+  height nil
+  thumbnail_width nil
+  thumbnail_height nil
+end
+
 Fabricator(:video_upload, from: :upload) do
   original_filename "video.mp4"
   width nil
diff --git a/spec/models/topic_thumbnail_spec.rb b/spec/models/topic_thumbnail_spec.rb
index 08b1e61..85b8ead 100644
--- a/spec/models/topic_thumbnail_spec.rb
+++ b/spec/models/topic_thumbnail_spec.rb
@@ -6,6 +6,8 @@ describe "TopicThumbnail" do
   let(:topic) { Fabricate(:topic, image_upload: upload1) }
   let(:upload2) { Fabricate(:image_upload, width: 5000, height: 5000) }
   let(:topic2) { Fabricate(:topic, image_upload: upload2) }
+  let(:upload3) { Fabricate(:upload_no_dimensions) }
+  let(:topic3) { Fabricate(:topic, image_upload: upload3) }
 
   before do
     SiteSetting.create_thumbnails = true
@@ -26,6 +28,18 @@ describe "TopicThumbnail" do
     topic2.reload
 
     expect(topic2.topic_thumbnails.length).to eq(0)
+    expect(Jobs::GenerateTopicThumbnails.jobs.size).to eq(0)
+  end
+
+  it "does not enque job if image_upload width is nil" do
+    SiteSetting.create_thumbnails = true
+    topic3.image_url(enqueue_if_missing: true)
+
+    TopicThumbnail.ensure_consistency!
+    topic3.reload
+
+    expect(topic3.topic_thumbnails.length).to eq(0)
+    expect(Jobs::GenerateTopicThumbnails.jobs.size).to eq(0)
   end
 
   it "cleans up deleted uploads" do

GitHub sha: c2f3c0dc