diff options
author | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-01-11 23:12:34 +0000 |
---|---|---|
committer | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-03-15 02:39:08 +0000 |
commit | 1baac9211238f60d2d2a50cccd0bea6979bfa6ba (patch) | |
tree | 7f07f3d1eb65f31f01c52973d115d349a95e3de2 /app | |
parent | ffb1c65b0ba7fa8a4ea7e128cb47449f04837869 (diff) | |
download | gitlab-ce-1baac9211238f60d2d2a50cccd0bea6979bfa6ba.tar.gz |
Multi-file upload and Commit API obey LFS filters
Updates Files::MultiService for the commits API which is in turn used by the
multi-file upload web UI
Ensures that files which should be in LFS are transformed into LFS pointers
Uses Lfs::Transformer which then links LfsObjectProjects on success
Diffstat (limited to 'app')
-rw-r--r-- | app/services/files/create_service.rb | 5 | ||||
-rw-r--r-- | app/services/files/multi_service.rb | 25 | ||||
-rw-r--r-- | app/services/lfs/file_transformer.rb | 35 |
3 files changed, 55 insertions, 10 deletions
diff --git a/app/services/files/create_service.rb b/app/services/files/create_service.rb index 675b05e8fc4..b8ae03842a3 100644 --- a/app/services/files/create_service.rb +++ b/app/services/files/create_service.rb @@ -1,9 +1,8 @@ module Files class CreateService < Files::BaseService def create_commit! - handler = Lfs::FileTransformer.new(project, @branch_name) - - handler.new_file(@file_path, @file_content) do |content_or_lfs_pointer| + Lfs::FileTransformer.link_lfs_objects(project, @branch_name) do |transformer| + content_or_lfs_pointer = transformer.new_file(@file_path, @file_content) create_transformed_commit(content_or_lfs_pointer) end end diff --git a/app/services/files/multi_service.rb b/app/services/files/multi_service.rb index a03c59f569d..bd88c46e4af 100644 --- a/app/services/files/multi_service.rb +++ b/app/services/files/multi_service.rb @@ -3,11 +3,32 @@ module Files UPDATE_FILE_ACTIONS = %w(update move delete).freeze def create_commit! + Lfs::FileTransformer.link_lfs_objects(project, @branch_name) do |transformer| + actions = actions_after_lfs_transformation(transformer, params[:actions]) + + commit_actions!(actions) + end + end + + private + + def actions_after_lfs_transformation(transformer, actions) + actions.map do |action| + if action[:action] == 'create' + content = transformer.new_file(action[:file_path], action[:content]) + action[:content] = content + end + + action + end + end + + def commit_actions!(actions) repository.multi_action( current_user, message: @commit_message, branch_name: @branch_name, - actions: params[:actions], + actions: actions, author_email: @author_email, author_name: @author_name, start_project: @start_project, @@ -17,8 +38,6 @@ module Files raise_error(e) end - private - def validate! super diff --git a/app/services/lfs/file_transformer.rb b/app/services/lfs/file_transformer.rb index 030f2c6aeba..7c93659b60d 100644 --- a/app/services/lfs/file_transformer.rb +++ b/app/services/lfs/file_transformer.rb @@ -1,4 +1,16 @@ module Lfs + # Usage: Open a `.link_lfs_objects` block, call `new_file` on the yielded object + # as many times as needed. LfsObjectProject links will be saved if the + # return value of the block is truthy. + # + # Calling `new_file` will check the path to see if it should be in LFS, + # save and LFS pointer of needed and return its content to be saved in + # a commit. If the file isn't LFS the untransformed content is returned. + # + # Lfs::FileTransformer.link_lfs_objects(project, @branch_name) do |transformer| + # content_or_lfs_pointer = transformer.new_file(file_path, file_content) + # create_transformed_commit(content_or_lfs_pointer) + # end class FileTransformer attr_reader :project, :branch_name @@ -9,26 +21,41 @@ module Lfs @branch_name = branch_name end + def self.link_lfs_objects(project, branch_name) + transformer = new(project, branch_name) + result = yield(transformer) + transformer.after_transform! if result + + result + 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) + on_success_actions << -> { link_lfs_object!(lfs_object) } - link_lfs_object!(lfs_object) if success + lfs_pointer_file.pointer else - yield(file_content) + file_content end end + def after_transform! + on_success_actions.map(&:call) + end + private def lfs_file?(file_path) repository.attributes_at(branch_name, file_path)['filter'] == 'lfs' end + def on_success_actions + @on_success_actions ||= [] + 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) |