Merge pull request #13108 from Bo98/basename-no-query

download_strategy: strip query string from basename
This commit is contained in:
Bo Anderson 2022-04-11 18:22:52 +01:00 committed by GitHub
commit a90b781abc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -345,17 +345,19 @@ class AbstractFileDownloadStrategy < AbstractDownloadStrategy
end end
uri_path = URI.decode_www_form_component(uri_path) uri_path = URI.decode_www_form_component(uri_path)
query_regex = /[^?&]+/
# We need a Pathname because we've monkeypatched extname to support double # We need a Pathname because we've monkeypatched extname to support double
# extensions (e.g. tar.gz). # extensions (e.g. tar.gz).
# Given a URL like https://example.com/download.php?file=foo-1.0.tar.gz # Given a URL like https://example.com/download.php?file=foo-1.0.tar.gz
# the basename we want is "foo-1.0.tar.gz", not "download.php". # the basename we want is "foo-1.0.tar.gz", not "download.php".
Pathname.new(uri_path).ascend do |path| Pathname.new(uri_path).ascend do |path|
ext = path.extname[/[^?&]+/] ext = path.extname[query_regex]
return path.basename.to_s[/[^?&]+#{Regexp.escape(ext)}/] if ext return path.basename.to_s[/#{query_regex.source}#{Regexp.escape(ext)}/] if ext
end end
File.basename(uri_path) # Strip query string
File.basename(uri_path)[query_regex]
end end
end end