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

Refactored FileQuery to FilesInfoQuery

Andreas Pfohlc25c8d38ab04
diff --git a/modules/storages/app/common/storages/peripherals/storage_file_info_converter.rb b/modules/storages/app/common/storages/peripherals/storage_file_info_converter.rb
new file mode 100644
index 00000000000..78fc7da9ec4
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_file_info_converter.rb
@@ -0,0 +1,46 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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
+ module StorageFileInfoConverter
+ def to_storage_file(storage_file_info)
+ Storages::StorageFile.new(
+ storage_file_info.id,
+ storage_file_info.name,
+ storage_file_info.size,
+ storage_file_info.mime_type,
+ storage_file_info.created_at,
+ storage_file_info.last_modified_at,
+ storage_file_info.owner_name,
+ storage_file_info.last_modified_by_name,
+ storage_file_info.location,
+ storage_file_info.permissions
+ )
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb
deleted file mode 100644
index 45211ce025b..00000000000
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb
+++ /dev/null
@@ -1,118 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2023 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::Nextcloud
- class FileQuery
- using Storages::Peripherals::ServiceResultRefinements
-
- FILE_INFO_PATH = 'ocs/v1.php/apps/integration_openproject/fileinfo'.freeze
-
- def initialize(storage)
- @uri = URI(storage.host).normalize
- @oauth_client = storage.oauth_client
- end
-
- def call(user:, file_id:)
- Util.token(user:, oauth_client: @oauth_client) do |token|
- file_info(file_id, token) >>
- method(:handle_access_control) >>
- method(:storage_file)
- end
- end
-
- def error(code, log_message = nil, data = nil)
- ServiceResult.failure(errors: Storages::StorageError.new(code:, log_message:, data:))
- end
-
- private
-
- def file_info(file_id, token)
- service_result = begin
- ServiceResult.success(
- result: RestClient::Request.execute(
- method: :get,
- url: Util.join_uri_path(@uri, FILE_INFO_PATH, file_id),
- headers: {
- 'Authorization' => "Bearer #{token.access_token}",
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json'
- }
- )
- )
- rescue RestClient::Unauthorized => e
- error(:not_authorized, 'Outbound request not authorized!', e.response)
- rescue RestClient::NotFound => e
- Util.error(:not_found, 'Outbound request destination not found!', e.response)
- rescue RestClient::ExceptionWithResponse => e
- Util.error(:error, 'Outbound request failed!', e.response)
- rescue StandardError
- Util.error(:error, 'Outbound request failed!')
- end
-
- # rubocop:disable Style/OpenStructUse
- service_result.map { |response| JSON.parse(response.body, object_class: OpenStruct) }
- # rubocop:enable Style/OpenStructUse
- end
-
- def handle_access_control(file_info_response)
- data = file_info_response.ocs.data
- if data.statuscode == 403
- Util.error(:forbidden)
- else
- ServiceResult.success(result: data)
- end
- end
-
- # rubocop:disable Metrics/AbcSize
- def storage_file(file_info_data)
- storage_file = ::Storages::StorageFile.new(file_info_data.id,
- file_info_data.name,
- file_info_data.size,
- file_info_data.mimetype,
- Time.zone.at(file_info_data.ctime),
- Time.zone.at(file_info_data.mtime),
- file_info_data.owner_name,
- file_info_data.modifier_name,
- location(file_info_data.path),
- file_info_data.dav_permissions)
- ServiceResult.success(result: storage_file)
- end
-
- # rubocop:enable Metrics/AbcSize
-
- def location(files_path)
- prefix = 'files/'
- idx = files_path.rindex(prefix)
- return '/' if idx == nil
-
- idx += prefix.length - 1
-
- Util.escape_path(files_path[idx..])
- end
- end
-end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
new file mode 100644
index 00000000000..90d957104f0
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
@@ -0,0 +1,124 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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::Nextcloud
+ class FilesInfoQuery
+ using Storages::Peripherals::ServiceResultRefinements
+
+ FILES_INFO_PATH = 'ocs/v1.php/apps/integration_openproject/filesinfo'.freeze
+
+ def initialize(storage)
+ @uri = URI(storage.host).normalize
+ @oauth_client = storage.oauth_client
+ end
+
+ def call(user:, file_ids: [])
+ if file_ids.nil?
+ return Util.error(:error, 'File IDs can not be nil', file_ids)
+ end
+
+ if file_ids.empty?
+ return ServiceResult.success(result: [])
+ end
+
+ Util.token(user:, oauth_client: @oauth_client) do |token|
+ files_info(file_ids, token) >> json >> create_storage_file_infos
+ end
+ end
+
+ private
+
+ def files_info(file_ids, token)
+ ServiceResult.success(
+ result: RestClient::Request.execute(
+ method: :post,
+ url: Util.join_uri_path(@uri, FILES_INFO_PATH),
+ body: { fileIds: file_ids }.to_json,
+ headers: {
+ 'Authorization' => "Bearer #{token.access_token}",
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json'
+ }
+ )
+ )
+ rescue RestClient::Unauthorized => e
+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)
+ rescue RestClient::NotFound => e
+ Util.error(:not_found, 'Outbound request destination not found!', e.response)
+ rescue RestClient::ExceptionWithResponse => e
+ Util.error(:error, 'Outbound request failed!', e.response)
+ rescue StandardError
+ Util.error(:error, 'Outbound request failed!')
+ end
+
+ def json
+ ->(response) do
+ # rubocop:disable Style/OpenStructUse
+ response
+ .map(&:body)
+ .map { |body| JSON.parse(body, object_class: OpenStruct) }
+ # rubocop:enable Style/OpenStructUse
+ end
+ end
+
+ # rubocop:disable Metrics/AbcSize
+ def create_storage_file_infos
+ ->(json) do
+ ServiceResult.success(
+ result: ::Storages::StorageFileInfo.new(json.status,
+ json.status_code,
+ json.id,
+ json.name,
+ Time.zone.at(json.mtime),
+ Time.zone.at(json.ctime),
+ json.mimetype,
+ json.size,
+ json.owner_name,
+ json.owner_id,
+ json.trashed,
+ json.modifier_name,
+ json.modifier_id,
+ json.dav_permissions,
+ location(json.path))
+ )
+ end
+ end
+
+ # rubocop:enable Metrics/AbcSize
+
+ def location(file_path)
+ prefix = 'files/'
+ idx = file_path.rindex(prefix)
+ return '/' if idx == nil
+
+ idx += prefix.length - 1
+
+ Util.escape_path(file_path[idx..])
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_requests.rb b/modules/storages/app/common/storages/peripherals/storage_requests.rb
index 737daf98604..c736df1c113 100644
--- a/modules/storages/app/common/storages/peripherals/storage_requests.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_requests.rb
@@ -39,7 +39,7 @@ module Storages::Peripherals
QUERIES = %i[
download_link_query
- file_query
+ files_info_query
files_query
file_id_query
file_ids_query
diff --git a/modules/storages/app/models/storages/storage_file_info.rb b/modules/storages/app/models/storages/storage_file_info.rb
new file mode 100644
index 00000000000..fb56d7fd170
--- /dev/null
+++ b/modules/storages/app/models/storages/storage_file_info.rb
@@ -0,0 +1,51 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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.
+#++
+
+class Storages::StorageFileInfo
+ attr_reader :status, :status_code, :id, :name, :last_modified_at, :created_at, :mime_type, :size, :owner_name,
+ :owner_id, :trashed, :last_modified_by_name, :last_modified_by_id, :permissions, :location
+
+ def initialize(status, status_code, id, name, last_modified_at, created_at, mime_type, size, owner_name, owner_id,
+ trashed, last_modified_by_name, last_modified_by_id, permissions, location)
+ @status = status
+ @status_code = status_code
+ @id = id
+ @name = name
+ @last_modified_at = last_modified_at
+ @created_at = created_at
+ @mime_type = mime_type
+ @size = size
+ @owner_name = owner_name
+ @owner_id = owner_id
+ @trashed = trashed
+ @last_modified_by_name = last_modified_by_name
+ @last_modified_by_id = last_modified_by_id
+ @permissions = permissions
+ @location = location
+ 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 15ad3567a69..b60e87ede6f 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
@@ -29,7 +29,7 @@
module API::V3::StorageFiles
class StorageFilesAPI < ::API::OpenProjectAPI
using Storages::Peripherals::ServiceResultRefinements
- helpers Storages::Peripherals::StorageErrorHelper
+ helpers Storages::Peripherals::StorageErrorHelper, Storages::Peripherals::StorageFileInfoConverter
resources :files do
get do
@@ -47,8 +47,10 @@ module API::V3::StorageFiles
get do
Storages::Peripherals::StorageRequests
.new(storage: @storage)
- .file_query
- .call(user: current_user, file_id: params[:file_id])
+ .files_info_query
+ .call(user: current_user, file_id: [params[:file_id]])
+ .map(&:first)
+ .map(&method(to_storage_file))
.match(
on_success: ->(storage_file) {
API::V3::StorageFiles::StorageFileRepresenter.new(storage_file, @storage, current_user:)
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index e8fc9ffcfa6..0905375f1fd 100644
--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
@@ -310,7 +310,7 @@ RSpec.describe Storages::Peripherals::StorageRequests, webmock: true do
include_examples 'outbound is failing', 500, :error
end
- describe '#file_query' do
+ describe '#files_info_query' do
let(:file_id) { '819' }
let(:expected_response_body) do
<<~JSON
@@ -356,7 +356,7 @@ RSpec.describe Storages::Peripherals::StorageRequests, webmock: true do
context 'with Nextcloud storage type selected' do
it 'must return a list of files when called' do
result = subject
- .file_query
+ .files_info_query
.call(user:, file_id:)
expect(result).to be_success
storage_file = result.result