Add extra protection in Upload#get_from_url.

In case the extension goes missing from the URL.
This commit is contained in:
Guo Xiang Tan 2018-09-12 00:12:14 -07:00
parent 39a2d92417
commit dffd4fa9e6
2 changed files with 9 additions and 2 deletions

View File

@ -177,7 +177,7 @@ class Upload < ActiveRecord::Base
end end
return if uri&.path.blank? 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? return if data.blank?
sha1 = data[2] sha1 = data[2]
upload = nil upload = nil

View File

@ -82,9 +82,16 @@ describe Upload do
expect(Upload.get_from_url(upload.url)).to eq(upload) expect(Upload.get_from_url(upload.url)).to eq(upload)
end 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 describe 'for a url without a tree' do
let(:url) { "/uploads/default/original/1X/#{sha1}.png" } let(:url) { "/uploads/default/original/1X/#{sha1}.png" }
let(:upload) { Fabricate(:upload, url: url, sha1: sha1) }
it 'should return the right upload' do it 'should return the right upload' do
expect(Upload.get_from_url(upload.url)).to eq(upload) expect(Upload.get_from_url(upload.url)).to eq(upload)