debian-mirror-gitlab/app/services/projects/lfs_pointers/lfs_download_service.rb

122 lines
3.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-11-08 19:23:39 +05:30
# This service downloads and links lfs objects from a remote URL
module Projects
module LfsPointers
class LfsDownloadService < BaseService
2019-02-02 18:00:53 +05:30
SizeError = Class.new(StandardError)
OidError = Class.new(StandardError)
2018-12-15 14:41:45 +05:30
2019-02-02 18:00:53 +05:30
attr_reader :lfs_download_object
delegate :oid, :size, :credentials, :sanitized_url, to: :lfs_download_object, prefix: :lfs
2018-11-08 19:23:39 +05:30
2019-02-02 18:00:53 +05:30
def initialize(project, lfs_download_object)
super(project)
2018-11-08 19:23:39 +05:30
2019-02-02 18:00:53 +05:30
@lfs_download_object = lfs_download_object
end
2018-11-08 19:23:39 +05:30
2019-02-02 18:00:53 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def execute
return unless project&.lfs_enabled? && lfs_download_object
return error("LFS file with oid #{lfs_oid} has invalid attributes") unless lfs_download_object.valid?
return if LfsObject.exists?(oid: lfs_oid)
2018-11-08 19:23:39 +05:30
2019-02-02 18:00:53 +05:30
wrap_download_errors do
download_lfs_file!
2018-11-08 19:23:39 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-11-08 19:23:39 +05:30
private
2019-02-02 18:00:53 +05:30
def wrap_download_errors(&block)
yield
rescue SizeError, OidError, StandardError => e
error("LFS file with oid #{lfs_oid} could't be downloaded from #{lfs_sanitized_url}: #{e.message}")
end
def download_lfs_file!
with_tmp_file do |tmp_file|
download_and_save_file!(tmp_file)
project.all_lfs_objects << LfsObject.new(oid: lfs_oid,
size: lfs_size,
file: tmp_file)
success
2019-01-03 12:48:30 +05:30
end
end
2019-02-02 18:00:53 +05:30
def download_and_save_file!(file)
digester = Digest::SHA256.new
response = Gitlab::HTTP.get(lfs_sanitized_url, download_headers) do |fragment|
digester << fragment
2019-01-03 12:48:30 +05:30
file.write(fragment)
2019-02-02 18:00:53 +05:30
raise_size_error! if file.size > lfs_size
2019-01-03 12:48:30 +05:30
end
raise StandardError, "Received error code #{response.code}" unless response.success?
2018-11-08 19:23:39 +05:30
2019-02-02 18:00:53 +05:30
raise_size_error! if file.size != lfs_size
raise_oid_error! if digester.hexdigest != lfs_oid
end
2018-11-08 19:23:39 +05:30
2019-02-02 18:00:53 +05:30
def download_headers
{ stream_body: true }.tap do |headers|
if lfs_credentials[:user].present? || lfs_credentials[:password].present?
2018-11-08 19:23:39 +05:30
# Using authentication headers in the request
2019-02-02 18:00:53 +05:30
headers[:basic_auth] = { username: lfs_credentials[:user], password: lfs_credentials[:password] }
2018-11-08 19:23:39 +05:30
end
end
end
2019-02-02 18:00:53 +05:30
def with_tmp_file
2018-11-08 19:23:39 +05:30
create_tmp_storage_dir
2019-02-02 18:00:53 +05:30
File.open(tmp_filename, 'wb') do |file|
2019-05-18 00:54:41 +05:30
yield file
rescue StandardError => e
# If the lfs file is successfully downloaded it will be removed
# when it is added to the project's lfs files.
# Nevertheless if any excetion raises the file would remain
# in the file system. Here we ensure to remove it
File.unlink(file) if File.exist?(file)
raise e
2019-02-02 18:00:53 +05:30
end
end
def tmp_filename
File.join(tmp_storage_dir, lfs_oid)
2018-11-08 19:23:39 +05:30
end
def create_tmp_storage_dir
FileUtils.makedirs(tmp_storage_dir) unless Dir.exist?(tmp_storage_dir)
end
def tmp_storage_dir
@tmp_storage_dir ||= File.join(storage_dir, 'tmp', 'download')
end
def storage_dir
@storage_dir ||= Gitlab.config.lfs.storage_path
end
2019-02-02 18:00:53 +05:30
def raise_size_error!
raise SizeError, 'Size mistmatch'
end
def raise_oid_error!
raise OidError, 'Oid mismatch'
end
def error(message, http_status = nil)
log_error(message)
super
end
2018-11-08 19:23:39 +05:30
end
end
end