diff --git a/app/models/concerns/has_url.rb b/app/models/concerns/has_url.rb index eca6cf95723..fd0c02f080c 100644 --- a/app/models/concerns/has_url.rb +++ b/app/models/concerns/has_url.rb @@ -8,16 +8,16 @@ module HasUrl def get_from_url(url) return if url.blank? - + uri = begin URI(URI.unescape(url)) rescue URI::Error end - + return if uri&.path.blank? data = extract_url(uri.path) return if data.blank? - + self.find_by("url LIKE ?", "%#{data[1]}") end end diff --git a/lib/upload_recovery.rb b/lib/upload_recovery.rb index 6880c52bc9b..4e5bec5ca25 100644 --- a/lib/upload_recovery.rb +++ b/lib/upload_recovery.rb @@ -28,7 +28,7 @@ class UploadRecovery recover_post_upload(post, Upload.sha1_from_short_url(orig_src)) end elsif url = (media["href"] || media["src"]) - data = Upload.extract_upload_url(url) + data = Upload.extract_url(url) next unless data sha1 = data[2] @@ -58,7 +58,7 @@ class UploadRecovery background = user_profile.public_send(column) if background.present? && !Upload.exists?(url: background) - data = Upload.extract_upload_url(background) + data = Upload.extract_url(background) next unless data sha1 = data[2] diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 59fa7e40877..0cc49e6a741 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -95,6 +95,14 @@ describe Upload do expect(created_upload.valid?).to eq(false) end + context ".extract_url" do + let(:url) { 'https://example.com/uploads/default/original/1X/d1c2d40ab994e8410c.png' } + + it 'should return the right part of url' do + expect(Upload.extract_url(url).to_s).to eq('/original/1X/d1c2d40ab994e8410c.png') + end + end + context ".get_from_url" do let(:sha1) { "10f73034616a796dfd70177dc54b6def44c4ba6f" } let(:upload) { Fabricate(:upload, sha1: sha1) }