diff options
author | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-03-13 00:55:49 +0000 |
---|---|---|
committer | James Edwards-Jones <jedwardsjones@gitlab.com> | 2018-03-15 21:49:01 +0000 |
commit | 1f5e809c047d9561fe03d04c51b1dc4fffd95e04 (patch) | |
tree | d8bd865e43e592e846adcc1a3de02e36c4fd982a | |
parent | ca66a04ffec2e311e72b5bdd2c68d3286ef6631c (diff) | |
download | gitlab-ce-1f5e809c047d9561fe03d04c51b1dc4fffd95e04.tar.gz |
Use correct encoding with Lfs::FileTransfromer
-rw-r--r-- | app/services/files/multi_service.rb | 5 | ||||
-rw-r--r-- | app/services/lfs/file_transformer.rb | 16 | ||||
-rw-r--r-- | spec/services/files/multi_service_spec.rb | 40 | ||||
-rw-r--r-- | spec/services/lfs/file_transformer_spec.rb | 26 |
4 files changed, 68 insertions, 19 deletions
diff --git a/app/services/files/multi_service.rb b/app/services/files/multi_service.rb index be9c7c526a6..13a1dee4173 100644 --- a/app/services/files/multi_service.rb +++ b/app/services/files/multi_service.rb @@ -15,8 +15,9 @@ module Files 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 + result = transformer.new_file(action[:file_path], action[:content], encoding: action[:encoding]) + action[:content] = result.content + action[:encoding] = result.encoding end action diff --git a/app/services/lfs/file_transformer.rb b/app/services/lfs/file_transformer.rb index bdb2f1bea42..69281ee3137 100644 --- a/app/services/lfs/file_transformer.rb +++ b/app/services/lfs/file_transformer.rb @@ -20,16 +20,26 @@ module Lfs @branch_name = branch_name end - def new_file(file_path, file_content) + def new_file(file_path, file_content, encoding: nil) if project.lfs_enabled? && lfs_file?(file_path) + file_content = Base64.decode64(file_content) if encoding == 'base64' lfs_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content) lfs_object = create_lfs_object!(lfs_pointer_file, file_content) link_lfs_object!(lfs_object) - lfs_pointer_file.pointer + Result.new(content: lfs_pointer_file.pointer, encoding: 'text') else - file_content + 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 diff --git a/spec/services/files/multi_service_spec.rb b/spec/services/files/multi_service_spec.rb index 7b47df32b43..0dc92762d64 100644 --- a/spec/services/files/multi_service_spec.rb +++ b/spec/services/files/multi_service_spec.rb @@ -16,18 +16,18 @@ describe Files::MultiService do Gitlab::Git::Commit.last_for_path(project.repository, branch_name, original_file_path).sha end - let(:actions) do - [ - { - action: action, - file_path: new_file_path, - previous_path: original_file_path, - content: file_content, - last_commit_id: original_commit_id - } - ] + let(:default_action) do + { + action: action, + file_path: new_file_path, + previous_path: original_file_path, + content: file_content, + last_commit_id: original_commit_id + } end + let(:actions) { [default_action] } + let(:commit_params) do { commit_message: "Update File", @@ -135,6 +135,26 @@ describe Files::MultiService do expect(LfsObject.last.file.read).to eq file_content end + context 'with base64 encoded content' do + let(:raw_file_content) { 'Raw content' } + let(:file_content) { Base64.encode64(raw_file_content) } + let(:actions) { [default_action.merge(encoding: 'base64')] } + + it 'creates an LFS pointer' do + subject.execute + + blob = repository.blob_at('lfs', new_file_path) + + expect(blob.data).to start_with('version https://git-lfs.github.com/spec/v1') + end + + it "creates an LfsObject with the file's content" do + subject.execute + + expect(LfsObject.last.file.read).to eq raw_file_content + end + end + it 'links the LfsObject to the project' do expect do subject.execute diff --git a/spec/services/lfs/file_transformer_spec.rb b/spec/services/lfs/file_transformer_spec.rb index 6d17ab03ac3..00c5328a3a0 100644 --- a/spec/services/lfs/file_transformer_spec.rb +++ b/spec/services/lfs/file_transformer_spec.rb @@ -16,6 +16,18 @@ describe Lfs::FileTransformer do subject.new_file(file_path, file_content) end + + it 'returns untransformed content' do + result = subject.new_file(file_path, file_content) + + expect(result.content).to eq(file_content) + end + + it 'returns untransformed encoding' do + result = subject.new_file(file_path, file_content, encoding: 'base64') + + expect(result.encoding).to eq('base64') + end end context 'with lfs enabled' do @@ -38,17 +50,23 @@ describe Lfs::FileTransformer do expect(LfsObject.last.file.read).to eq file_content end - it 'creates an LFS pointer' do - new_content = subject.new_file(file_path, file_content) + it 'returns an LFS pointer' do + result = subject.new_file(file_path, file_content) + + expect(result.content).to start_with('version https://git-lfs.github.com/spec/v1') + end + + it 'returns LFS pointer encoding as text' do + result = subject.new_file(file_path, file_content, encoding: 'base64') - expect(new_content).to start_with('version https://git-lfs.github.com/spec/v1') + expect(result.encoding).to eq('text') end context "when doesn't use LFS" do let(:file_path) { 'other.filetype' } it "doesn't create LFS pointers" do - new_content = subject.new_file(file_path, file_content) + new_content = subject.new_file(file_path, file_content).content expect(new_content).not_to start_with('version https://git-lfs.github.com/spec/v1') expect(new_content).to eq(file_content) |