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

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.rb
index 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::OpenProjectAPI
get &::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.rb
index 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::StorageFiles
class StorageFilesAPI < ::API::OpenProjectAPI
resources :files do
get 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:
)
end
end
diff --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.rb
deleted file mode 100644
index 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
-end
diff --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.rb
index 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}"
+ )
+ 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
new file mode 100644
index 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
+end
diff --git a/modules/storages/lib/api/v3/utilities/storage_requests.rb b/modules/storages/lib/api/v3/utilities/storage_requests.rb
index 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::StorageRequests
end
end
- 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.files
end
end
end