diff options
author | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-01-11 23:12:34 +0000 |
---|---|---|
committer | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-02-06 15:26:25 +0000 |
commit | d2a77094ae4a44b63fbe22ca910e836cb336a729 (patch) | |
tree | 4c2a33b4ad440355202c8dc9d915957a04a78f44 /app/services/lfs | |
parent | 321ef1d45b7fad9a73451b97fc55476c77118477 (diff) | |
download | gitlab-ce-d2a77094ae4a44b63fbe22ca910e836cb336a729.tar.gz |
File upload UI obeys LFS filters
Uses Lfs::FileModificationHandler to coordinate LFS detection, creation of LfsObject, etc
Caveats:
1. This isn't used by the multi-file editor / Web IDE
2. This isn't used on rename. We'd need to be able to download LFS files
and add them to the commit if they no longer match so not as simple.
3. We only check the root .gitattributes file, so this should be improved
to correctly check for nested .gitattributes files in subfolders.
Diffstat (limited to 'app/services/lfs')
-rw-r--r-- | app/services/lfs/file_modification_handler.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/app/services/lfs/file_modification_handler.rb b/app/services/lfs/file_modification_handler.rb new file mode 100644 index 00000000000..fe9091a6e5d --- /dev/null +++ b/app/services/lfs/file_modification_handler.rb @@ -0,0 +1,42 @@ +module Lfs + class FileModificationHandler + attr_reader :project, :branch_name + + delegate :repository, to: :project + + def initialize(project, branch_name) + @project = project + @branch_name = branch_name + end + + def new_file(file_path, file_content) + if project.lfs_enabled? && lfs_file?(file_path) + lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content) + lfs_object = create_lfs_object!(lfs_pointer_file, file_content) + content = lfs_pointer_file.pointer + + success = yield(content) + + link_lfs_object!(lfs_object) if success + else + yield(file_content) + end + end + + private + + def lfs_file?(file_path) + repository.attributes_at(branch_name, file_path)['filter'] == 'lfs' + end + + 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 + + def link_lfs_object!(lfs_object) + project.lfs_objects << lfs_object + end + end +end |