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

Use only httx in nextcloud related requests.

Pavel Balashou89e4d06f8b00
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
index f4ea5b8916f..bbf24cc8f80 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/copy_template_folder_command.rb
@@ -79,9 +79,6 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def validate_destination
->(urls) do
- request = Net::HTTP::Head.new(urls[:destination_url])
- request.initialize_http_header Util.basic_auth_header(@username, @password)
-
response = Util
.httpx
.basic_auth(@username, @password)
@@ -100,7 +97,6 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end
- # rubocop:disable Metrics/AbcSize
def copy_folder
->(urls) do
response = Util
@@ -110,7 +106,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
urls[:source_url],
headers: {
'Destination' => urls[:destination_url],
- 'Depth' => 'infinity',
+ 'Depth' => 'infinity'
})
case response.status
@@ -127,7 +123,5 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end
end
-
- # rubocop:enable Metrics/AbcSize
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
index 3038fde0a9b..96dad117732 100644
--- 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
@@ -60,15 +60,14 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
private
def files_info(file_ids, token)
- httpx = HTTPX.with(headers:
- {
- 'Authorization' => "Bearer #{token.access_token}",
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'OCS-APIRequest' => 'true'
- }
- )
- response = httpx.post(Util.join_uri_path(@uri.to_s, FILES_INFO_PATH), json: { fileIds: file_ids })
+ response = Util
+ .httpx
+ .with(headers: { 'Authorization' => "Bearer #{token.access_token}",
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json',
+ 'OCS-APIRequest' => 'true' })
+ .post(Util.join_uri_path(@uri.to_s, FILES_INFO_PATH),
+ json: { fileIds: file_ids })
case response.status
when 200
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
index 0c0bbcac81a..bdd0455219a 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
@@ -105,14 +105,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def storage_files(response)
response.map do |xml|
- a = Nokogiri::XML(xml)
- .xpath('//d:response')
- .to_a
-
- parent, *files =
- a.map do |file_element|
- storage_file(file_element)
- end
+ parent, *files = Nokogiri::XML(xml)
+ .xpath('//d:response')
+ .to_a
+ .map { |file_element| storage_file(file_element) }
::Storages::StorageFiles.new(files, parent, ancestors(parent.location))
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
index c2be065d34a..2472fbcd575 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/internal/delete_entity_command.rb
@@ -44,7 +44,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Internal
response = UTIL
.httpx
.basic_auth(@username, @password)
- .delete(UTIL.join_uri_path(@uri, "remote.php/dav/files",
+ .delete(UTIL.join_uri_path(@uri,
+ "remote.php/dav/files",
CGI.escapeURIComponent(@username),
UTIL.escape_path(location)))
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
index 18f90cde322..2e767d916eb 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/set_permissions_command.rb
@@ -81,7 +81,6 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
response = Util
.httpx
- .with(debug_level: 1, debug: $stdout)
.basic_auth(@username, @password)
.request(
"PROPPATCH",
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
index cbe90cf9d2d..0b41fc73def 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
@@ -66,28 +66,28 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
private
def outbound_response(method:, relative_path:, payload:, token:)
- response = Util.http(@uri).post(
- Util.join_uri_path(@uri.path, relative_path),
- payload.to_json,
- {
- 'Authorization' => "Bearer #{token.access_token}",
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json'
- }
- )
- case response
- when Net::HTTPSuccess
+ response = Util
+ .httpx
+ .with(headers: { 'Authorization' => "Bearer #{token.access_token}",
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json' })
+ .post(
+ Util.join_uri_path(@uri, relative_path),
+ json: payload
+ )
+ case response.status
+ when 200
# The nextcloud API returns a successful response with empty body if the authorization is missing or expired
if response.body.present?
ServiceResult.success(
- result: JSON.parse(response.body, object_class: OpenStruct) # rubocop:disable Style/OpenStructUse
+ result: JSON.parse(response.body.to_s, object_class: OpenStruct) # rubocop:disable Style/OpenStructUse
)
else
Util.error(:unauthorized, 'Outbound request not authorized!')
end
- when Net::HTTPNotFound
+ when 404
Util.error(:not_found, 'Outbound request destination not found!', response)
- when Net::HTTPUnauthorized
+ when 401
Util.error(:unauthorized, 'Outbound request not authorized!', response)
else
Util.error(:error, 'Outbound request failed!')
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
index 19acdbcb7a7..63c3868bfcb 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
@@ -73,12 +73,6 @@ module Storages::Peripherals::StorageInteraction::Nextcloud::Util
)
end
- def http(uri)
- http = Net::HTTP.new(uri.host, uri.port)
- http.use_ssl = uri.scheme == 'https'
- http
- end
-
def httpx
HTTPX.plugin(:basic_auth).plugin(:webdav)
end
diff --git a/modules/storages/app/validator/nextcloud_application_credentials_validator.rb b/modules/storages/app/validator/nextcloud_application_credentials_validator.rb
index 8b029fd4872..70e544db5f8 100644
--- a/modules/storages/app/validator/nextcloud_application_credentials_validator.rb
+++ b/modules/storages/app/validator/nextcloud_application_credentials_validator.rb
@@ -26,6 +26,8 @@
# See COPYRIGHT and LICENSE files for more details.
#++
class NextcloudApplicationCredentialsValidator
+ UTIL = Storages::Peripherals::StorageInteraction::Nextcloud::Util
+
attr_reader :contract, :uri
def initialize(contract)
@@ -36,29 +38,17 @@ class NextcloudApplicationCredentialsValidator
def call
return unless contract.model.password_changed?
- case make_http_head_request_from(build_http_head_request)
- when Net::HTTPSuccess
+ response = UTIL
+ .httpx
+ .basic_auth(contract.username, contract.password)
+ .head(UTIL.join_uri_path(uri, "remote.php/dav"))
+ case response.status
+ when 200
true
- when Net::HTTPUnauthorized
+ when 401
contract.errors.add(:password, :invalid_password)
else
contract.errors.add(:password, :unknown_error)
end
end
-
- private
-
- def build_http_head_request
- request = Net::HTTP::Head.new Storages::Peripherals::StorageInteraction::Nextcloud::Util
- .join_uri_path(uri.path, "remote.php/dav")
- request.initialize_http_header Storages::Peripherals::StorageInteraction::Nextcloud::Util
- .basic_auth_header(contract.username, contract.password)
- request
- end
-
- def make_http_head_request_from(request)
- Storages::Peripherals::StorageInteraction::Nextcloud::Util
- .http(uri)
- .request(request)
- end
end