From 040dee7432155dfea15097ed8c5e16edf6f78a3f Mon Sep 17 00:00:00 2001 From: Kaname Asakura Date: Mon, 16 Mar 2020 17:47:50 +0900 Subject: [PATCH] encode Content-Disposition filename --- lib/refile/app.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/refile/app.rb b/lib/refile/app.rb index e61c7942..b51c5d12 100644 --- a/lib/refile/app.rb +++ b/lib/refile/app.rb @@ -136,11 +136,18 @@ def stream_file(file) end filename = Rack::Utils.unescape(request.path.split("/").last) - disposition = force_download?(params) ? "attachment" : "inline" + disposition = force_download?(params) ? "attachment; filename*=UTF-8''#{percent_escape(filename)}" : "inline" send_file path, filename: filename, disposition: disposition, type: ::File.extname(filename) end + # https://github.com/rails/rails/pull/33829 + def percent_escape(filename) + filename.gsub(/[^A-Za-z0-9!#$&+.^_`|~-]/) do |char| + char.bytes.map { |byte| "%%%02X" % byte }.join + end + end + def backend Refile.backends.fetch(params[:backend]) do |name| log_error("Could not find backend: #{name}")