From e1b3f2c70df2ff1277ec5b4fc55e709c376b2b6c Mon Sep 17 00:00:00 2001 From: Wieland Lindenthal Date: Fri, 10 May 2019 14:36:59 +0200 Subject: [PATCH] Add an API endpoint 'bcf_xml' accepting POST of a BCF-XML file --- modules/bcf/lib/api/v3/bcf_xml/bcf_xml_api.rb | 76 +++++++++++++++++++ modules/bcf/lib/open_project/bcf/engine.rb | 9 +++ 2 files changed, 85 insertions(+) create mode 100644 modules/bcf/lib/api/v3/bcf_xml/bcf_xml_api.rb diff --git a/modules/bcf/lib/api/v3/bcf_xml/bcf_xml_api.rb b/modules/bcf/lib/api/v3/bcf_xml/bcf_xml_api.rb new file mode 100644 index 0000000000..b0c0097390 --- /dev/null +++ b/modules/bcf/lib/api/v3/bcf_xml/bcf_xml_api.rb @@ -0,0 +1,76 @@ +#-- copyright +# OpenProject Documents Plugin +# +# Former OpenProject Core functionality extracted into a plugin. +# +# Copyright (C) 2009-2014 the OpenProject Foundation (OPF) +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See doc/COPYRIGHT.rdoc for more details. +#++ + +module API + module V3 + module BcfXml + class BcfXmlAPI < ::API::OpenProjectAPI + resources :projects do + route_param :id do + namespace 'bcf_xml' do + helpers do + # Global helper to set allowed content_types + # This may be overriden when multipart is allowed (file uploads) + def allowed_content_types + if post_request? + %w(application/octet-stream) + else + super + end + end + + def post_request? + request.env['REQUEST_METHOD'] == 'POST' + end + end + + post do + project = Project.find(params[:id]) + + authorize(:manage_bcf, context: project) do + raise API::Errors::NotFound.new + end + + begin + file = request.body + importer = ::OpenProject::Bcf::BcfXml::Importer.new project, current_user: current_user + importer.import! file + rescue StandardError => e + raise API::Errors::InternalError.new e.message + end + end + end + end + end + end + end + end +end diff --git a/modules/bcf/lib/open_project/bcf/engine.rb b/modules/bcf/lib/open_project/bcf/engine.rb index e0c1521881..ec668a1826 100644 --- a/modules/bcf/lib/open_project/bcf/engine.rb +++ b/modules/bcf/lib/open_project/bcf/engine.rb @@ -61,6 +61,15 @@ module OpenProject::Bcf prepend Patches::Api::V3::ExportFormats end + add_api_path :bcf_xml do |project_id| + "#{project(project_id)}/bcf_xml" + end + + add_api_endpoint 'API::V3::Projects::ProjectsAPI' do + content_type :binary, 'application/octet-stream' + mount ::API::V3::BcfXml::BcfXmlAPI + end + initializer 'bcf.register_hooks' do # don't use require_dependency to not reload hooks in development mode require 'open_project/xls_export/hooks/work_package_hook.rb'