2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-11 18:12:34 -05:00
|
|
|
module Lfs
|
2018-03-14 19:45:57 -04:00
|
|
|
# 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.
|
2018-01-11 18:12:34 -05:00
|
|
|
#
|
2019-06-19 13:51:57 -04:00
|
|
|
# The `repository` passed to the initializer can be a Repository or
|
|
|
|
# a DesignManagement::Repository (an EE-specific class that inherits
|
|
|
|
# from Repository).
|
|
|
|
#
|
|
|
|
# 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-03-14 19:45:57 -04:00
|
|
|
# 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-06-06 02:51:50 -04:00
|
|
|
# transformer = Lfs::FileTransformer.new(project, repository, @branch_name)
|
2018-03-14 19:45:57 -04:00
|
|
|
# content_or_lfs_pointer = transformer.new_file(file_path, content).content
|
|
|
|
# create_transformed_commit(content_or_lfs_pointer)
|
2018-01-11 18:12:34 -05:00
|
|
|
#
|
2018-03-14 20:04:43 -04:00
|
|
|
class FileTransformer
|
2019-06-06 02:51:50 -04:00
|
|
|
attr_reader :project, :repository, :repository_type, :branch_name
|
2018-01-11 18:12:34 -05:00
|
|
|
|
2019-06-06 02:51:50 -04:00
|
|
|
def initialize(project, repository, branch_name)
|
2018-01-11 18:12:34 -05:00
|
|
|
@project = project
|
2019-06-06 02:51:50 -04:00
|
|
|
@repository = repository
|
|
|
|
@repository_type = repository.repo_type.name
|
2018-01-11 18:12:34 -05:00
|
|
|
@branch_name = branch_name
|
|
|
|
end
|
|
|
|
|
2018-03-12 20:55:49 -04:00
|
|
|
def new_file(file_path, file_content, encoding: nil)
|
2018-01-11 18:12:34 -05:00
|
|
|
if project.lfs_enabled? && lfs_file?(file_path)
|
2019-05-06 08:51:49 -04:00
|
|
|
file_content = parse_file_content(file_content, encoding: encoding)
|
2018-01-11 18:12:34 -05:00
|
|
|
lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content)
|
|
|
|
lfs_object = create_lfs_object!(lfs_pointer_file, file_content)
|
|
|
|
|
2018-03-14 19:45:57 -04:00
|
|
|
link_lfs_object!(lfs_object)
|
2018-01-11 18:12:34 -05:00
|
|
|
|
2018-03-12 20:55:49 -04:00
|
|
|
Result.new(content: lfs_pointer_file.pointer, encoding: 'text')
|
2018-01-11 18:12:34 -05:00
|
|
|
else
|
2018-03-12 20:55:49 -04:00
|
|
|
Result.new(content: file_content, encoding: encoding)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Result
|
|
|
|
attr_reader :content, :encoding
|
|
|
|
|
|
|
|
def initialize(content:, encoding:)
|
|
|
|
@content = content
|
|
|
|
@encoding = encoding
|
2018-01-11 18:12:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def lfs_file?(file_path)
|
2018-03-11 15:17:39 -04:00
|
|
|
cached_attributes.attributes(file_path)['filter'] == 'lfs'
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_attributes
|
2019-06-19 13:51:57 -04:00
|
|
|
@cached_attributes ||= repository.attributes_at(branch_name)
|
2018-01-11 18:12:34 -05:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-11 18:12:34 -05:00
|
|
|
def create_lfs_object!(lfs_pointer_file, file_content)
|
|
|
|
LfsObject.find_or_create_by(oid: lfs_pointer_file.sha256, size: lfs_pointer_file.size) do |lfs_object|
|
|
|
|
lfs_object.file = CarrierWaveStringFile.new(file_content)
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-01-11 18:12:34 -05:00
|
|
|
|
|
|
|
def link_lfs_object!(lfs_object)
|
2019-06-06 02:51:50 -04:00
|
|
|
LfsObjectsProject.safe_find_or_create_by!(
|
|
|
|
project: project,
|
|
|
|
lfs_object: lfs_object,
|
|
|
|
repository_type: repository_type
|
|
|
|
)
|
2018-01-11 18:12:34 -05:00
|
|
|
end
|
2019-05-06 08:51:49 -04:00
|
|
|
|
|
|
|
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-01-11 18:12:34 -05:00
|
|
|
end
|
|
|
|
end
|