Commit 1f5e809c authored by James Edwards-Jones's avatar James Edwards-Jones

Use correct encoding with Lfs::FileTransfromer

parent ca66a04f
...@@ -15,8 +15,9 @@ module Files ...@@ -15,8 +15,9 @@ module Files
def actions_after_lfs_transformation(transformer, actions) def actions_after_lfs_transformation(transformer, actions)
actions.map do |action| actions.map do |action|
if action[:action] == 'create' if action[:action] == 'create'
content = transformer.new_file(action[:file_path], action[:content]) result = transformer.new_file(action[:file_path], action[:content], encoding: action[:encoding])
action[:content] = content action[:content] = result.content
action[:encoding] = result.encoding
end end
action action
......
...@@ -20,16 +20,26 @@ module Lfs ...@@ -20,16 +20,26 @@ module Lfs
@branch_name = branch_name @branch_name = branch_name
end 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) 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_pointer_file = Gitlab::Git::LfsPointerFile.new(file_content)
lfs_object = create_lfs_object!(lfs_pointer_file, file_content) lfs_object = create_lfs_object!(lfs_pointer_file, file_content)
link_lfs_object!(lfs_object) link_lfs_object!(lfs_object)
lfs_pointer_file.pointer Result.new(content: lfs_pointer_file.pointer, encoding: 'text')
else 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
end end
......
...@@ -16,8 +16,7 @@ describe Files::MultiService do ...@@ -16,8 +16,7 @@ describe Files::MultiService do
Gitlab::Git::Commit.last_for_path(project.repository, branch_name, original_file_path).sha Gitlab::Git::Commit.last_for_path(project.repository, branch_name, original_file_path).sha
end end
let(:actions) do let(:default_action) do
[
{ {
action: action, action: action,
file_path: new_file_path, file_path: new_file_path,
...@@ -25,9 +24,10 @@ describe Files::MultiService do ...@@ -25,9 +24,10 @@ describe Files::MultiService do
content: file_content, content: file_content,
last_commit_id: original_commit_id last_commit_id: original_commit_id
} }
]
end end
let(:actions) { [default_action] }
let(:commit_params) do let(:commit_params) do
{ {
commit_message: "Update File", commit_message: "Update File",
...@@ -135,6 +135,26 @@ describe Files::MultiService do ...@@ -135,6 +135,26 @@ describe Files::MultiService do
expect(LfsObject.last.file.read).to eq file_content expect(LfsObject.last.file.read).to eq file_content
end 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 it 'links the LfsObject to the project' do
expect do expect do
subject.execute subject.execute
......
...@@ -16,6 +16,18 @@ describe Lfs::FileTransformer do ...@@ -16,6 +16,18 @@ describe Lfs::FileTransformer do
subject.new_file(file_path, file_content) subject.new_file(file_path, file_content)
end 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 end
context 'with lfs enabled' do context 'with lfs enabled' do
...@@ -38,17 +50,23 @@ describe Lfs::FileTransformer do ...@@ -38,17 +50,23 @@ describe Lfs::FileTransformer do
expect(LfsObject.last.file.read).to eq file_content expect(LfsObject.last.file.read).to eq file_content
end end
it 'creates an LFS pointer' do it 'returns an LFS pointer' do
new_content = subject.new_file(file_path, file_content) 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 end
context "when doesn't use LFS" do context "when doesn't use LFS" do
let(:file_path) { 'other.filetype' } let(:file_path) { 'other.filetype' }
it "doesn't create LFS pointers" do 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).not_to start_with('version https://git-lfs.github.com/spec/v1')
expect(new_content).to eq(file_content) expect(new_content).to eq(file_content)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment