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
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
......
......@@ -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
......
......@@ -16,8 +16,7 @@ describe Files::MultiService do
Gitlab::Git::Commit.last_for_path(project.repository, branch_name, original_file_path).sha
end
let(:actions) do
[
let(:default_action) do
{
action: action,
file_path: new_file_path,
......@@ -25,9 +24,10 @@ describe Files::MultiService do
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
......
......@@ -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)
......
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