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

[#50987] Add ParentFolder class for clearer code

Dominic Bräunlein02838362ec2e
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
index 479b7b640ed..20c2122a093 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/one_drive/files_query.rb
@@ -46,7 +46,7 @@ module Storages
@uri = storage.uri
end
- def call(user:, folder: nil)
+ def call(user:, folder:)
result = Util.using_user_token(@storage, user) do |token|
# Make the Get Request to the necessary endpoints
response = Net::HTTP.start(@uri.host, @uri.port, use_ssl: true) do |http|
@@ -107,7 +107,7 @@ module Storages
end
def empty_response(folder)
- files_result = if folder.blank?
+ files_result = if folder.root?
StorageFiles.new([], root(Digest::SHA256.hexdigest('i_am_root')), [])
else
StorageFiles.new(
@@ -168,7 +168,7 @@ module Storages
end
def uri_path_for(folder)
- return "/v1.0/drives/#{@storage.drive_id}/root/children" unless folder
+ return "/v1.0/drives/#{@storage.drive_id}/root/children" if folder.root?
"/v1.0/drives/#{@storage.drive_id}/root:#{encode_path(folder)}:/children"
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_parent_folder_extractor.rb b/modules/storages/app/common/storages/peripherals/storage_parent_folder_extractor.rb
new file mode 100644
index 00000000000..b4c7388111b
--- /dev/null
+++ b/modules/storages/app/common/storages/peripherals/storage_parent_folder_extractor.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.
+#++
+
+module Storages::Peripherals
+ class ParentFolder
+ attr_reader :path
+
+ def initialize(path)
+ @path = path
+ end
+
+ def root?
+ @path == '/'
+ end
+
+ def to_s
+ @path
+ end
+ end
+
+ module StorageParentFolderExtractor
+ def extract_parent_folder(params)
+ ParentFolder.new(params[:parent].presence || '/')
+ 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 25314b3f052..a1a00971d9a 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
@@ -31,7 +31,9 @@
module API::V3::StorageFiles
class StorageFilesAPI < ::API::OpenProjectAPI
using Storages::Peripherals::ServiceResultRefinements
- helpers Storages::Peripherals::StorageErrorHelper, Storages::Peripherals::StorageFileInfoConverter
+ helpers Storages::Peripherals::StorageErrorHelper,
+ Storages::Peripherals::StorageFileInfoConverter,
+ Storages::Peripherals::StorageParentFolderExtractor
resources :files do
get do
@@ -39,7 +41,7 @@ module API::V3::StorageFiles
.resolve("queries.#{@storage.short_provider_type}.files")
.call(
storage: @storage,
- user: current_user, folder: params[:parent]
+ user: current_user, folder: extract_parent_folder(params)
)
.match(
on_success: ->(files) { API::V3::StorageFiles::StorageFilesRepresenter.new(files, @storage, current_user:) },