send_file_upload.rb 936 Bytes
Newer Older
1
module SendFileUpload
2
  def send_upload(file_upload, send_params: {}, redirect_params: {}, attachment: nil, disposition: 'attachment')
3
    if attachment
4
      redirect_params[:query] = { "response-content-disposition" => "#{disposition};filename=#{attachment.inspect}" }
5 6 7 8
      # 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'
9
      send_params.merge!(filename: attachment, disposition: disposition)
10 11 12 13
    end

    if file_upload.file_storage?
      send_file file_upload.path, send_params
14
    elsif file_upload.class.proxy_download_enabled?
Micaël Bergeron's avatar
Micaël Bergeron committed
15 16
      headers.store(*Gitlab::Workhorse.send_url(file_upload.url(**redirect_params)))
      head :ok
17 18 19 20 21
    else
      redirect_to file_upload.url(**redirect_params)
    end
  end
end