Commit cca4fdf1 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Use memoize with retrievers and extract useful methods

parent a5d69905
......@@ -9,36 +9,34 @@ module Gitlab
#
class FileRetriever < BaseRetriever
def execute
recorded_file = fetch_resource
return error('Upload not found') unless recorded_file
return file_not_found(recorded_file) unless recorded_file.exist?
return error('Upload not found') unless valid?(recorded_file)
return error('Upload not found') unless valid?
success(CarrierWave::SanitizedFile.new(recorded_file.absolute_path))
end
private
# rubocop: disable CodeReuse/ActiveRecord
def fetch_resource
Upload.find_by(id: object_db_id)
def recorded_file
strong_memoize(:recorded_file) do
Upload.find_by_id(object_db_id)
end
end
# rubocop: enable CodeReuse/ActiveRecord
def valid?(recorded_file)
matches_requested_model?(recorded_file) &&
matches_checksum?(recorded_file)
def valid?
matches_requested_model? && matches_checksum?
end
def matches_requested_model?(recorded_file)
def matches_requested_model?
message[:id] == recorded_file.model_id &&
message[:type] == recorded_file.model_type
end
def matches_checksum?(recorded_file)
def matches_checksum?
# Remove this when we implement checksums for files on the Object Storage
return true unless recorded_file.local?
message[:checksum] == Upload.hexdigest(recorded_file.absolute_path)
end
end
......
......@@ -9,8 +9,6 @@ module Gitlab
#
class JobArtifactRetriever < BaseRetriever
def execute
job_artifact = fetch_resource
unless job_artifact.present?
return error('Job artifact not found')
end
......@@ -26,13 +24,11 @@ module Gitlab
private
# rubocop: disable CodeReuse/ActiveRecord
def fetch_resource
::Ci::JobArtifact.find_by(id: object_db_id)
def job_artifact
strong_memoize(:job_artifact) do
::Ci::JobArtifact.find_by_id(object_db_id)
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
......
......@@ -9,10 +9,8 @@ module Gitlab
#
class LfsRetriever < BaseRetriever
def execute
lfs_object = fetch_resource
return error('LFS object not found') unless lfs_object
return error('LFS object not found') if message[:checksum] != lfs_object.oid
return error('LFS object not found') unless matches_checksum?
unless lfs_object.file.present? && lfs_object.file.exists?
log_error("Could not upload LFS object because it does not have a file", id: lfs_object.id)
......@@ -25,13 +23,15 @@ module Gitlab
private
# rubocop: disable CodeReuse/ActiveRecord
def fetch_resource
LfsObject.find_by(id: object_db_id)
def lfs_object
strong_memoize(:lfs_object) do
LfsObject.find_by_id(object_db_id)
end
end
# rubocop: enable CodeReuse/ActiveRecord
def matches_checksum?
message[:checksum] == lfs_object.oid
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment