Commit 1acc39c6 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Extract `BaseUploader` and use it on other classes

`BaseUploader` is now the base class for `FileUploader`,
`JobArtifactUploader` and `LfsUploader`
parent 002819ad
# frozen_string_literal: true
module Gitlab
module Geo
module Replication
class BaseUploader
include LogHelpers
FILE_NOT_FOUND_GEO_CODE = 'FILE_NOT_FOUND'.freeze
attr_reader :object_db_id, :message
def initialize(object_db_id, message)
@object_db_id = object_db_id
@message = message
end
private
def success(file)
{ code: :ok, message: 'Success', file: file }
end
def error(message)
{ code: :not_found, message: message }
end
# A 404 implies the client made a mistake requesting that resource.
# In this case, we know that the resource should exist, so it is a 500 server error.
# We send a special "geo_code" so the secondary can mark the file as synced.
def file_not_found(resource)
{
code: :not_found,
geo_code: FILE_NOT_FOUND_GEO_CODE,
message: "#{resource.class.name} ##{resource.id} file not found"
}
end
end
end
end
end
...@@ -7,23 +7,9 @@ module Gitlab ...@@ -7,23 +7,9 @@ module Gitlab
# * Finding an Upload record # * Finding an Upload record
# * Returning the necessary response data to send the file back # * Returning the necessary response data to send the file back
# #
# TODO: Rearrange things so this class not inherited by JobArtifactUploader and LfsUploader class FileUploader < BaseUploader
# Maybe rename it so it doesn't seem generic. It only works with Upload records.
class FileUploader
include LogHelpers
FILE_NOT_FOUND_GEO_CODE = 'FILE_NOT_FOUND'.freeze
attr_reader :object_db_id, :message
def initialize(object_db_id, message)
@object_db_id = object_db_id
@message = message
end
# rubocop: disable CodeReuse/ActiveRecord
def execute def execute
recorded_file = Upload.find_by(id: object_db_id) 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?
...@@ -32,10 +18,16 @@ module Gitlab ...@@ -32,10 +18,16 @@ module Gitlab
success(CarrierWave::SanitizedFile.new(recorded_file.absolute_path)) success(CarrierWave::SanitizedFile.new(recorded_file.absolute_path))
end end
# rubocop: enable CodeReuse/ActiveRecord
private private
# rubocop: disable CodeReuse/ActiveRecord
def fetch_resource
Upload.find_by(id: object_db_id)
end
# rubocop: enable CodeReuse/ActiveRecord
def valid?(recorded_file) def valid?(recorded_file)
matches_requested_model?(recorded_file) && matches_requested_model?(recorded_file) &&
matches_checksum?(recorded_file) matches_checksum?(recorded_file)
...@@ -49,25 +41,6 @@ module Gitlab ...@@ -49,25 +41,6 @@ module Gitlab
def matches_checksum?(recorded_file) def matches_checksum?(recorded_file)
message[:checksum] == Upload.hexdigest(recorded_file.absolute_path) message[:checksum] == Upload.hexdigest(recorded_file.absolute_path)
end end
def success(file)
{ code: :ok, message: 'Success', file: file }
end
def error(message)
{ code: :not_found, message: message }
end
# A 404 implies the client made a mistake requesting that resource.
# In this case, we know that the resource should exist, so it is a 500 server error.
# We send a special "geo_code" so the secondary can mark the file as synced.
def file_not_found(resource)
{
code: :not_found,
geo_code: FILE_NOT_FOUND_GEO_CODE,
message: "#{resource.class.name} ##{resource.id} file not found"
}
end
end end
end end
end end
......
...@@ -7,11 +7,9 @@ module Gitlab ...@@ -7,11 +7,9 @@ module Gitlab
# * Finding an ::Ci::JobArtifact record # * Finding an ::Ci::JobArtifact record
# * Returning the necessary response data to send the file back # * Returning the necessary response data to send the file back
# #
# TODO: Rearrange things so this class does not inherit from FileUploader class JobArtifactUploader < BaseUploader
class JobArtifactUploader < FileUploader
# rubocop: disable CodeReuse/ActiveRecord
def execute def execute
job_artifact = ::Ci::JobArtifact.find_by(id: object_db_id) job_artifact = fetch_resource
unless job_artifact.present? unless job_artifact.present?
return error('Job artifact not found') return error('Job artifact not found')
...@@ -25,6 +23,15 @@ module Gitlab ...@@ -25,6 +23,15 @@ module Gitlab
success(job_artifact.file) success(job_artifact.file)
end end
private
# rubocop: disable CodeReuse/ActiveRecord
def fetch_resource
::Ci::JobArtifact.find_by(id: object_db_id)
end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
end end
end end
......
...@@ -7,11 +7,9 @@ module Gitlab ...@@ -7,11 +7,9 @@ module Gitlab
# * Finding an LfsObject record # * Finding an LfsObject record
# * Returning the necessary response data to send the file back # * Returning the necessary response data to send the file back
# #
# TODO: Rearrange things so this class does not inherit from FileUploader class LfsUploader < BaseUploader
class LfsUploader < FileUploader
# rubocop: disable CodeReuse/ActiveRecord
def execute def execute
lfs_object = LfsObject.find_by(id: object_db_id) 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') if message[:checksum] != lfs_object.oid
...@@ -24,6 +22,15 @@ module Gitlab ...@@ -24,6 +22,15 @@ module Gitlab
success(lfs_object.file) success(lfs_object.file)
end end
private
# rubocop: disable CodeReuse/ActiveRecord
def fetch_resource
LfsObject.find_by(id: object_db_id)
end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
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