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

[#52886] Implement FileIdsDeepQuery for Sharepoint

Eric Schubert1a79355f926b
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/folder_files_file_ids_deep_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/folder_files_file_ids_deep_query.rb
index 3da2b94fe5e..d648c29d60e 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/folder_files_file_ids_deep_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/folder_files_file_ids_deep_query.rb
@@ -30,16 +30,18 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class FolderFilesFileIdsDeepQuery
- def self.call(storage:, path:)
+ def self.call(storage:, folder:)
::Storages::Peripherals::Registry
.resolve("queries.nextcloud.propfind")
.call(
storage:,
depth: 'infinity',
- path:,
+ path: folder.path,
# nc:acl-list is only required to avoid https://community.openproject.org/wp/49628. See comment #4.
props: %w[oc:fileid nc:acl-list]
- )
+ ).map do |obj|
+ obj.transform_values { |value| Storages::StorageFileInfo.from_id(value["fileid"]) }
+ end
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb
index 65de8a06d87..d9400a0f691 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive.rb
@@ -39,6 +39,7 @@ module Storages
register(:file_info, FileInfoQuery)
register(:files_info, FilesInfoQuery)
register(:open_file_link, OpenFileLinkQuery)
+ register(:folder_files_file_ids_deep_query, FolderFilesFileIdsDeepQuery)
register(:open_storage, OpenStorageQuery)
register(:upload_link, UploadLinkQuery)
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb
index b333fca82de..e21304d4c62 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/file_info_query.rb
@@ -53,7 +53,9 @@ module Storages
)
end
- @delegate.call(user:, drive_item_id: file_id, fields: FIELDS).map(&storage_file_infos)
+ Util.using_user_token(@storage, user) do |token|
+ @delegate.call(token:, drive_item_id: file_id, fields: FIELDS).map(&storage_file_infos)
+ end
end
private
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/folder_files_file_ids_deep_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/folder_files_file_ids_deep_query.rb
new file mode 100644
index 00000000000..fe87df81cac
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/folder_files_file_ids_deep_query.rb
@@ -0,0 +1,137 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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::OneDrive
+ class FolderFilesFileIdsDeepQuery
+ FIELDS = %w[id name file folder parentReference].freeze
+
+ def self.call(storage:, folder:)
+ new(storage).call(folder:)
+ end
+
+ def initialize(storage)
+ @storage = storage
+ @delegate = Internal::ChildrenQuery.new(storage)
+ end
+
+ def call(folder:)
+ Util.using_admin_token(@storage) do |http|
+ fetch_result = fetch_folder(http, folder)
+ return fetch_result if fetch_result.failure?
+
+ file_ids_dictionary = fetch_result.result
+ queue = [folder]
+
+ while queue.any?
+ dir = queue.shift
+
+ visit = visit(http, dir)
+ return visit if visit.failure?
+
+ entry, to_queue = visit.result.values_at(:entry, :to_queue)
+ file_ids_dictionary = file_ids_dictionary.merge(entry)
+ queue.concat(to_queue)
+ end
+
+ file_ids_dictionary
+ end
+ end
+
+ private
+
+ def visit(http, folder)
+ call = @delegate.call(http:, folder:, fields: FIELDS)
+ return call if call.failure?
+
+ entry = {}
+ to_queue = []
+
+ call.result[:value].each do |json|
+ new_entry, folder = parse_drive_item_info(json).values_at(:entry, :folder)
+
+ entry = entry.merge(new_entry)
+ if folder.present?
+ to_queue.append(folder)
+ end
+ end
+
+ ServiceResult.success(result: { entry:, to_queue: })
+ end
+
+ def parse_drive_item_info(json)
+ drive_item_id = json[:id]
+ location = Util.extract_location(json[:parentReference], json[:name])
+
+ entry = { location => Storages::StorageFileInfo.from_id(drive_item_id) }
+ folder = json[:folder].present? ? Storages::Peripherals::ParentFolder.new(drive_item_id) : nil
+
+ { entry:, folder: }
+ end
+
+ # TODO: REMOVE WITH #51713, as this should be replaced by internal drive item query
+ # with harmonized interface for authentication
+
+ def fetch_folder(http, folder)
+ uri_path = if folder.root?
+ "/v1.0/drives/#{@storage.drive_id}/root"
+ else
+ "/v1.0/drives/#{@storage.drive_id}/items/#{folder}"
+ end
+
+ response = http.get(Util.join_uri_path(@storage.uri, "#{uri_path}?$select=id,name,parentReference"))
+ handle_responses(response).map do |json|
+ if folder.root?
+ { '/' => Storages::StorageFileInfo.from_id(json[:id]) }
+ else
+ parse_drive_item_info(json)[:entry]
+ end
+ end
+ end
+
+ def handle_responses(response)
+ case response
+ in { status: 200..299 }
+ ServiceResult.success(result: response.json(symbolize_keys: true))
+ in { status: 404 }
+ ServiceResult.failure(result: :not_found,
+ errors: UTIL.storage_error(response:, code: :not_found, source: self))
+ in { status: 403 }
+ ServiceResult.failure(result: :forbidden,
+ errors: UTIL.storage_error(response:, code: :forbidden, source: self))
+ in { status: 401 }
+ ServiceResult.failure(result: :unauthorized,
+ errors: UTIL.storage_error(response:, code: :unauthorized, source: self))
+ else
+ data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
+ ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb
new file mode 100644
index 00000000000..2acd8a2e370
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/children_query.rb
@@ -0,0 +1,94 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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
+ module Peripherals
+ module StorageInteraction
+ module OneDrive
+ module Internal
+ class ChildrenQuery
+ UTIL = ::Storages::Peripherals::StorageInteraction::OneDrive::Util
+
+ def self.call(storage:, http:, folder:, fields: [])
+ new(storage).call(http:, folder:, fields:)
+ end
+
+ def initialize(storage)
+ @storage = storage
+ @uri = storage.uri
+ end
+
+ def call(http:, folder:, fields: [])
+ select_url_query = if fields.empty?
+ ''
+ else
+ "?$select=#{fields.join(',')}"
+ end
+
+ make_children_request(folder, http, select_url_query)
+ end
+
+ private
+
+ def make_children_request(folder, http, select_url_query)
+ response = http.get(UTIL.join_uri_path(@uri, uri_path_for(folder) + select_url_query))
+ handle_responses(response)
+ end
+
+ def handle_responses(response)
+ case response
+ in { status: 200..299 }
+ ServiceResult.success(result: response.json(symbolize_keys: true))
+ in { status: 404 }
+ ServiceResult.failure(result: :not_found,
+ errors: UTIL.storage_error(response:, code: :not_found, source: self))
+ in { status: 403 }
+ ServiceResult.failure(result: :forbidden,
+ errors: UTIL.storage_error(response:, code: :forbidden, source: self))
+ in { status: 401 }
+ ServiceResult.failure(result: :unauthorized,
+ errors: UTIL.storage_error(response:, code: :unauthorized, source: self))
+ else
+ data = ::Storages::StorageErrorData.new(source: self.class, payload: response)
+ ServiceResult.failure(result: :error, errors: ::Storages::StorageError.new(code: :error, data:))
+ end
+ end
+
+ def uri_path_for(folder)
+ return "/v1.0/drives/#{@storage.drive_id}/root/children" if folder.root?
+
+ "/v1.0/drives/#{@storage.drive_id}/items/#{folder.path}/children"
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
index 62fdf5cd155..cb52988648f 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/internal/drive_item_query.rb
@@ -36,8 +36,8 @@ module Storages
class DriveItemQuery
UTIL = ::Storages::Peripherals::StorageInteraction::OneDrive::Util
- def self.call(storage:, user:, drive_item_id:, fields: [])
- new(storage).call(user:, drive_item_id:, fields:)
+ def self.call(storage:, token:, drive_item_id:, fields: [])
+ new(storage).call(token:, drive_item_id:, fields:)
end
def initialize(storage)
@@ -45,16 +45,14 @@ module Storages
@uri = storage.uri
end
- def call(user:, drive_item_id:, fields: [])
+ def call(token:, drive_item_id:, fields: [])
select_url_query = if fields.empty?
''
else
"?$select=#{fields.join(',')}"
end
- UTIL.using_user_token(@storage, user) do |token|
- make_file_request(drive_item_id, token, select_url_query)
- end
+ make_file_request(drive_item_id, token, select_url_query)
end
private
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb
index 5293235f64f..57bd5c8303e 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/open_file_link_query.rb
@@ -46,24 +46,26 @@ module Storages
def call(user:, file_id:, open_location: false)
@user = user
- if open_location
- request_parent_id.call(file_id) >> request_web_url
- else
- request_web_url.call(file_id)
+ Util.using_user_token(@storage, user) do |token|
+ if open_location
+ request_parent_id(token).call(file_id) >> request_web_url(token)
+ else
+ request_web_url(token).call(file_id)
+ end
end
end
private
- def request_web_url
+ def request_web_url(token)
->(file_id) do
- @delegate.call(user: @user, drive_item_id: file_id, fields: %w[webUrl]).map(&web_url)
+ @delegate.call(token:, drive_item_id: file_id, fields: %w[webUrl]).map(&web_url)
end
end
- def request_parent_id
+ def request_parent_id(token)
->(file_id) do
- @delegate.call(user: @user, drive_item_id: file_id, fields: %w[parentReference]).map(&parent_id)
+ @delegate.call(token:, drive_item_id: file_id, fields: %w[parentReference]).map(&parent_id)
end
end
diff --git a/modules/storages/app/models/storages/storage_file_info.rb b/modules/storages/app/models/storages/storage_file_info.rb
index 35a7215856b..ccc1dddf772 100644
--- a/modules/storages/app/models/storages/storage_file_info.rb
+++ b/modules/storages/app/models/storages/storage_file_info.rb
@@ -79,29 +79,9 @@ module Storages
location:
)
end
+
+ def self.from_id(file_id)
+ new(id: file_id, status: 'OK', status_code: 200)
+ end
end
end
-
-# 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/app/services/projects/copy/file_links_dependent_service.rb b/modules/storages/app/services/projects/copy/file_links_dependent_service.rb
index 905a4644703..9f6bbd15038 100644
--- a/modules/storages/app/services/projects/copy/file_links_dependent_service.rb
+++ b/modules/storages/app/services/projects/copy/file_links_dependent_service.rb
@@ -64,7 +64,7 @@ module Projects::Copy
file_ids: source_file_links.map(&:origin_id))
folder_files_file_ids_deep_query_result = folder_files_file_ids_deep_query(
storage:,
- path: target_project_storage.managed_project_folder_path
+ location: target_project_storage.managed_project_folder_path
)
source_file_links.each do |old_file_link|
attributes = {
@@ -120,10 +120,10 @@ module Projects::Copy
.result
end
- def folder_files_file_ids_deep_query(storage:, path:)
+ def folder_files_file_ids_deep_query(storage:, location:)
Storages::Peripherals::Registry
.resolve("queries.#{storage.short_provider_type}.folder_files_file_ids_deep_query")
- .call(storage:, path:)
+ .call(storage:, folder: Storages::Peripherals::ParentFolder.new(location))
.on_failure { |r| add_error!("folder_files_file_ids_deep_query", r.to_active_model_errors) }
.result
end