2018-09-14 01:42:05 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-07 17:27:04 -04:00
|
|
|
module SendFileUpload
|
2018-11-07 10:54:37 -05:00
|
|
|
def send_upload(file_upload, send_params: {}, redirect_params: {}, attachment: nil, proxy: false, disposition: 'attachment')
|
2017-09-07 17:27:04 -04:00
|
|
|
if attachment
|
2019-02-04 20:27:22 -05:00
|
|
|
response_disposition = ::Gitlab::ContentDisposition.format(disposition: 'attachment', filename: attachment)
|
|
|
|
|
2018-08-13 18:36:15 -04:00
|
|
|
# Response-Content-Type will not override an existing Content-Type in
|
|
|
|
# Google Cloud Storage, so the metadata needs to be cleared on GCS for
|
|
|
|
# this to work. However, this override works with AWS.
|
2019-02-04 20:27:22 -05:00
|
|
|
redirect_params[:query] = { "response-content-disposition" => response_disposition,
|
2018-08-13 18:36:15 -04:00
|
|
|
"response-content-type" => guess_content_type(attachment) }
|
2018-05-14 00:43:48 -04:00
|
|
|
# By default, Rails will send uploads with an extension of .js with a
|
|
|
|
# content-type of text/javascript, which will trigger Rails'
|
|
|
|
# cross-origin JavaScript protection.
|
|
|
|
send_params[:content_type] = 'text/plain' if File.extname(attachment) == '.js'
|
2019-02-04 20:27:22 -05:00
|
|
|
|
|
|
|
send_params.merge!(filename: attachment, disposition: utf8_encoded_disposition(disposition, attachment))
|
2017-09-07 17:27:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if file_upload.file_storage?
|
|
|
|
send_file file_upload.path, send_params
|
2018-11-07 10:54:37 -05:00
|
|
|
elsif file_upload.class.proxy_download_enabled? || proxy
|
2018-03-22 14:37:47 -04:00
|
|
|
headers.store(*Gitlab::Workhorse.send_url(file_upload.url(**redirect_params)))
|
|
|
|
head :ok
|
2017-09-07 17:27:04 -04:00
|
|
|
else
|
|
|
|
redirect_to file_upload.url(**redirect_params)
|
|
|
|
end
|
|
|
|
end
|
2018-08-13 18:36:15 -04:00
|
|
|
|
2019-02-04 20:27:22 -05:00
|
|
|
# Since Rails 5 doesn't properly support support non-ASCII filenames,
|
|
|
|
# we have to add our own to ensure RFC 5987 compliance. However, Rails
|
|
|
|
# 5 automatically appends `filename#{filename}` here:
|
|
|
|
# https://github.com/rails/rails/blob/v5.0.7/actionpack/lib/action_controller/metal/data_streaming.rb#L137
|
|
|
|
# Rails 6 will have https://github.com/rails/rails/pull/33829, so we
|
|
|
|
# can get rid of this special case handling when we upgrade.
|
|
|
|
def utf8_encoded_disposition(disposition, filename)
|
|
|
|
content = ::Gitlab::ContentDisposition.new(disposition: disposition, filename: filename)
|
|
|
|
|
|
|
|
"#{disposition}; #{content.utf8_filename}"
|
|
|
|
end
|
|
|
|
|
2018-08-13 18:36:15 -04:00
|
|
|
def guess_content_type(filename)
|
|
|
|
types = MIME::Types.type_for(filename)
|
|
|
|
|
|
|
|
if types.present?
|
|
|
|
types.first.content_type
|
|
|
|
else
|
|
|
|
"application/octet-stream"
|
|
|
|
end
|
|
|
|
end
|
2017-09-07 17:27:04 -04:00
|
|
|
end
|