FIX: Handle invalid URLs gracefully when pulling hotlinked images

FIX: Handle invalid URLs gracefully when pulling hotlinked images

diff --git a/app/jobs/regular/pull_hotlinked_images.rb b/app/jobs/regular/pull_hotlinked_images.rb
index 80367f4..415ce3b 100644
--- a/app/jobs/regular/pull_hotlinked_images.rb
+++ b/app/jobs/regular/pull_hotlinked_images.rb
@@ -220,7 +220,7 @@ module Jobs
       uri.normalize!
       uri.scheme = nil
       uri.to_s
-    rescue URI::Error
+    rescue URI::Error, Addressable::URI::InvalidURIError
       src
     end
   end
diff --git a/spec/jobs/pull_hotlinked_images_spec.rb b/spec/jobs/pull_hotlinked_images_spec.rb
index 37499a8..b589383 100644
--- a/spec/jobs/pull_hotlinked_images_spec.rb
+++ b/spec/jobs/pull_hotlinked_images_spec.rb
@@ -222,6 +222,16 @@ describe Jobs::PullHotlinkedImages do
       MD
     end
 
+    it 'works when invalid url in post'  do
+      post = Fabricate(:post, raw: <<~MD)
+      ![some test](#{image_url})
+      ![some test 2]("#{image_url})
+      MD
+
+      expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }
+        .to change { Upload.count }.by(1)
+    end
+
     it 'replaces bbcode images' do
       post = Fabricate(:post, raw: <<~MD)
       [img]

GitHub sha: 9f2e7e46

1 Like

Nice :+1:

Is that the only place were we should handle both standard URI::Error and Adresseable’s URI::InvalidURIError?