Correct short url decoding for sha1s leading with zero

approved
heisentest
#1

Correct short url decoding for sha1s leading with zero

This was picked up cause we had a flaky test!

diff --git a/app/models/upload.rb b/app/models/upload.rb
index ace1e34..329e597 100644
--- a/app/models/upload.rb
+++ b/app/models/upload.rb
@@ -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
 
diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb
index 53a98da..47ec85b 100644
--- a/spec/models/upload_spec.rb
+++ b/spec/models/upload_spec.rb
@@ -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

GitHub sha: f766ea42

Approved #2