debian-mirror-gitlab/lib/gitlab/import_export/after_export_strategies/web_upload_strategy.rb

67 lines
1.6 KiB
Ruby
Raw Normal View History

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?
2019-07-07 11:18:12 +05:30
raise StrategyError.new("Error uploading the project. Code #{response.code}: #{response.message}")
2018-05-09 12:01:36 +05:30
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