2020-01-24 13:09:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Repositories
|
|
|
|
class LfsStorageController < Repositories::GitHttpClientController
|
|
|
|
include LfsRequest
|
|
|
|
include WorkhorseRequest
|
|
|
|
include SendFileUpload
|
|
|
|
|
2022-05-04 05:09:02 -04:00
|
|
|
InvalidUploadedFile = Class.new(StandardError)
|
|
|
|
|
2020-01-24 13:09:00 -05:00
|
|
|
skip_before_action :verify_workhorse_api!, only: :download
|
|
|
|
|
2021-08-18 08:11:19 -04:00
|
|
|
# added here as a part of the refactor, will be removed
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/328692
|
|
|
|
delegate :deploy_token, :user, to: :authentication_result, allow_nil: true
|
2021-11-12 13:12:20 -05:00
|
|
|
urgency :medium, [:download, :upload_authorize]
|
|
|
|
urgency :low, [:upload_finalize]
|
2021-08-18 08:11:19 -04:00
|
|
|
|
2020-01-24 13:09:00 -05: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" })
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_authorize
|
|
|
|
set_workhorse_internal_api_content_type
|
|
|
|
|
2021-01-22 16:09:10 -05:00
|
|
|
# We don't actually know whether Workhorse received an LFS upload
|
|
|
|
# request with a Content-Length header or `Transfer-Encoding:
|
|
|
|
# chunked`. Since we don't know, we need to be pessimistic and
|
|
|
|
# set `has_length` to `false` so that multipart uploads will be
|
|
|
|
# used for AWS. Otherwise, AWS will respond with `501 NOT IMPLEMENTED`
|
|
|
|
# error because a PutObject request with `Transfer-Encoding: chunked`
|
|
|
|
# is not supported.
|
|
|
|
#
|
|
|
|
# This is only an issue with object storage-specific settings, not
|
|
|
|
# with consolidated object storage settings.
|
|
|
|
authorized = LfsObjectUploader.workhorse_authorize(has_length: false, maximum_size: size)
|
2020-01-24 13:09:00 -05:00
|
|
|
authorized.merge!(LfsOid: oid, LfsSize: size)
|
|
|
|
|
|
|
|
render json: authorized
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_finalize
|
2022-05-04 05:09:02 -04:00
|
|
|
validate_uploaded_file!
|
|
|
|
|
2020-01-24 13:09:00 -05:00
|
|
|
if store_file!(oid, size)
|
2020-11-10 19:08:58 -05:00
|
|
|
head 200, content_type: LfsRequest::CONTENT_TYPE
|
2020-01-24 13:09:00 -05:00
|
|
|
else
|
|
|
|
render plain: 'Unprocessable entity', status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render_lfs_forbidden
|
|
|
|
rescue UploadedFile::InvalidPathError
|
|
|
|
render_lfs_forbidden
|
|
|
|
rescue ObjectStorage::RemoteStoreError
|
|
|
|
render_lfs_forbidden
|
2022-05-04 05:09:02 -04:00
|
|
|
rescue InvalidUploadedFile
|
|
|
|
render plain: 'SHA256 or size mismatch', status: :bad_request
|
2020-01-24 13:09:00 -05: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
|
|
|
|
|
2020-10-26 08:08:44 -04:00
|
|
|
def uploaded_file
|
|
|
|
params[:file]
|
|
|
|
end
|
|
|
|
|
2020-01-24 13:09:00 -05:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def store_file!(oid, size)
|
|
|
|
object = LfsObject.find_by(oid: oid, size: size)
|
2020-10-26 08:08:44 -04:00
|
|
|
|
|
|
|
if object
|
|
|
|
replace_file!(object) unless object.file&.exists?
|
|
|
|
else
|
2020-01-24 13:09:00 -05:00
|
|
|
object = create_file!(oid, size)
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless object
|
|
|
|
|
|
|
|
link_to_project!(object)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
def create_file!(oid, size)
|
2020-08-05 17:09:40 -04:00
|
|
|
return unless uploaded_file.is_a?(UploadedFile)
|
2020-01-24 13:09:00 -05:00
|
|
|
|
|
|
|
LfsObject.create!(oid: oid, size: size, file: uploaded_file)
|
|
|
|
end
|
|
|
|
|
2020-10-26 08:08:44 -04:00
|
|
|
def replace_file!(lfs_object)
|
|
|
|
raise UploadedFile::InvalidPathError unless uploaded_file.is_a?(UploadedFile)
|
|
|
|
|
|
|
|
Gitlab::AppJsonLogger.info(message: "LFS file replaced because it did not exist", oid: oid, size: size)
|
|
|
|
lfs_object.file = uploaded_file
|
|
|
|
lfs_object.save!
|
|
|
|
end
|
|
|
|
|
2020-01-24 13:09:00 -05:00
|
|
|
def link_to_project!(object)
|
2020-02-07 07:09:13 -05:00
|
|
|
return unless object
|
|
|
|
|
|
|
|
LfsObjectsProject.safe_find_or_create_by!(
|
|
|
|
project: project,
|
|
|
|
lfs_object: object
|
|
|
|
)
|
2020-01-24 13:09:00 -05:00
|
|
|
end
|
2022-05-04 05:09:02 -04:00
|
|
|
|
|
|
|
def validate_uploaded_file!
|
|
|
|
return unless uploaded_file
|
|
|
|
|
|
|
|
if size != uploaded_file.size || oid != uploaded_file.sha256
|
|
|
|
raise InvalidUploadedFile
|
|
|
|
end
|
|
|
|
end
|
2020-01-24 13:09:00 -05:00
|
|
|
end
|
|
|
|
end
|