Commit 4c7b120a authored by James Lopez's avatar James Lopez

add upload manager and spec

parent 0ed8f349
module Gitlab
module ImportExport
class UploadsManager
include Gitlab::ImportExport::CommandLineUtil
def initialize(project:, shared:, relative_export_path: 'uploads', from: nil)
@project = project
@shared = shared
@relative_export_path = relative_export_path
@from = from || default_uploads_path
end
def copy
copy_files(@from, uploads_export_path) if File.directory?(@from)
copy_from_object_storage
end
private
def copy_from_object_storage
return unless Gitlab::ImportExport.object_storage?
return if uploads.empty?
mkdir_p(uploads_export_path)
uploads.each do |upload_model|
next unless upload_model.file
next if upload_model.upload.local? # Already copied
download_and_copy(upload_model)
end
end
def default_uploads_path
FileUploader.absolute_base_dir(@project)
end
def uploads_export_path
@uploads_export_path ||= File.join(@shared.export_path, @relative_export_path)
end
def uploads
@uploads ||= begin
if @relative_export_path == 'avatar'
[@project.avatar].compact
else
(@project.uploads - [@project.avatar&.upload]).map(&:build_uploader)
end
end
end
def download_and_copy(upload)
upload_path = File.join(uploads_export_path, upload.filename)
File.open(upload_path, 'w') do |file|
IO.copy_stream(URI.parse(upload.file.url).open, file)
end
end
end
end
end
require 'spec_helper'
describe Gitlab::ImportExport::UploadsManager do
let(:shared) { project.import_export_shared }
let(:export_path) { "#{Dir.tmpdir}/project_tree_saver_spec" }
let(:project) { create(:project) }
subject(:manager) { described_class.new(project: project, shared: shared) }
before do
allow_any_instance_of(Gitlab::ImportExport).to receive(:storage_path).and_return(export_path)
FileUtils.mkdir_p(shared.export_path)
end
after do
FileUtils.rm_rf(shared.export_path)
end
describe '#copy' do
context 'when the project has uploads locally stored' do
let(:upload) { create(:upload) }
before do
project.uploads << upload
end
it 'does not cause errors' do
manager.copy
expect(shared.errors).to be_empty
end
it 'copies the file in the correct location when there is an upload' do
manager.copy
expect(File).to exist("#{shared.export_path}/uploads/#{File.basename(upload.path)}")
end
end
end
end
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