2021-01-29 00:20:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DependencyProxy
|
|
|
|
class FindOrCreateBlobService < DependencyProxy::BaseService
|
|
|
|
def initialize(group, image, token, blob_sha)
|
|
|
|
@group = group
|
|
|
|
@image = image
|
|
|
|
@token = token
|
|
|
|
@blob_sha = blob_sha
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-10-27 15:23:28 +05:30
|
|
|
from_cache = true
|
2021-01-29 00:20:46 +05:30
|
|
|
file_name = @blob_sha.sub('sha256:', '') + '.gz'
|
2021-11-18 22:05:49 +05:30
|
|
|
blob = @group.dependency_proxy_blobs.active.find_or_build(file_name)
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
unless blob.persisted?
|
2021-10-27 15:23:28 +05:30
|
|
|
from_cache = false
|
2021-01-29 00:20:46 +05:30
|
|
|
result = DependencyProxy::DownloadBlobService
|
|
|
|
.new(@image, @blob_sha, @token).execute
|
|
|
|
|
|
|
|
if result[:status] == :error
|
|
|
|
log_failure(result)
|
|
|
|
|
|
|
|
return error('Failed to download the blob', result[:http_status])
|
|
|
|
end
|
|
|
|
|
|
|
|
blob.file = result[:file]
|
|
|
|
blob.size = result[:file].size
|
|
|
|
blob.save!
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
blob.read! if from_cache
|
2021-10-27 15:23:28 +05:30
|
|
|
success(blob: blob, from_cache: from_cache)
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def log_failure(result)
|
|
|
|
log_error(
|
|
|
|
"Dependency proxy: Failed to download the blob." \
|
|
|
|
"Blob sha: #{@blob_sha}." \
|
|
|
|
"Error message: #{result[:message][0, 100]}" \
|
|
|
|
"HTTP status: #{result[:http_status]}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|