gitlab-org--gitlab-foss/app/controllers/projects/lfs_storage_controller.rb

90 lines
2.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-07-20 12:41:26 -04:00
class Projects::LfsStorageController < Projects::GitHttpClientController
include LfsRequest
include WorkhorseRequest
include SendFileUpload
2016-07-20 12:41:26 -04:00
skip_before_action :verify_workhorse_api!, only: :download
2016-07-20 12:41:26 -04:00
def download
lfs_object = LfsObject.find_by_oid(oid)
unless lfs_object && lfs_object.file.exists?
render_lfs_not_found
return
end
send_upload(lfs_object.file, send_params: { content_type: "application/octet-stream" })
2016-07-20 12:41:26 -04:00
end
def upload_authorize
set_workhorse_internal_api_content_type
2018-03-23 06:07:22 -04:00
2018-05-09 11:27:38 -04:00
authorized = LfsObjectUploader.workhorse_authorize(has_length: true)
2018-03-23 06:07:22 -04:00
authorized.merge!(LfsOid: oid, LfsSize: size)
render json: authorized
2016-07-20 12:41:26 -04:00
end
def upload_finalize
2018-03-23 06:07:22 -04:00
if store_file!(oid, size)
2016-07-20 12:41:26 -04:00
head 200
else
2018-07-02 06:43:06 -04:00
render plain: 'Unprocessable entity', status: :unprocessable_entity
2016-07-20 12:41:26 -04:00
end
2018-03-23 06:07:22 -04:00
rescue ActiveRecord::RecordInvalid
render_lfs_forbidden
rescue UploadedFile::InvalidPathError
render_lfs_forbidden
2018-03-23 06:07:22 -04:00
rescue ObjectStorage::RemoteStoreError
render_lfs_forbidden
2016-07-20 12:41:26 -04:00
end
private
def download_request?
action_name == 'download'
end
def upload_request?
%w[upload_authorize upload_finalize].include? action_name
end
def oid
params[:oid].to_s
end
def size
params[:size].to_i
end
# rubocop: disable CodeReuse/ActiveRecord
2018-03-23 06:07:22 -04:00
def store_file!(oid, size)
object = LfsObject.find_by(oid: oid, size: size)
unless object&.file&.exists?
object = create_file!(oid, size)
end
2016-07-20 12:41:26 -04:00
2018-03-23 06:07:22 -04:00
return unless object
2016-07-20 12:41:26 -04:00
2018-03-23 06:07:22 -04:00
link_to_project!(object)
2016-07-20 12:41:26 -04:00
end
# rubocop: enable CodeReuse/ActiveRecord
2016-07-20 12:41:26 -04:00
2018-03-23 06:07:22 -04:00
def create_file!(oid, size)
uploaded_file = UploadedFile.from_params(
params, :file, LfsObjectUploader.workhorse_local_upload_path)
return unless uploaded_file
LfsObject.create!(oid: oid, size: size, file: uploaded_file)
2016-07-20 12:41:26 -04:00
end
# rubocop: disable CodeReuse/ActiveRecord
2018-03-23 06:07:22 -04:00
def link_to_project!(object)
2016-07-20 12:41:26 -04:00
if object && !object.projects.exists?(storage_project.id)
object.lfs_objects_projects.create!(project: storage_project)
2016-07-20 12:41:26 -04:00
end
end
# rubocop: enable CodeReuse/ActiveRecord
2016-07-20 12:41:26 -04:00
end