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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Implemented WebDAV querying against Nextcloud storage
Andreas Pfohlce89d6e31071
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.rbindex 5faa1ba015e..aa9c96422e5 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@@ -49,9 +49,10 @@ class API::V3::FileLinks::FileLinksAPI < ::API::OpenProjectAPIget &::API::V3::Utilities::Endpoints::Show.new(model: ::Storages::FileLink).mount# A helper is used to define the behaviour at DELETE /api/v3/file_links/:file_link_id- delete &::API::V3::Utilities::Endpoints::Delete.new(model: ::Storages::FileLink,- process_service: ::Storages::FileLinks::DeleteService)- .mount+ delete &::API::V3::Utilities::Endpoints::Delete.new(+ model: ::Storages::FileLink,+ process_service: ::Storages::FileLinks::DeleteService+ ).mount# Additional API definitions are mounted under the current namespace, hence they are# appended to /api/v3/file_links/:file_link_id/...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.rbindex e3e3fa2b8e1..944ff5b2b61 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,11 +30,14 @@ module API::V3::StorageFilesclass StorageFilesAPI < ::API::OpenProjectAPIresources :files doget do- files_query = ::API::V3::Utilities::StorageRequests.new(storage: @storage).files_query+ files_query = ::API::V3::Utilities::StorageRequests+ .new(storage: @storage)+ .files_query(user: current_user)::API::V3::StorageFiles::StorageFileCollectionRepresenter.new(files_query.call,- self_link: api_v3_paths.storage_files(@storage.id), current_user:+ self_link: api_v3_paths.storage_files(@storage.id),+ current_user:)endenddiff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/abstract_storage_query.rb b/modules/storages/lib/api/v3/utilities/storage_interaction/abstract_storage_query.rbdeleted file mode 100644index 2e943165f42..00000000000--- a/modules/storages/lib/api/v3/utilities/storage_interaction/abstract_storage_query.rb+++ /dev/null@@ -1,7 +0,0 @@-module API::V3::Utilities::StorageInteraction- class AbstractStorageQuery- def files- raise NotImplementedError- end- end-enddiff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb b/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rbindex 893f65f35e1..f91f25d0ce9 100644--- a/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb+++ b/modules/storages/lib/api/v3/utilities/storage_interaction/nextcloud_storage_query.rb@@ -1,7 +1,59 @@module API::V3::Utilities::StorageInteraction- class NextcloudStorageQuery < AbstractStorageQuery+ class NextcloudStorageQuery+ def initialize(base_uri:, token:, token_refresh:)+ @uri = base_uri+ @token = token+ @token_refresh = token_refresh+ end+def files- ::Storages::StorageFile.all+ http = Net::HTTP.new(@uri.host, @uri.port)+ http.use_ssl = @uri.scheme == 'https'++ path = "/remote.php/dav/files/#{@token.origin_user_id}/"++ result = @token_refresh.call do+ response = http.propfind(+ path,+ nil,+ {+ 'Depth' => '1',+ 'Authorization' => "Bearer #{@token.access_token}"+ }+ )++ if %w[401 403].include?(response.code)+ ServiceResult.failure(result: :not_authorized)+ else+ ServiceResult.success(result: response.body)+ end+ end++ parse_response(result, path)+ end++ private++ def parse_response(response, base)+ return response unless response.success++ document = Nokogiri::XML(response.result)+ elements = document.xpath('//d:href')++ elements.drop(1).map do |e|+ normalized_name = e.children.first.to_s.delete_prefix(base)++ ::Storages::StorageFile.new(+ nil,+ CGI.unescape(normalized_name),+ 'text/plain',+ nil,+ nil,+ nil,+ nil,+ "/#{normalized_name}"+ )+ endendendenddiff --git a/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb b/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rbnew file mode 100644index 00000000000..4b0554dc0c0--- /dev/null+++ b/modules/storages/lib/api/v3/utilities/storage_interaction/storage_queries.rb@@ -0,0 +1,30 @@+module API::V3::Utilities::StorageInteraction+ class StorageQueries+ 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)+ token = connection_manager.get_access_token.result++ ::API::V3::Utilities::StorageInteraction::NextcloudStorageQuery.new(+ base_uri: @uri,+ token:,+ token_refresh: ->(&block) {+ connection_manager.request_with_token_refresh(token) do+ block.call+ end+ }+ )+ else+ raise ArgumentError+ end+ end+ end+enddiff --git a/modules/storages/lib/api/v3/utilities/storage_requests.rb b/modules/storages/lib/api/v3/utilities/storage_requests.rbindex dfa21b1c3e5..8c1275f0ba9 100644--- a/modules/storages/lib/api/v3/utilities/storage_requests.rb+++ b/modules/storages/lib/api/v3/utilities/storage_requests.rb@@ -54,14 +54,18 @@ class API::V3::Utilities::StorageRequestsendend- def files_query- case @storage.provider_type- when ::Storages::Storage::PROVIDER_TYPE_NEXTCLOUD- -> do- ::API::V3::Utilities::StorageInteraction::NextcloudStorageQuery.new.files- end- else- raise ArgumentError+ def files_query(user:)+ query = ::API::V3::Utilities::StorageInteraction::StorageQueries+ .new(+ uri: URI(@storage.host),+ provider_type: @storage.provider_type,+ user:,+ oauth_client: @oauth_client+ )+ .files_query++ -> do+ query.filesendendend