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 ...@@ -9,36 +9,34 @@ module Gitlab
# #
class FileRetriever < BaseRetriever class FileRetriever < BaseRetriever
def execute def execute
recorded_file = fetch_resource
return error('Upload not found') unless recorded_file return error('Upload not found') unless recorded_file
return file_not_found(recorded_file) unless recorded_file.exist? 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)) success(CarrierWave::SanitizedFile.new(recorded_file.absolute_path))
end end
private private
# rubocop: disable CodeReuse/ActiveRecord def recorded_file
strong_memoize(:recorded_file) do
def fetch_resource Upload.find_by_id(object_db_id)
Upload.find_by(id: object_db_id) end
end end
# rubocop: enable CodeReuse/ActiveRecord def valid?
matches_requested_model? && matches_checksum?
def valid?(recorded_file)
matches_requested_model?(recorded_file) &&
matches_checksum?(recorded_file)
end end
def matches_requested_model?(recorded_file) def matches_requested_model?
message[:id] == recorded_file.model_id && message[:id] == recorded_file.model_id &&
message[:type] == recorded_file.model_type message[:type] == recorded_file.model_type
end 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) message[:checksum] == Upload.hexdigest(recorded_file.absolute_path)
end end
end end
......
...@@ -9,8 +9,6 @@ module Gitlab ...@@ -9,8 +9,6 @@ module Gitlab
# #
class JobArtifactRetriever < BaseRetriever class JobArtifactRetriever < BaseRetriever
def execute def execute
job_artifact = fetch_resource
unless job_artifact.present? unless job_artifact.present?
return error('Job artifact not found') return error('Job artifact not found')
end end
...@@ -26,13 +24,11 @@ module Gitlab ...@@ -26,13 +24,11 @@ module Gitlab
private private
# rubocop: disable CodeReuse/ActiveRecord def job_artifact
strong_memoize(:job_artifact) do
def fetch_resource ::Ci::JobArtifact.find_by_id(object_db_id)
::Ci::JobArtifact.find_by(id: object_db_id) end
end end
# rubocop: enable CodeReuse/ActiveRecord
end end
end end
end end
......
...@@ -9,10 +9,8 @@ module Gitlab ...@@ -9,10 +9,8 @@ module Gitlab
# #
class LfsRetriever < BaseRetriever class LfsRetriever < BaseRetriever
def execute def execute
lfs_object = fetch_resource
return error('LFS object not found') unless lfs_object 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? 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) log_error("Could not upload LFS object because it does not have a file", id: lfs_object.id)
...@@ -25,13 +23,15 @@ module Gitlab ...@@ -25,13 +23,15 @@ module Gitlab
private private
# rubocop: disable CodeReuse/ActiveRecord def lfs_object
strong_memoize(:lfs_object) do
def fetch_resource LfsObject.find_by_id(object_db_id)
LfsObject.find_by(id: object_db_id) end
end end
# rubocop: enable CodeReuse/ActiveRecord def matches_checksum?
message[:checksum] == lfs_object.oid
end
end end
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