2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
module Lfs
|
|
|
|
# Usage: Calling `new_file` check to see if a file should be in LFS and
|
|
|
|
# return a transformed result with `content` and `encoding` to commit.
|
|
|
|
#
|
2019-09-30 21:07:59 +05:30
|
|
|
# The `repository` passed to the initializer can be a Repository or
|
2020-05-24 23:13:21 +05:30
|
|
|
# class that inherits from Repository.
|
2019-09-30 21:07:59 +05:30
|
|
|
#
|
|
|
|
# The `repository_type` property will be one of the types named in
|
|
|
|
# `Gitlab::GlRepository.types`, and is recorded on the `LfsObjectsProject`
|
|
|
|
# in order to identify the repository location of the blob.
|
|
|
|
#
|
2018-05-09 12:01:36 +05:30
|
|
|
# For LFS an LfsObject linked to the project is stored and an LFS
|
|
|
|
# pointer returned. If the file isn't in LFS the untransformed content
|
|
|
|
# is returned to save in the commit.
|
|
|
|
#
|
2019-09-04 21:01:54 +05:30
|
|
|
# transformer = Lfs::FileTransformer.new(project, repository, @branch_name)
|
2018-05-09 12:01:36 +05:30
|
|
|
# content_or_lfs_pointer = transformer.new_file(file_path, content).content
|
|
|
|
# create_transformed_commit(content_or_lfs_pointer)
|
|
|
|
#
|
|
|
|
class FileTransformer
|
2019-09-04 21:01:54 +05:30
|
|
|
attr_reader :project, :repository, :repository_type, :branch_name
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
def initialize(project, repository, branch_name)
|
2018-05-09 12:01:36 +05:30
|
|
|
@project = project
|
2019-09-04 21:01:54 +05:30
|
|
|
@repository = repository
|
|
|
|
@repository_type = repository.repo_type.name
|
2018-05-09 12:01:36 +05:30
|
|
|
@branch_name = branch_name
|
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def new_file(file_path, file_content, encoding: nil, detect_content_type: false)
|
2018-05-09 12:01:36 +05:30
|
|
|
if project.lfs_enabled? && lfs_file?(file_path)
|
2019-07-07 11:18:12 +05:30
|
|
|
file_content = parse_file_content(file_content, encoding: encoding)
|
2018-05-09 12:01:36 +05:30
|
|
|
lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content)
|
2023-03-17 16:20:25 +05:30
|
|
|
lfs_object = create_lfs_object!(lfs_pointer_file, file_content, detect_content_type)
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
link_lfs_object!(lfs_object)
|
|
|
|
|
|
|
|
Result.new(content: lfs_pointer_file.pointer, encoding: 'text')
|
|
|
|
else
|
|
|
|
Result.new(content: file_content, encoding: encoding)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Result
|
|
|
|
attr_reader :content, :encoding
|
|
|
|
|
|
|
|
def initialize(content:, encoding:)
|
|
|
|
@content = content
|
|
|
|
@encoding = encoding
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def lfs_file?(file_path)
|
|
|
|
cached_attributes.attributes(file_path)['filter'] == 'lfs'
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_attributes
|
2019-09-30 21:07:59 +05:30
|
|
|
@cached_attributes ||= repository.attributes_at(branch_name)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2023-03-17 16:20:25 +05:30
|
|
|
def create_lfs_object!(lfs_pointer_file, file_content, detect_content_type)
|
2018-05-09 12:01:36 +05:30
|
|
|
LfsObject.find_or_create_by(oid: lfs_pointer_file.sha256, size: lfs_pointer_file.size) do |lfs_object|
|
2023-03-17 16:20:25 +05:30
|
|
|
lfs_object.file = if detect_content_type && (content_type = Gitlab::Utils::MimeType.from_string(file_content))
|
|
|
|
CarrierWaveStringFile.new_file(
|
|
|
|
file_content: file_content,
|
|
|
|
filename: '',
|
|
|
|
content_type: content_type
|
|
|
|
)
|
|
|
|
else
|
|
|
|
CarrierWaveStringFile.new(file_content)
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
def link_lfs_object!(lfs_object)
|
2019-09-04 21:01:54 +05:30
|
|
|
LfsObjectsProject.safe_find_or_create_by!(
|
|
|
|
project: project,
|
|
|
|
lfs_object: lfs_object,
|
|
|
|
repository_type: repository_type
|
|
|
|
)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
def parse_file_content(file_content, encoding: nil)
|
|
|
|
return file_content.read if file_content.respond_to?(:read)
|
|
|
|
return Base64.decode64(file_content) if encoding == 'base64'
|
|
|
|
|
|
|
|
file_content
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|