Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#44856] Validate request body for prepare upload endpoint

Andreas Pfohl354041100640
diff --git a/modules/storages/app/common/storages/peripherals/storage_error_helper.rb b/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
index ea122e492a1..34a0c84a17c 100644
--- a/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_error_helper.rb
@@ -34,6 +34,8 @@ module Storages::Peripherals
case error.code
when :not_found
raise API::Errors::NotFound.new
+ when :bad_request
+ raise API::Errors::BadRequest.new("Malformed body.")
else
raise API::Errors::InternalError.new
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/files_query_helpers.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/files_query_helpers.rb
new file mode 100644
index 00000000000..d8607cc36d7
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/files_query_helpers.rb
@@ -0,0 +1,41 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages::Peripherals::StorageInteraction
+ module FilesQueryHelpers
+ def files_query(storage, user)
+ Storages::Peripherals::StorageRequests
+ .new(storage:)
+ .files_query(user:)
+ end
+
+ def execute_files_query(parent)
+ ->(query) { query.call(parent) }
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/upload_link_query_helpers.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/upload_link_query_helpers.rb
new file mode 100644
index 00000000000..02221651132
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/upload_link_query_helpers.rb
@@ -0,0 +1,56 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module Storages::Peripherals::StorageInteraction
+ module UploadLinkQueryHelpers
+ using Storages::Peripherals::ServiceResultRefinements
+
+ def validate_request_body(body)
+ case body.transform_keys(&:to_sym)
+ in { projectId: project_id, fileName: file_name, parent: parent }
+ authorize(:manage_file_links, context: Project.find(project_id))
+ ServiceResult.success(result: { fileName: file_name, parent: }.transform_keys(&:to_s))
+ else
+ ServiceResult.failure(errors: :bad_request)
+ end
+ end
+
+ def upload_link_query(storage, user)
+ Storages::Peripherals::StorageRequests
+ .new(storage:)
+ .upload_link_query(
+ user:,
+ finalize_url: nil # ToDo: api_v3_paths.finalize_upload(@storage.id)
+ )
+ end
+
+ def execute_upload_link_query(request_body)
+ ->(query) { validate_request_body(request_body) >> query }
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
index 467b81322ba..a33127a3dec 100644
--- a/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
+++ b/modules/storages/lib/api/v3/storage_files/storage_files_api.rb
@@ -30,55 +30,36 @@ module API::V3::StorageFiles
class StorageFilesAPI < ::API::OpenProjectAPI
using Storages::Peripherals::ServiceResultRefinements
helpers Storages::Peripherals::StorageErrorHelper
+ helpers Storages::Peripherals::StorageInteraction::UploadLinkQueryHelpers
+ helpers Storages::Peripherals::StorageInteraction::FilesQueryHelpers
resources :files do
get do
- Storages::Peripherals::StorageRequests
- .new(storage: @storage)
- .files_query(user: current_user)
+ (files_query(@storage, current_user) >> execute_files_query(params[:parent]))
.match(
- on_success: ->(files_query) {
- files_query
- .call(params[:parent])
- .map do |files|
- API::V3::StorageFiles::StorageFileCollectionRepresenter.new(
- files,
- self_link: api_v3_paths.storage_files(@storage.id),
- current_user:
- )
- end.match(
- on_success: ->(representer) { representer },
- on_failure: ->(error) { raise_error(error) }
+ on_success: ->(files) do
+ API::V3::StorageFiles::StorageFileCollectionRepresenter.new(
+ files,
+ self_link: api_v3_paths.storage_files(@storage.id),
+ current_user:
)
- },
+ end,
on_failure: ->(error) { raise_error(error) }
)
end
# RequestBody:
# {
+ # "projectId": 42
# "fileName": "ape.png",
# "parent": "/Pictures"
# }
post :prepare_upload do
raise ::API::Errors::NotFound unless OpenProject::FeatureDecisions.storage_file_upload_active?
- Storages::Peripherals::StorageRequests
- .new(storage: @storage)
- .upload_link_query(
- user: current_user,
- finalize_url: nil # ToDo: api_v3_paths.finalize_upload(@storage.id)
- )
+ (upload_link_query(@storage, current_user) >> execute_upload_link_query(request_body))
.match(
- on_success: ->(upload_link_query) {
- upload_link_query
- .call(request_body)
- .map { |link| API::V3::StorageFiles::StorageUploadLinkRepresenter.new(link, current_user:) }
- .match(
- on_success: ->(representer) { representer },
- on_failure: ->(error) { raise_error(error) }
- )
- },
+ on_success: ->(link) { API::V3::StorageFiles::StorageUploadLinkRepresenter.new(link, current_user:) },
on_failure: ->(error) { raise_error(error) }
)
end