Skip to content
Snippets Groups Projects

Encode Content-Disposition filenames

Merged Stan Hu requested to merge sh-encode-content-disposition into master
All threads resolved!
Files
2
@@ -3,18 +3,25 @@
@@ -3,18 +3,25 @@
module SendFileUpload
module SendFileUpload
def send_upload(file_upload, send_params: {}, redirect_params: {}, attachment: nil, proxy: false, disposition: 'attachment')
def send_upload(file_upload, send_params: {}, redirect_params: {}, attachment: nil, proxy: false, disposition: 'attachment')
if attachment
if attachment
disposition = ::Gitlab::ContentDisposition.format(disposition: disposition, filename: attachment)
response_disposition = ::Gitlab::ContentDisposition.format(disposition: 'attachment', filename: attachment)
# Response-Content-Type will not override an existing Content-Type in
# Response-Content-Type will not override an existing Content-Type in
# Google Cloud Storage, so the metadata needs to be cleared on GCS for
# Google Cloud Storage, so the metadata needs to be cleared on GCS for
# this to work. However, this override works with AWS.
# this to work. However, this override works with AWS.
redirect_params[:query] = { "response-content-disposition" => disposition,
redirect_params[:query] = { "response-content-disposition" => response_disposition,
"response-content-type" => guess_content_type(attachment) }
"response-content-type" => guess_content_type(attachment) }
# By default, Rails will send uploads with an extension of .js with a
# By default, Rails will send uploads with an extension of .js with a
# content-type of text/javascript, which will trigger Rails'
# content-type of text/javascript, which will trigger Rails'
# cross-origin JavaScript protection.
# cross-origin JavaScript protection.
send_params[:content_type] = 'text/plain' if File.extname(attachment) == '.js'
send_params[:content_type] = 'text/plain' if File.extname(attachment) == '.js'
send_params.merge!(filename: attachment, disposition: disposition)
 
# Rails 5 will append an extra `; filename#{filename}` here:
 
# https://github.com/rails/rails/blob/v5.0.7/actionpack/lib/action_controller/metal/data_streaming.rb#L137
 
# We're including the full Content-Disposition because we want the
 
# properly encoded field. Rails 6 will have
 
# https://github.com/rails/rails/pull/33829, and we can simplify
 
# the disposition argument to `attachment`.
 
send_params.merge!(filename: attachment, disposition: response_disposition)
end
end
if file_upload.file_storage?
if file_upload.file_storage?
Loading