FIX: `UploadRecovery` should look through posts for img src and bbcode.

FIX: UploadRecovery should look through posts for img src and bbcode.

diff --git a/lib/upload_recovery.rb b/lib/upload_recovery.rb
index 5f50460..6880c52 100644
--- a/lib/upload_recovery.rb
+++ b/lib/upload_recovery.rb
@@ -15,34 +15,29 @@ class UploadRecovery
         analyzer = PostAnalyzer.new(post.raw, post.topic_id)
 
         analyzer.cooked_stripped.css("img", "a").each do |media|
-          if media.name == "img"
+          if media.name == "img" && orig_src = media["data-orig-src"]
             if dom_class = media["class"]
               if (Post.white_listed_image_classes & dom_class.split).count > 0
                 next
               end
             end
 
-            orig_src = media["data-orig-src"]
+            if @dry_run
+              puts "#{post.full_url} #{orig_src}"
+            else
+              recover_post_upload(post, Upload.sha1_from_short_url(orig_src))
+            end
+          elsif url = (media["href"] || media["src"])
+            data = Upload.extract_upload_url(url)
+            next unless data
+
+            sha1 = data[2]
 
-            if orig_src
+            unless upload = Upload.get_from_url(url)
               if @dry_run
-                puts "#{post.full_url} #{orig_src}"
+                puts "#{post.full_url} #{url}"
               else
-                recover_post_upload(post, Upload.sha1_from_short_url(orig_src))
-              end
-            end
-          elsif media.name == "a"
-            href = media["href"]
-
-            if href && data = Upload.extract_upload_url(href)
-              sha1 = data[2]
-
-              unless upload = Upload.get_from_url(href)
-                if @dry_run
-                  puts "#{post.full_url} #{href}"
-                else
-                  recover_post_upload(post, sha1)
-                end
+                recover_post_upload(post, sha1)
               end
             end
           end
diff --git a/spec/lib/upload_recovery_spec.rb b/spec/lib/upload_recovery_spec.rb
index 69d9412..fca10f5 100644
--- a/spec/lib/upload_recovery_spec.rb
+++ b/spec/lib/upload_recovery_spec.rb
@@ -113,6 +113,60 @@ RSpec.describe UploadRecovery do
       expect(File.read(Discourse.store.path_for(post.uploads.first)))
         .to eq(File.read(file_from_fixtures("smallest.png")))
     end
+
+    describe 'image tag' do
+      let(:post) do
+        Fabricate(:post,
+          raw: <<~SQL,
+          <img src='#{upload.url}'>
+          SQL
+          user: user
+        ).tap(&:link_post_uploads)
+      end
+
+      it 'should recover the upload' do
+        stub_request(:get, "http://test.localhost#{upload.url}")
+          .to_return(status: 200)
+
+        expect do
+          upload.destroy!
+        end.to change { post.reload.uploads.count }.from(1).to(0)
+
+        expect do
+          upload_recovery.recover
+        end.to change { post.reload.uploads.count }.from(0).to(1)
+
+        expect(File.read(Discourse.store.path_for(post.uploads.first)))
+          .to eq(File.read(file_from_fixtures("smallest.png")))
+      end
+    end
+
+    describe 'bbcode' do
+      let(:post) do
+        Fabricate(:post,
+          raw: <<~SQL,
+          [img]#{upload.url}[/img]
+          SQL
+          user: user
+        ).tap(&:link_post_uploads)
+      end
+
+      it 'should recover the upload' do
+        stub_request(:get, "http://test.localhost#{upload.url}")
+          .to_return(status: 200)
+
+        expect do
+          upload.destroy!
+        end.to change { post.reload.uploads.count }.from(1).to(0)
+
+        expect do
+          upload_recovery.recover
+        end.to change { post.reload.uploads.count }.from(0).to(1)
+
+        expect(File.read(Discourse.store.path_for(post.uploads.first)))
+          .to eq(File.read(file_from_fixtures("smallest.png")))
+      end
+    end
   end
 
   describe "#recover_user_profile_backgrounds" do

GitHub sha: 6a95d3fd