2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-11 12:10:14 -05:00
|
|
|
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,
|
2020-04-30 14:09:38 -04:00
|
|
|
Gitlab::Json.parse(Base64.urlsafe_decode64(header))
|
2016-02-11 12:10:14 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2016-08-19 13:10:41 -04:00
|
|
|
|
|
|
|
def workhorse_internal_api_request_header
|
|
|
|
{ 'HTTP_' + Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER.upcase.tr('-', '_') => jwt_token }
|
|
|
|
end
|
2019-09-02 12:41:18 -04:00
|
|
|
|
|
|
|
# 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-10-18 07:11:44 -04:00
|
|
|
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
|
2020-02-27 04:09:01 -05:00
|
|
|
def workhorse_finalize(url, method: :post, file_key:, params:, headers: {}, send_rewritten_field: false)
|
2020-04-14 23:09:11 -04:00
|
|
|
workhorse_finalize_with_multiple_files(url, method: method, file_keys: file_key, params: params, headers: headers, send_rewritten_field: send_rewritten_field)
|
|
|
|
end
|
|
|
|
|
|
|
|
def workhorse_finalize_with_multiple_files(url, method: :post, file_keys:, params:, headers: {}, send_rewritten_field: false)
|
|
|
|
workhorse_request_with_multiple_files(method, url,
|
|
|
|
file_keys: file_keys,
|
|
|
|
params: params,
|
|
|
|
extra_headers: headers,
|
|
|
|
send_rewritten_field: send_rewritten_field
|
2019-10-18 07:11:44 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def workhorse_request_with_file(method, url, file_key:, params:, env: {}, extra_headers: {}, send_rewritten_field:)
|
2020-04-14 23:09:11 -04:00
|
|
|
workhorse_request_with_multiple_files(method, url, file_keys: file_key, params: params, env: env, extra_headers: extra_headers, send_rewritten_field: send_rewritten_field)
|
|
|
|
end
|
|
|
|
|
|
|
|
def workhorse_request_with_multiple_files(method, url, file_keys:, params:, env: {}, extra_headers: {}, send_rewritten_field:)
|
2019-09-02 12:41:18 -04:00
|
|
|
workhorse_params = params.dup
|
|
|
|
|
2020-04-14 23:09:11 -04:00
|
|
|
file_keys = Array(file_keys)
|
|
|
|
rewritten_fields = {}
|
|
|
|
|
|
|
|
file_keys.each do |key|
|
|
|
|
file = workhorse_params.delete(key)
|
|
|
|
rewritten_fields[key] = file.path if file
|
|
|
|
workhorse_params = workhorse_disk_accelerated_file_params(key, file).merge(workhorse_params)
|
|
|
|
end
|
2019-10-18 07:11:44 -04:00
|
|
|
|
|
|
|
headers = if send_rewritten_field
|
2020-04-14 23:09:11 -04:00
|
|
|
workhorse_rewritten_fields_header(rewritten_fields)
|
2019-10-18 07:11:44 -04:00
|
|
|
else
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
headers.merge!(extra_headers)
|
2019-09-02 12:41:18 -04:00
|
|
|
|
2019-10-18 07:11:44 -04:00
|
|
|
process(method, url, params: workhorse_params, headers: headers, env: env)
|
2019-09-02 12:41:18 -04:00
|
|
|
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-10-18 07:11:44 -04:00
|
|
|
return {} unless file
|
|
|
|
|
2019-09-02 12:41:18 -04:00
|
|
|
{
|
|
|
|
"#{key}.name" => file.original_filename,
|
2019-10-18 07:11:44 -04:00
|
|
|
"#{key}.size" => file.size
|
|
|
|
}.tap do |params|
|
2020-04-14 23:09:11 -04:00
|
|
|
if file.path
|
|
|
|
params["#{key}.path"] = file.path
|
|
|
|
params["#{key}.sha256"] = Digest::SHA256.file(file.path).hexdigest
|
|
|
|
end
|
|
|
|
|
2020-03-26 14:08:03 -04:00
|
|
|
params["#{key}.remote_id"] = file.remote_id if file.respond_to?(:remote_id) && file.remote_id.present?
|
2019-10-18 07:11:44 -04:00
|
|
|
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-09-02 12:41:18 -04:00
|
|
|
end
|
2016-02-17 09:22:33 -05:00
|
|
|
end
|