2016-07-20 12:41:26 -04:00
|
|
|
class Projects::LfsStorageController < Projects::GitHttpClientController
|
2016-11-21 10:31:51 -05:00
|
|
|
include LfsRequest
|
|
|
|
include WorkhorseRequest
|
2017-09-07 17:27:04 -04:00
|
|
|
include SendFileUpload
|
2016-07-20 12:41:26 -04:00
|
|
|
|
2016-11-21 10:31:51 -05: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
|
|
|
|
|
2017-09-07 17:27:04 -04:00
|
|
|
send_upload(lfs_object.file, send_params: { content_type: "application/octet-stream" })
|
2016-07-20 12:41:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def upload_authorize
|
2016-08-19 13:10:41 -04:00
|
|
|
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
|
|
|
|
render plain: 'Unprocessable entity', status: 422
|
|
|
|
end
|
2018-03-23 06:07:22 -04:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
2018-04-03 12:47:33 -04:00
|
|
|
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
|
|
|
|
|
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)
|
2018-04-03 12:47:33 -04:00
|
|
|
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
|
|
|
|
|
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)
|
2018-05-01 16:27:54 -04:00
|
|
|
object.lfs_objects_projects.create!(project: storage_project)
|
2016-07-20 12:41:26 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|