Refactor Geo file uploader to use a Uploader subclass

parent 8111df89
......@@ -2,72 +2,35 @@ module Geo
class FileUploadService
IAT_LEEWAY = 60.seconds.to_i
attr_reader :params, :auth_header
attr_reader :object_type, :object_db_id, :auth_header
def initialize(params, auth_header)
@params = params
@object_type = params[:type]
@object_db_id = params[:id]
@auth_header = auth_header
end
def execute
# Returns { code: :ok, file: CarrierWave File object } upon success
data = ::Gitlab::Geo::JwtRequestDecoder.new(auth_header).decode
return unless data.present?
response =
case params[:type]
when 'avatar'
handle_avatar_geo_request(params[:id], data)
when 'lfs'
handle_lfs_geo_request(params[:id], data)
else
{}
end
response
end
def handle_avatar_geo_request(id, message)
status = { code: :not_found, message: 'Avatar file not found' }
upload = Upload.find(id)
return status unless upload.present?
if message[:id] != upload.model_id || message[:type] != upload.model_type || message[:checksum] != upload.checksum
return status
end
unless upload.model.avatar&.exists?
status[:message] = "#{upload.model_type} does not have a avatar"
return status
begin
uploader_class.new(object_db_id, data).execute
rescue NameError
log("unknown file type: #{object_type}")
{}
end
status[:code] = :ok
status[:message] = 'Success'
status[:file] = upload.model.avatar
status
end
def handle_lfs_geo_request(id, message)
status = { code: :not_found, message: 'LFS object not found' }
lfs_object = LfsObject.find(id)
private
return status unless lfs_object.present?
if message[:sha256] != lfs_object.oid
return status
end
unless lfs_object.file.present? && lfs_object.file.exists?
status[:message] = "LFS object does not have a file"
return status
end
def uploader_class
"Gitlab::Geo::#{object_type.camelize}Uploader".constantize
end
status[:code] = :ok
status[:message] = "Success"
status[:file] = lfs_object.file
status
def log(message)
Rails.logger.info "#{self.class.name}: #{message}"
end
end
end
module Gitlab
module Geo
class AvatarUploader < FileUploader
def execute
upload = Upload.find(object_db_id)
return error unless upload.present?
return error unless valid?(upload)
unless upload.model.avatar&.exists?
return error("#{upload.model_type} does not have a avatar")
end
success(upload.model.avatar)
end
private
def valid?(upload)
message[:id] == upload.model_id && message[:type] == upload.model_type &&
message[:checksum] == upload.checksum
end
end
end
end
module Gitlab
module Geo
class FileUploader
attr_reader :object_db_id, :message
def initialize(object_db_id, message)
@object_db_id = object_db_id
@message = message
end
def execute
raise NotImplementedError
end
def success(file)
{ code: :ok, message: 'Success', file: file }
end
def error(message = 'File not found')
{ code: :not_found, message: message }
end
end
end
end
module Gitlab
module Geo
class LfsUploader < FileUploader
def execute
lfs_object = LfsObject.find(object_db_id)
return error unless lfs_object.present?
return error if message[:sha256] != lfs_object.oid
unless lfs_object.file.present? && lfs_object.file.exists?
return error('LFS object does not have a file')
end
success(lfs_object.file)
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