debian-mirror-gitlab/spec/support/helpers/workhorse_helpers.rb

93 lines
3 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
module WorkhorseHelpers
extend self
def workhorse_send_data
@_workhorse_send_data ||= begin
header = response.headers[Gitlab::Workhorse::SEND_DATA_HEADER]
split_header = header.split(':')
type = split_header.shift
header = split_header.join(':')
[
type,
2017-09-10 17:25:29 +05:30
JSON.parse(Base64.urlsafe_decode64(header))
2016-06-02 11:05:42 +05:30
]
end
end
2016-09-29 09:46:39 +05:30
def workhorse_internal_api_request_header
{ 'HTTP_' + Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER.upcase.tr('-', '_') => jwt_token }
end
2019-12-04 20:38:33 +05:30
# workhorse_post_with_file will transform file_key inside params as if it was disk accelerated by workhorse
def workhorse_post_with_file(url, file_key:, params:)
2019-12-21 20:55:43 +05:30
workhorse_request_with_file(:post, url,
file_key: file_key,
params: params,
env: { 'CONTENT_TYPE' => 'multipart/form-data' },
send_rewritten_field: true
)
end
# workhorse_finalize will transform file_key inside params as if it was the finalize call of an inline object storage upload.
# note that based on the content of the params it can simulate a disc acceleration or an object storage upload
def workhorse_finalize(url, method: :post, file_key:, params:, headers: {})
workhorse_request_with_file(method, url,
file_key: file_key,
params: params,
extra_headers: headers,
send_rewritten_field: false
)
end
def workhorse_request_with_file(method, url, file_key:, params:, env: {}, extra_headers: {}, send_rewritten_field:)
2019-12-04 20:38:33 +05:30
workhorse_params = params.dup
file = workhorse_params.delete(file_key)
2019-12-21 20:55:43 +05:30
workhorse_params = workhorse_disk_accelerated_file_params(file_key, file).merge(workhorse_params)
headers = if send_rewritten_field
workhorse_rewritten_fields_header(file_key => file.path)
else
{}
end
headers.merge!(extra_headers)
2019-12-04 20:38:33 +05:30
2019-12-21 20:55:43 +05:30
process(method, url, params: workhorse_params, headers: headers, env: env)
2019-12-04 20:38:33 +05:30
end
private
def jwt_token(data = {})
JWT.encode({ 'iss' => 'gitlab-workhorse' }.merge(data), Gitlab::Workhorse.secret, 'HS256')
end
def workhorse_rewritten_fields_header(fields)
{ Gitlab::Middleware::Multipart::RACK_ENV_KEY => jwt_token('rewritten_fields' => fields) }
end
def workhorse_disk_accelerated_file_params(key, file)
2019-12-21 20:55:43 +05:30
return {} unless file
2019-12-04 20:38:33 +05:30
{
"#{key}.name" => file.original_filename,
2019-12-21 20:55:43 +05:30
"#{key}.size" => file.size
}.tap do |params|
params["#{key}.path"] = file.path if file.path
2020-03-28 13:19:24 +05:30
params["#{key}.remote_id"] = file.remote_id if file.respond_to?(:remote_id) && file.remote_id.present?
2019-12-21 20:55:43 +05:30
end
end
def fog_to_uploaded_file(file)
filename = File.basename(file.key)
UploadedFile.new(nil,
filename: filename,
remote_id: filename,
size: file.content_length
)
2019-12-04 20:38:33 +05:30
end
2016-06-02 11:05:42 +05:30
end