debian-mirror-gitlab/app/controllers/projects/lfs_storage_controller.rb

90 lines
2.1 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
class Projects::LfsStorageController < Projects::GitHttpClientController
2017-08-17 22:00:37 +05:30
include LfsRequest
include WorkhorseRequest
2018-05-09 12:01:36 +05:30
include SendFileUpload
2016-09-13 17:45:13 +05:30
2019-02-13 22:33:31 +05:30
skip_before_action :verify_workhorse_api!, only: :download
2016-09-13 17:45:13 +05:30
def download
lfs_object = LfsObject.find_by_oid(oid)
unless lfs_object && lfs_object.file.exists?
render_lfs_not_found
return
end
2018-05-09 12:01:36 +05:30
send_upload(lfs_object.file, send_params: { content_type: "application/octet-stream" })
2016-09-13 17:45:13 +05:30
end
def upload_authorize
2016-09-29 09:46:39 +05:30
set_workhorse_internal_api_content_type
2018-05-09 12:01:36 +05:30
2018-11-08 19:23:39 +05:30
authorized = LfsObjectUploader.workhorse_authorize(has_length: true)
2018-05-09 12:01:36 +05:30
authorized.merge!(LfsOid: oid, LfsSize: size)
render json: authorized
2016-09-13 17:45:13 +05:30
end
def upload_finalize
2018-05-09 12:01:36 +05:30
if store_file!(oid, size)
2016-09-13 17:45:13 +05:30
head 200
else
2018-11-18 11:00:15 +05:30
render plain: 'Unprocessable entity', status: :unprocessable_entity
2016-09-13 17:45:13 +05:30
end
2018-05-09 12:01:36 +05:30
rescue ActiveRecord::RecordInvalid
render_lfs_forbidden
rescue UploadedFile::InvalidPathError
render_lfs_forbidden
rescue ObjectStorage::RemoteStoreError
render_lfs_forbidden
2016-09-13 17:45:13 +05:30
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-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-05-09 12:01:36 +05:30
def store_file!(oid, size)
object = LfsObject.find_by(oid: oid, size: size)
unless object&.file&.exists?
object = create_file!(oid, size)
end
2016-09-13 17:45:13 +05:30
2018-05-09 12:01:36 +05:30
return unless object
2016-09-13 17:45:13 +05:30
2018-05-09 12:01:36 +05:30
link_to_project!(object)
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
2018-05-09 12:01:36 +05:30
def create_file!(oid, size)
uploaded_file = UploadedFile.from_params(
params, :file, LfsObjectUploader.workhorse_local_upload_path)
return unless uploaded_file
2016-09-13 17:45:13 +05:30
2018-05-09 12:01:36 +05:30
LfsObject.create!(oid: oid, size: size, file: uploaded_file)
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-05-09 12:01:36 +05:30
def link_to_project!(object)
2016-09-13 17:45:13 +05:30
if object && !object.projects.exists?(storage_project.id)
2018-05-09 12:01:36 +05:30
object.lfs_objects_projects.create!(project: storage_project)
2016-09-13 17:45:13 +05:30
end
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
end