Skip to content

A platform for community discussion. Free, open, simple.

FIX: Ensure that extract_upload_ids works with all short URLs (#17070)

David Taylor6650218e3d9b
diff --git a/app/models/upload.rb b/app/models/upload.rb
index 2f84e2bbeaf..11fb98d0e15 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -515,7 +515,7 @@ class Upload < ActiveRecord::Base
sha1s << match[0]
end
- raw.scan(/\/([a-zA-Z0-9]{27})/).each do |match|
+ raw.scan(/\/([a-zA-Z0-9]+)/).each do |match|
sha1s << Upload.sha1_from_base62_encoded(match[0])
end
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 1a2a748bc8a..b360524063e 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -520,9 +520,7 @@ describe Upload do
describe '.extract_upload_ids' do
let(:upload) { Fabricate(:upload) }
- # This spec fails when upload has a sha1 of "035839d28676a96fda268562e6aa93c57b11113c".
- # When the sha1 is converted to hex, the leading 0 is ignored so the base62 encoding will be 26 chars long.
- skip 'works with short URLs' do
+ it 'works with short URLs' do
ids = Upload.extract_upload_ids("This URL #{upload.short_url} is an upload")
expect(ids).to contain_exactly(upload.id)
end
@@ -532,12 +530,17 @@ describe Upload do
expect(ids).to contain_exactly(upload.id)
end
- # This spec fails when upload has a sha1 of "035839d28676a96fda268562e6aa93c57b11113c".
- # When the sha1 is converted to hex, the leading 0 is ignored so the base62 encoding will be 26 chars long.
- skip 'works with Base62 hashes' do
+ it 'works with Base62 hashes' do
ids = Upload.extract_upload_ids("This URL /#{Upload.base62_sha1(upload.sha1)} is an upload")
expect(ids).to contain_exactly(upload.id)
end
+
+ it 'works with shorter base62 hashes (when sha1 has leading 0s)' do
+ upload.update(sha1: "0000c513e1da04f7b4e99230851ea2aafeb8cc4e")
+ base62 = Upload.base62_sha1(upload.sha1).delete_prefix("0")
+ ids = Upload.extract_upload_ids("This URL /#{base62} is an upload")
+ expect(ids).to contain_exactly(upload.id)
+ end
end
def enable_secure_media