2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
module AfterExportStrategies
|
|
|
|
class WebUploadStrategy < BaseAfterExportStrategy
|
|
|
|
PUT_METHOD = 'PUT'.freeze
|
|
|
|
POST_METHOD = 'POST'.freeze
|
|
|
|
INVALID_HTTP_METHOD = 'invalid. Only PUT and POST methods allowed.'.freeze
|
|
|
|
|
|
|
|
validates :url, url: true
|
|
|
|
|
|
|
|
validate do
|
|
|
|
unless [PUT_METHOD, POST_METHOD].include?(http_method.upcase)
|
|
|
|
errors.add(:http_method, INVALID_HTTP_METHOD)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(url:, http_method: PUT_METHOD)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def strategy_execute
|
|
|
|
handle_response_error(send_file)
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
project.remove_exports
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def handle_response_error(response)
|
|
|
|
unless response.success?
|
|
|
|
error_code = response.dig('Error', 'Code') || response.code
|
|
|
|
error_message = response.dig('Error', 'Message') || response.message
|
|
|
|
|
|
|
|
raise StrategyError.new("Error uploading the project. Code #{error_code}: #{error_message}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def send_file
|
2018-11-08 19:23:39 +05:30
|
|
|
Gitlab::HTTP.public_send(http_method.downcase, url, send_file_options) # rubocop:disable GitlabSecurity/PublicSend
|
2018-05-09 12:01:36 +05:30
|
|
|
ensure
|
2018-11-20 20:47:30 +05:30
|
|
|
export_file.close if export_file
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def export_file
|
2018-11-20 20:47:30 +05:30
|
|
|
project.export_file.open
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def send_file_options
|
2018-05-09 12:01:36 +05:30
|
|
|
{
|
|
|
|
body_stream: export_file,
|
|
|
|
headers: headers
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def headers
|
2018-11-08 19:23:39 +05:30
|
|
|
{ 'Content-Length' => export_size.to_s }
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_size
|
2018-11-20 20:47:30 +05:30
|
|
|
project.export_file.file.size
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|