Class: Lfs::FileTransformer

Inherits:
Object
  • Object
show all
Defined in:
app/services/lfs/file_transformer.rb

Overview

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.

The `repository` passed to the initializer can be a Repository or
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.

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.

transformer = Lfs::FileTransformer.new(project, repository, @branch_name) content_or_lfs_pointer = transformer.new_file(file_path, content).content create_transformed_commit(content_or_lfs_pointer)

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, repository, branch_name) ⇒ FileTransformer

Returns a new instance of FileTransformer.


25
26
27
28
29
30
# File 'app/services/lfs/file_transformer.rb', line 25

def initialize(project, repository, branch_name)
  @project = project
  @repository = repository
  @repository_type = repository.repo_type.name
  @branch_name = branch_name
end

Instance Attribute Details

#branch_nameObject (readonly)

Returns the value of attribute branch_name.


23
24
25
# File 'app/services/lfs/file_transformer.rb', line 23

def branch_name
  @branch_name
end

#projectObject (readonly)

Returns the value of attribute project.


23
24
25
# File 'app/services/lfs/file_transformer.rb', line 23

def project
  @project
end

#repositoryObject (readonly)

Returns the value of attribute repository.


23
24
25
# File 'app/services/lfs/file_transformer.rb', line 23

def repository
  @repository
end

#repository_typeObject (readonly)

Returns the value of attribute repository_type.


23
24
25
# File 'app/services/lfs/file_transformer.rb', line 23

def repository_type
  @repository_type
end

Instance Method Details

#new_file(file_path, file_content, encoding: nil) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/lfs/file_transformer.rb', line 32

def new_file(file_path, file_content, encoding: nil)
  if project.lfs_enabled? && lfs_file?(file_path)
    file_content = parse_file_content(file_content, encoding: encoding)
    lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content)
    lfs_object = create_lfs_object!(lfs_pointer_file, file_content)

    link_lfs_object!(lfs_object)

    Result.new(content: lfs_pointer_file.pointer, encoding: 'text')
  else
    Result.new(content: file_content, encoding: encoding)
  end
end