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

82 lines
1.7 KiB
Ruby
Raw Normal View History

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, :upload_finalize]
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
authorized = LfsObjectUploader.workhorse_authorize
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
render plain: 'Unprocessable entity', status: 422
end
2018-03-23 06:07:22 -04:00
rescue ActiveRecord::RecordInvalid
render_400
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
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
2018-03-23 06:07:22 -04:00
def create_file!(oid, size)
LfsObject.new(oid: oid, size: size).tap do |object|
object.file.store_workhorse_file!(params, :file)
object.save!
end
2016-07-20 12:41:26 -04:00
end
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.projects << storage_project
2018-03-23 06:07:22 -04:00
object.save!
2016-07-20 12:41:26 -04:00
end
end
end