diff --git a/app/services/uri_adapter.rb b/app/services/uri_adapter.rb index bf7f1c19dae..09cfc1a1823 100644 --- a/app/services/uri_adapter.rb +++ b/app/services/uri_adapter.rb @@ -6,14 +6,14 @@ class UriAdapter def initialize(target) raise Discourse::InvalidParameters unless target =~ /^https?:\/\// - @target = URI(target) + @target = Addressable::URI.parse(target) @original_filename = ::File.basename(@target.path) @content = download_content @tempfile = TempfileFactory.new.generate(@original_filename) end def download_content - open(target) + open(target.normalize) end def copy_to_tempfile(src) diff --git a/spec/services/uri_adapter_spec.rb b/spec/services/uri_adapter_spec.rb index 7bbde1722d2..8f5ef020f97 100644 --- a/spec/services/uri_adapter_spec.rb +++ b/spec/services/uri_adapter_spec.rb @@ -14,7 +14,7 @@ describe UriAdapter do describe "#initialize" do it "has a target" do - subject.target.should be_instance_of(URI::HTTP) + subject.target.should be_instance_of(Addressable::URI) end it "has content" do @@ -29,6 +29,27 @@ describe UriAdapter do subject.tempfile.should be_instance_of Tempfile end + describe "it handles ugly targets" do + let(:ugly_target) { "http://cdn.discourse.org/assets/logo with spaces.png" } + subject { UriAdapter.new(ugly_target) } + + it "handles targets" do + subject.target.should be_instance_of(Addressable::URI) + end + + it "has content" do + subject.content.should == response + end + + it "has an original_filename" do + subject.original_filename.should == "logo with spaces.png" + end + + it "has a tempfile" do + subject.tempfile.should be_instance_of Tempfile + end + end + end describe "#copy_to_tempfile" do