Skip to content

Commit

Permalink
Add extra protection in Upload#get_from_url.
Browse files Browse the repository at this point in the history
In case the extension goes missing from the URL.
  • Loading branch information
tgxworld committed Sep 14, 2018
1 parent 39a2d92 commit dffd4fa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/upload.rb
Expand Up @@ -177,7 +177,7 @@ def self.get_from_url(url)
end

return if uri&.path.blank?
data = uri.path.match(/(\/original\/\dX[\/\.\w]*\/([a-zA-Z0-9]+)[\.\w]+)/)
data = uri.path.match(/(\/original\/\dX[\/\.\w]*\/([a-zA-Z0-9]+)[\.\w]*)/)
return if data.blank?
sha1 = data[2]
upload = nil
Expand Down
9 changes: 8 additions & 1 deletion spec/models/upload_spec.rb
Expand Up @@ -82,9 +82,16 @@
expect(Upload.get_from_url(upload.url)).to eq(upload)
end

describe 'for an extensionless url' do
let(:url) { "/uploads/default/original/1X/#{sha1}" }

it 'should return the right upload' do
expect(Upload.get_from_url(upload.url)).to eq(upload)
end
end

describe 'for a url without a tree' do
let(:url) { "/uploads/default/original/1X/#{sha1}.png" }
let(:upload) { Fabricate(:upload, url: url, sha1: sha1) }

it 'should return the right upload' do
expect(Upload.get_from_url(upload.url)).to eq(upload)
Expand Down

0 comments on commit dffd4fa

Please sign in to comment.