Correct short url decoding for sha1s leading with zero

This was picked up cause we had a flaky test!
This commit is contained in:
Sam 2017-08-23 11:08:18 -04:00
parent c7532003f3
commit f766ea4257
2 changed files with 11 additions and 1 deletions

View File

@ -61,7 +61,12 @@ class Upload < ActiveRecord::Base
def self.sha1_from_short_url(url)
if url =~ /(upload:\/\/)?([a-zA-Z0-9]+)(\..*)?/
sha1 = Base62.decode($2).to_s(16)
sha1.length == 40 ? sha1 : nil
if sha1.length > 40
nil
else
sha1.rjust(40, '0')
end
end
end

View File

@ -128,6 +128,11 @@ describe Upload do
expect(Upload.sha1_from_short_url('upload://r3AYqESanERjladb4vBB7VsMBm6')).to eq(sha1)
expect(Upload.sha1_from_short_url('r3AYqESanERjladb4vBB7VsMBm6')).to eq(sha1)
end
it "should be able to look up sha1 even with leading zeros" do
sha1 = '0000c513e1da04f7b4e99230851ea2aafeb8cc4e'
expect(Upload.sha1_from_short_url('upload://1Eg9p8rrCURq4T3a6iJUk0ri6.png')).to eq(sha1)
end
end
end