diff --git a/Gemfile b/Gemfile index d7b155bc0..590eb13a9 100644 --- a/Gemfile +++ b/Gemfile @@ -249,10 +249,6 @@ gem 'stringex', require: false gem 'logster' -# we need that to support underscore in URLs (mostly when using S3 for storing files) -# cf. http://stackoverflow.com/a/17108137/11983 -gem 'addressable' - # perftools only works on 1.9 atm group :profile do # travis refuses to install this, instead of fuffing, just avoid it for now diff --git a/Gemfile.lock b/Gemfile.lock index 775156630..61e823047 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -407,7 +407,6 @@ PLATFORMS DEPENDENCIES actionpack-action_caching active_model_serializers (~> 0.8.0) - addressable annotate barber better_errors diff --git a/lib/file_helper.rb b/lib/file_helper.rb index b6029f1f3..9a2b18b24 100644 --- a/lib/file_helper.rb +++ b/lib/file_helper.rb @@ -1,15 +1,4 @@ require "open-uri" -require "addressable/uri" - -class URI::Parser - - # HACK to support underscores in URLs - def split(url) - a = Addressable::URI::parse(url) - [a.scheme, a.userinfo, a.host, a.port, nil, a.path, nil, a.query, a.fragment] - end - -end class FileHelper @@ -20,7 +9,7 @@ class FileHelper def self.download(url, max_file_size, tmp_file_name, follow_redirect=false) raise Discourse::InvalidParameters.new(:url) unless url =~ /^https?:\/\// - uri = URI.parse(url) + uri = parse_url(url) extension = File.extname(uri.path) tmp = Tempfile.new([tmp_file_name, extension]) @@ -46,4 +35,15 @@ class FileHelper @@images_regexp ||= /\.(#{images.to_a.join("|")})$/i end + # HACK to support underscores in URLs + # cf. http://stackoverflow.com/a/18938253/11983 + def self.parse_url(url) + URI.parse(url) + rescue URI::InvalidURIError + host = url.match(".+\:\/\/([^\/]+)")[1] + uri = URI.parse(url.sub(host, 'valid-host')) + uri.instance_variable_set("@host", host) + uri + end + end