Commit c75187df authored by Travis Miller's avatar Travis Miller

Add project export API implementation

parent a4308c53
...@@ -1560,7 +1560,7 @@ class Project < ActiveRecord::Base ...@@ -1560,7 +1560,7 @@ class Project < ActiveRecord::Base
def export_path def export_path
return nil unless namespace.present? || hashed_storage?(:repository) return nil unless namespace.present? || hashed_storage?(:repository)
File.join(Gitlab::ImportExport.storage_path, disk_path) Gitlab::ImportExport::Shared.new(self).archive_path
end end
def export_project_path def export_project_path
...@@ -1578,8 +1578,7 @@ class Project < ActiveRecord::Base ...@@ -1578,8 +1578,7 @@ class Project < ActiveRecord::Base
end end
def export_in_progress? def export_in_progress?
shared = Gitlab::ImportExport::Shared.new(relative_path: File.join(disk_path, 'work')) Gitlab::ImportExport::Shared.new(self).active_export_count > 0
File.directory?(shared.export_path)
end end
def remove_exports def remove_exports
......
...@@ -2,7 +2,7 @@ module Projects ...@@ -2,7 +2,7 @@ module Projects
module ImportExport module ImportExport
class ExportService < BaseService class ExportService < BaseService
def execute(_options = {}) def execute(_options = {})
@shared = Gitlab::ImportExport::Shared.new(relative_path: File.join(project.disk_path, 'work')) @shared = Gitlab::ImportExport::Shared.new(project)
save_all save_all
end end
......
...@@ -138,6 +138,7 @@ module API ...@@ -138,6 +138,7 @@ module API
mount ::API::PagesDomains mount ::API::PagesDomains
mount ::API::Pipelines mount ::API::Pipelines
mount ::API::PipelineSchedules mount ::API::PipelineSchedules
mount ::API::ProjectExport
mount ::API::ProjectImport mount ::API::ProjectImport
mount ::API::ProjectHooks mount ::API::ProjectHooks
mount ::API::Projects mount ::API::Projects
......
module API
class ProjectExport < Grape::API
before do
not_found! unless Gitlab::CurrentSettings.current_application_settings.project_export_enabled?
authorize_admin_project
end
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: { id: %r{[^/]+} } do
desc 'Get export status' do
success Entities::ProjectExportStatus
end
get ':id/export' do
present user_project, with: Entities::ProjectExportStatus
end
desc 'Download export'
get ':id/export/download' do
path = user_project.export_project_path
render_api_error!('404 Not found or has expired', 404) unless path
present_file!(path, File.basename(path), 'application/gzip')
end
desc 'Start export'
post ':id/export' do
user_project.add_export_job(current_user: current_user)
accepted!
end
end
end
end
...@@ -9,7 +9,7 @@ module Gitlab ...@@ -9,7 +9,7 @@ module Gitlab
@archive_file = project.import_source @archive_file = project.import_source
@current_user = project.creator @current_user = project.creator
@project = project @project = project
@shared = Gitlab::ImportExport::Shared.new(relative_path: path_with_namespace) @shared = Gitlab::ImportExport::Shared.new(project)
end end
def execute def execute
......
module Gitlab module Gitlab
module ImportExport module ImportExport
class Shared class Shared
attr_reader :errors, :opts attr_reader :errors, :project
def initialize(opts) def initialize(project)
@opts = opts @project = project
@errors = [] @errors = []
end end
def active_export_count
Dir[File.join(archive_path, '*')].count { |name| File.directory?(name) }
end
def export_path def export_path
@export_path ||= Gitlab::ImportExport.export_path(relative_path: relative_path) @export_path ||= Gitlab::ImportExport.export_path(relative_path: relative_path)
end end
...@@ -31,11 +35,15 @@ module Gitlab ...@@ -31,11 +35,15 @@ module Gitlab
private private
def relative_path def relative_path
File.join(opts[:relative_path], SecureRandom.hex) File.join(relative_archive_path, SecureRandom.hex)
end end
def relative_archive_path def relative_archive_path
File.join(opts[:relative_path], '..') if @project.is_a?(Project)
@project.disk_path
else
@project[:relative_path]
end
end end
def error_out(message, caller) def error_out(message, caller)
......
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