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

Moved storages outbound requests await from API namespace

Andreas Pfohldc611c187c07
diff --git a/modules/storages/app/common/storages/peripherals/parse_create_params_service.rb b/modules/storages/app/common/storages/peripherals/parse_create_params_service.rb
new file mode 100644
index 00000000000..6ef60603802
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/parse_create_params_service.rb
@@ -0,0 +1,76 @@
+#-- 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
+ class ParseCreateParamsService < ::API::ParseResourceParamsService
+ MAX_ELEMENTS = 20
+
+ attr_reader :request_body
+
+ private
+
+ def parse_attributes(request_body)
+ @request_body = request_body
+ assert_valid_elements
+
+ elements.map do |element|
+ super(element)
+ end
+ end
+
+ def elements
+ @elements ||= request_body.dig("_embedded", "elements")
+ end
+
+ def assert_valid_elements
+ assert_elements_is_present
+ assert_elements_is_an_array
+ assert_elements_does_not_exceed_maximum
+ end
+
+ def assert_elements_is_present
+ return if elements.present?
+
+ raise API::Errors::PropertyMissingError.new('_embedded/elements')
+ end
+
+ def assert_elements_is_an_array
+ return if elements.is_a?(Array)
+
+ raise API::Errors::PropertyFormatError.new('_embedded/elements', 'Array', elements.class.name)
+ end
+
+ def assert_elements_does_not_exceed_maximum
+ return if elements.size <= MAX_ELEMENTS
+
+ raise API::Errors::Validation.new('_embedded/elements',
+ I18n.t('api_v3.errors.too_many_elements_created_at_once',
+ max: MAX_ELEMENTS, actual: elements.size))
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/utilities/scopes.rb b/modules/storages/app/common/storages/peripherals/scopes.rb
similarity index 85%
rename from modules/storages/lib/api/v3/utilities/scopes.rb
rename to modules/storages/app/common/storages/peripherals/scopes.rb
index f3d5f562c57..b3111816b73 100644
--- a/modules/storages/lib/api/v3/utilities/scopes.rb
+++ b/modules/storages/app/common/storages/peripherals/scopes.rb
@@ -26,12 +26,14 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module API::V3::Utilities::Scopes
- def visible_storages
- ::Storages::Storage.visible(current_user)
- end
+module Storages::Peripherals
+ module Scopes
+ def visible_storages
+ ::Storages::Storage.visible(current_user)
+ end
- def visible_file_links
- ::Storages::FileLink.visible(current_user)
+ def visible_file_links
+ ::Storages::FileLink.visible(current_user)
+ end
end
end
diff --git a/modules/storages/lib/api/v3/utilities/parse_create_params_service.rb b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
similarity index 51%
rename from modules/storages/lib/api/v3/utilities/parse_create_params_service.rb
rename to modules/storages/app/common/storages/peripherals/service_result_refinements.rb
index 4a05c3c84c5..db7f14a06b4 100644
--- a/modules/storages/lib/api/v3/utilities/parse_create_params_service.rb
+++ b/modules/storages/app/common/storages/peripherals/service_result_refinements.rb
@@ -26,49 +26,16 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class API::V3::Utilities::ParseCreateParamsService < ::API::ParseResourceParamsService
- MAX_ELEMENTS = 20
-
- attr_reader :request_body
-
- private
-
- def parse_attributes(request_body)
- @request_body = request_body
- assert_valid_elements
-
- elements.map do |element|
- super(element)
+module Storages::Peripherals
+ module ServiceResultRefinements
+ refine ServiceResult do
+ def match(on_success:, on_failure:)
+ if success?
+ on_success.call(result)
+ else
+ on_failure.call(result)
+ end
+ end
end
end
-
- def elements
- @elements ||= request_body.dig("_embedded", "elements")
- end
-
- def assert_valid_elements
- assert_elements_is_present
- assert_elements_is_an_array
- assert_elements_does_not_exceed_maximum
- end
-
- def assert_elements_is_present
- return if elements.present?
-
- raise API::Errors::PropertyMissingError.new('_embedded/elements')
- end
-
- def assert_elements_is_an_array
- return if elements.is_a?(Array)
-
- raise API::Errors::PropertyFormatError.new('_embedded/elements', 'Array', elements.class.name)
- end
-
- def assert_elements_does_not_exceed_maximum
- return if elements.size <= MAX_ELEMENTS
-
- raise API::Errors::Validation.new('_embedded/elements',
- I18n.t('api_v3.errors.too_many_elements_created_at_once',
- max: MAX_ELEMENTS, actual: elements.size))
- end
end
diff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud_storage_query.rb
similarity index 66%
rename from modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
rename to modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud_storage_query.rb
index d169f2e0d14..546f9ddff6d 100644
--- a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud_storage_query.rb
@@ -1,4 +1,32 @@
-module API::V3::Utilities::StorageInteraction
+#-- 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
class NextcloudStorageQuery
def initialize(base_uri:, origin_user_id:, token:, with_refreshed_token:)
@uri = base_uri
@@ -36,11 +64,11 @@ module API::V3::Utilities::StorageInteraction
'xmlns:d' => 'DAV:',
'xmlns:oc' => 'http://owncloud.org/ns'
) do
- xml['d'].send('prop') do
- xml['oc'].send('fileid')
- xml['oc'].send('size')
- xml['d'].send('getcontenttype')
- xml['d'].send('getlastmodified')
+ xml['d'].prop do
+ xml['oc'].fileid
+ xml['oc'].size
+ xml['d'].getcontenttype
+ xml['d'].getlastmodified
xml['oc'].send('owner-display-name')
end
end
@@ -93,8 +121,8 @@ module API::V3::Utilities::StorageInteraction
def name(element)
texts = element
- .xpath('d:href')
- .map(&:inner_text)
+ .xpath('d:href')
+ .map(&:inner_text)
return nil if texts.empty?
diff --git a/modules/storages/lib/api/v3/utilities/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
similarity index 50%
rename from modules/storages/lib/api/v3/utilities/storage_requests.rb
rename to modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
index d2889a92f8f..dbaba2bba44 100644
--- a/modules/storages/lib/api/v3/utilities/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/storage_queries.rb
@@ -26,44 +26,40 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class API::V3::Utilities::StorageRequests
- def initialize(storage:)
- @storage = storage
- @oauth_client = storage.oauth_client
- end
+module Storages::Peripherals::StorageInteraction
+ class StorageQueries
+ using ::Storages::Peripherals::ServiceResultRefinements
- # The download_command is actually a query and should be refactored
- def download_command
- ->(access_token:, file_id:) do
- request_url = File.join(@oauth_client.integration.host, '/ocs/v2.php/apps/dav/api/v1/direct')
- body = { fileId: file_id }
- header = {
- 'Authorization' => "Bearer #{access_token}",
- 'OCS-APIRequest' => 'true',
- 'Accept' => 'application/json'
- }
+ def initialize(uri:, provider_type:, user:, oauth_client:)
+ @uri = uri
+ @provider_type = provider_type
+ @user = user
+ @oauth_client = oauth_client
+ end
- begin
- response = RestClient.post request_url, body, header
- rescue RestClient::Unauthorized
- return ServiceResult.failure(result: I18n.t('http.request.failed_authorization'))
- rescue StandardError => e
- return ServiceResult.failure(result: e.message)
+ def files_query
+ case @provider_type
+ when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
+ connection_manager = ::OAuthClients::ConnectionManager.new(user: @user, oauth_client: @oauth_client)
+ connection_manager.get_access_token.match(
+ on_success: ->(token) do
+ ServiceResult.success(
+ result:
+ ::Storages::Peripherals::StorageInteraction::NextcloudStorageQuery.new(
+ base_uri: @uri,
+ origin_user_id: token.origin_user_id,
+ token: token.access_token,
+ with_refreshed_token: connection_manager.method(:with_refreshed_token).to_proc
+ )
+ )
+ end,
+ on_failure: ->(_) do
+ ServiceResult.failure(result: :not_authorized)
+ end
+ )
+ else
+ raise ArgumentError
end
-
- ServiceResult.success(result: response)
end
end
-
- def files_query(user:)
- ::API::V3::Utilities::StorageInteraction::StorageQueries
- .new(
- uri: URI(@storage.host),
- provider_type: @storage.provider_type,
- user:,
- oauth_client: @oauth_client
- )
- .files_query
- .map { |query| query.method(:files).to_proc }
- end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_requests.rb
new file mode 100644
index 00000000000..9484da745ed
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -0,0 +1,71 @@
+#-- 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
+ class StorageRequests
+ def initialize(storage:)
+ @storage = storage
+ @oauth_client = storage.oauth_client
+ end
+
+ # The download_command is actually a query and should be refactored
+ def download_command
+ ->(access_token:, file_id:) do
+ request_url = File.join(@oauth_client.integration.host, '/ocs/v2.php/apps/dav/api/v1/direct')
+ body = { fileId: file_id }
+ header = {
+ 'Authorization' => "Bearer #{access_token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json'
+ }
+
+ begin
+ response = RestClient.post request_url, body, header
+ rescue RestClient::Unauthorized
+ return ServiceResult.failure(result: I18n.t('http.request.failed_authorization'))
+ rescue StandardError => e
+ return ServiceResult.failure(result: e.message)
+ end
+
+ ServiceResult.success(result: response)
+ end
+ end
+
+ def files_query(user:)
+ ::Storages::Peripherals::StorageInteraction::StorageQueries
+ .new(
+ uri: URI(@storage.host),
+ provider_type: @storage.provider_type,
+ user:,
+ oauth_client: @oauth_client
+ )
+ .files_query
+ .map { |query| query.method(:files).to_proc }
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_url_helper.rb b/modules/storages/app/common/storages/peripherals/storage_url_helper.rb
new file mode 100644
index 00000000000..5bf507611ff
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_url_helper.rb
@@ -0,0 +1,115 @@
+#-- 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
+ # Helper for open and download links for a file link object.
+ module StorageUrlHelper
+ def storage_url_open_file(file_link, open_location: false)
+ location_flag = ActiveModel::Type::Boolean.new.cast(open_location) ? 0 : 1
+
+ "#{file_link.storage.host}/index.php/f/#{file_link.origin_id}?openfile=#{location_flag}"
+ end
+
+ def storage_url_open(storage)
+ "#{storage.host}/index.php/apps/files"
+ end
+
+ # rubocop:disable Metrics/AbcSize
+ def make_download_url(file_link:, user:)
+ storage = file_link.storage
+ oauth_client = storage.oauth_client
+
+ client_token = get_oauth_client_token(user:, oauth_client:)
+ return client_token if client_token.failure?
+
+ direct_download_response = make_direct_download storage:,
+ access_token: client_token.result.access_token,
+ file_id: file_link.origin_id
+
+ return direct_download_response if direct_download_response.failure?
+
+ download_token = direct_download_token(body: direct_download_response.result)
+ return download_token if download_token.failure?
+
+ url = "#{storage.host}/index.php/apps/integration_openproject/direct/#{download_token.result}/#{file_link.origin_name}"
+ ServiceResult.success(result: url)
+ end
+
+ # rubocop:enable Metrics/AbcSize
+
+ private
+
+ def get_oauth_client_token(user:, oauth_client:)
+ client_token = ::OAuthClients::ConnectionManager
+ .new(user:, oauth_client:)
+ .get_access_token
+
+ client_token.success? ? client_token : ServiceResult.failure(result: I18n.t('http.request.missing_authorization'))
+ end
+
+ def make_direct_download(storage:, access_token:, file_id:)
+ response = API::V3::Utilities::StorageRequests
+ .new(storage:)
+ .download_command
+ .call(access_token:, file_id:)
+
+ return response if response.failure?
+
+ # The nextcloud API returns a successful response with empty body if the authorization is missing or expired
+ return ServiceResult.failure(result: I18n.t('http.request.failed_authorization')) if response.result.body.blank?
+
+ ServiceResult.success(result: response.result.body)
+ end
+
+ def direct_download_token(body:)
+ token = parse_direct_download_token(body:)
+ if token.blank?
+ Rails.logger.error "Received unexpected json response: #{body}"
+ return ServiceResult.failure(result: I18n.t('http.response.unexpected'))
+ end
+
+ ServiceResult.success(result: token)
+ end
+
+ def parse_direct_download_token(body:)
+ begin
+ json = JSON.parse body
+ rescue JSON::ParserError
+ return nil
+ end
+
+ direct_download_url = json.dig('ocs', 'data', 'url')
+ return nil if direct_download_url.blank?
+
+ path = URI.parse(direct_download_url).path
+ return nil if path.blank?
+
+ path.split('/').last
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index e9a8bbfc53b..e7da280ec5c 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -50,7 +50,7 @@ module API::V3::FileLinks
include API::Decorators::LinkedResource
include API::Decorators::DateProperty
include ::API::Caching::CachedRepresenter
- include API::V3::Utilities::StorageUrlHelper
+ include Storages::Peripherals::StorageUrlHelper
property :id
diff --git a/modules/storages/lib/api/v3/file_links/file_links_api.rb b/modules/storages/lib/api/v3/file_links/file_links_api.rb
index aa9c96422e5..c162d72b7c8 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_api.rb
@@ -32,7 +32,7 @@
class API::V3::FileLinks::FileLinksAPI < ::API::OpenProjectAPI
# helpers is defined by the grape framework. They make methods from the
# module available from within the endpoint context.
- helpers API::V3::Utilities::Scopes
+ helpers Storages::Peripherals::Scopes
# The `:resources` keyword defines the API namespace -> /api/v3/file_links/...
resources :file_links do
diff --git a/modules/storages/lib/api/v3/file_links/file_links_download_api.rb b/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
index 68c269794b2..00eca9b4318 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
@@ -27,7 +27,7 @@
#++
class API::V3::FileLinks::FileLinksDownloadAPI < ::API::OpenProjectAPI
- helpers API::V3::Utilities::StorageUrlHelper
+ helpers Storages::Peripherals::StorageUrlHelper
resources :download do
get do
diff --git a/modules/storages/lib/api/v3/file_links/file_links_open_api.rb b/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
index d9490a0ec34..a2170393e00 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
@@ -27,7 +27,7 @@
#++
class API::V3::FileLinks::FileLinksOpenAPI < ::API::OpenProjectAPI
- helpers API::V3::Utilities::StorageUrlHelper
+ helpers Storages::Peripherals::StorageUrlHelper
resources :open do
get do
diff --git a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
index e0fff0daa12..0f22f7b771d 100644
--- a/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/work_packages_file_links_api.rb
@@ -33,7 +33,7 @@
class API::V3::FileLinks::WorkPackagesFileLinksAPI < ::API::OpenProjectAPI
# helpers is defined by the grape framework. They make methods from the
# module available from within the endpoint context.
- helpers API::V3::Utilities::Scopes
+ helpers Storages::Peripherals::Scopes
# The `:resources` keyword defines the API namespace -> /api/v3/work_packages/:id/file_links/...
resources :file_links do
@@ -72,7 +72,7 @@ class API::V3::FileLinks::WorkPackagesFileLinksAPI < ::API::OpenProjectAPI
post &::API::V3::FileLinks::CreateEndpoint
.new(
model: ::Storages::FileLink,
- parse_service: ::API::V3::Utilities::ParseCreateParamsService,
+ parse_service: Storages::Peripherals::ParseCreateParamsService,
render_representer: ::API::V3::FileLinks::FileLinkCollectionRepresenter
)
.mount
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 6cb45b90640..c8abc959084 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
@@ -28,7 +28,7 @@
module API::V3::StorageFiles
class StorageFilesAPI < ::API::OpenProjectAPI
- using ::API::V3::Utilities::ServiceResultRefinements
+ using Storages::Peripherals::ServiceResultRefinements
helpers do
def handle_files_error(files)
@@ -45,7 +45,7 @@ module API::V3::StorageFiles
resources :files do
get do
- ::API::V3::Utilities::StorageRequests
+ ::Storages::Peripherals::StorageRequests
.new(storage: @storage)
.files_query(user: current_user)
.match(
diff --git a/modules/storages/lib/api/v3/storages/storage_representer.rb b/modules/storages/lib/api/v3/storages/storage_representer.rb
index be7bb30d46d..e755b55a90f 100644
--- a/modules/storages/lib/api/v3/storages/storage_representer.rb
+++ b/modules/storages/lib/api/v3/storages/storage_representer.rb
@@ -39,7 +39,7 @@ module API::V3::Storages
# LinkedResource module defines helper methods to describe attributes
include API::Decorators::LinkedResource
include API::Decorators::DateProperty
- include API::V3::Utilities::StorageUrlHelper
+ include Storages::Peripherals::StorageUrlHelper
def initialize(model, current_user:, embed_links: nil)
@connection_manager =
diff --git a/modules/storages/lib/api/v3/storages/storages_api.rb b/modules/storages/lib/api/v3/storages/storages_api.rb
index 5979c0605b9..69d8a5e07b2 100644
--- a/modules/storages/lib/api/v3/storages/storages_api.rb
+++ b/modules/storages/lib/api/v3/storages/storages_api.rb
@@ -35,7 +35,7 @@
class API::V3::Storages::StoragesAPI < ::API::OpenProjectAPI
# helpers is defined by the grape framework. They make methods from the
# module available from within the endpoint context.
- helpers API::V3::Utilities::Scopes
+ helpers Storages::Peripherals::Scopes
# The `:resources` keyword defines the API namespace -> /api/v3/storages/...
resources :storages do
diff --git a/modules/storages/lib/api/v3/utilities/service_result_refinements.rb b/modules/storages/lib/api/v3/utilities/service_result_refinements.rb
deleted file mode 100644
index 605b06ae774..00000000000
--- a/modules/storages/lib/api/v3/utilities/service_result_refinements.rb
+++ /dev/null
@@ -1,11 +0,0 @@
-module API::V3::Utilities::ServiceResultRefinements
- refine ServiceResult do
- def match(on_success:, on_failure:)
- if success?
- on_success.call(result)
- else
- on_failure.call(result)
- end
- end
- end
-end
diff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb b/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb
deleted file mode 100644
index 76e632faa5a..00000000000
--- a/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb
+++ /dev/null
@@ -1,37 +0,0 @@
-module API::V3::Utilities::StorageInteraction
- class StorageQueries
- using ::API::V3::Utilities::ServiceResultRefinements
-
- def initialize(uri:, provider_type:, user:, oauth_client:)
- @uri = uri
- @provider_type = provider_type
- @user = user
- @oauth_client = oauth_client
- end
-
- def files_query
- case @provider_type
- when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD
- connection_manager = ::OAuthClients::ConnectionManager.new(user: @user, oauth_client: @oauth_client)
- connection_manager.get_access_token.match(
- on_success: ->(token) do
- ServiceResult.success(
- result:
- ::API::V3::Utilities::StorageInteraction::NextcloudStorageQuery.new(
- base_uri: @uri,
- origin_user_id: token.origin_user_id,
- token: token.access_token,
- with_refreshed_token: connection_manager.method(:with_refreshed_token).to_proc
- )
- )
- end,
- on_failure: ->(_) do
- ServiceResult.failure(result: :not_authorized)
- end
- )
- else
- raise ArgumentError
- end
- end
- end
-end
diff --git a/modules/storages/lib/api/v3/utilities/storage_url_helper.rb b/modules/storages/lib/api/v3/utilities/storage_url_helper.rb
deleted file mode 100644
index 9a3a659aa3f..00000000000
--- a/modules/storages/lib/api/v3/utilities/storage_url_helper.rb
+++ /dev/null
@@ -1,112 +0,0 @@
-#-- 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.
-#++
-
-# Helper for open and download links for a file link object.
-module API::V3::Utilities::StorageUrlHelper
- def storage_url_open_file(file_link, open_location: false)
- location_flag = ActiveModel::Type::Boolean.new.cast(open_location) ? 0 : 1
-
- "#{file_link.storage.host}/index.php/f/#{file_link.origin_id}?openfile=#{location_flag}"
- end
-
- def storage_url_open(storage)
- "#{storage.host}/index.php/apps/files"
- end
-
- # rubocop:disable Metrics/AbcSize
- def make_download_url(file_link:, user:)
- storage = file_link.storage
- oauth_client = storage.oauth_client
-
- client_token = get_oauth_client_token(user:, oauth_client:)
- return client_token if client_token.failure?
-
- direct_download_response = make_direct_download storage:,
- access_token: client_token.result.access_token,
- file_id: file_link.origin_id
- return direct_download_response if direct_download_response.failure?
-
- download_token = direct_download_token(body: direct_download_response.result)
- return download_token if download_token.failure?
-
- url = "#{storage.host}/index.php/apps/integration_openproject/direct/#{download_token.result}/#{file_link.origin_name}"
- ServiceResult.success(result: url)
- end
-
- # rubocop:enable Metrics/AbcSize
-
- private
-
- def get_oauth_client_token(user:, oauth_client:)
- client_token = ::OAuthClients::ConnectionManager
- .new(user:, oauth_client:)
- .get_access_token
-
- client_token.success? ? client_token : ServiceResult.failure(result: I18n.t('http.request.missing_authorization'))
- end
-
- def make_direct_download(storage:, access_token:, file_id:)
- response = API::V3::Utilities::StorageRequests
- .new(storage:)
- .download_command
- .call(access_token:, file_id:)
-
- return response if response.failure?
-
- # The nextcloud API returns a successful response with empty body if the authorization is missing or expired
- return ServiceResult.failure(result: I18n.t('http.request.failed_authorization')) if response.result.body.blank?
-
- ServiceResult.success(result: response.result.body)
- end
-
- def direct_download_token(body:)
- token = parse_direct_download_token(body:)
- if token.blank?
- Rails.logger.error "Received unexpected json response: #{body}"
- return ServiceResult.failure(result: I18n.t('http.response.unexpected'))
- end
-
- ServiceResult.success(result: token)
- end
-
- def parse_direct_download_token(body:)
- begin
- json = JSON.parse body
- rescue JSON::ParserError
- return nil
- end
-
- direct_download_url = json.dig('ocs', 'data', 'url')
- return nil if direct_download_url.blank?
-
- path = URI.parse(direct_download_url).path
- return nil if path.blank?
-
- path.split('/').last
- end
-end