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

Introduce new module Util inside storages.

Pavel Balashov1a4150415b3c
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index b40501d1192..8ca7f885d7e 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -586,14 +586,6 @@ module API
root_url.gsub(duplicate_regexp, '') + send(path, arguments)
end
-
- def self.join_uri_path(uri, *parts)
- # We use `File.join` to ensure single `/` in between every part. This API will break if executed on a
- # Windows context, as it used `\` as file separators. But we anticipate that OpenProject
- # Server is not run on a Windows context.
- # URI::join cannot be used, as it behaves very different for the path parts depending on trailing slashes.
- File.join(uri.to_s, *parts)
- end
end
def api_v3_paths
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
index 775f3349fdd..4f41b260e7c 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/create_folder_command.rb
@@ -28,15 +28,13 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class CreateFolderCommand < Storages::Peripherals::StorageInteraction::StorageCommand
- include API::V3::Utilities::PathHelper
- include Errors
using Storages::Peripherals::ServiceResultRefinements
def initialize(storage)
super()
@uri = URI(storage.host).normalize
- @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(storage.username))
+ @base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", Util.escape_whitespace(storage.username))
@groupfolder = storage.groupfolder
@username = storage.username
@password = storage.password
@@ -62,34 +60,30 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
if error_text_from_response(response) == 'The resource you tried to create already exists'
ServiceResult.success(message: 'Folder already exists.')
else
- error(:not_allowed)
+ Util.error(:not_allowed)
end
when Net::HTTPUnauthorized
- error(:not_authorized)
+ Util.error(:not_authorized)
when Net::HTTPNotFound
- error(:not_found)
+ Util.error(:not_found)
when Net::HTTPConflict
- error(:conflict, error_text_from_response(response))
+ Util.error(:conflict, error_text_from_response(response))
else
- error(:error)
+ Util.error(:error)
end
end
# rubocop:enable Metrics/AbcSize
private
- def error_text_from_response(response)
- Nokogiri::XML(response.body).xpath("//s:message").text
- end
-
- def escape_whitespace(value)
- value.gsub(' ', '%20')
- end
-
def requested_folder(folder)
raise ArgumentError.new("Folder can't be nil or empty string!") if folder.blank?
- escape_whitespace(folder)
+ Util.escape_whitespace(folder)
+ end
+
+ def error_text_from_response(response)
+ Nokogiri::XML(response.body).xpath("//s:message").text
end
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
index 896a0ecdf0f..c984baf6927 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
@@ -28,15 +28,13 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class DownloadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery
- include API::V3::Utilities::PathHelper
- include Errors
using Storages::Peripherals::ServiceResultRefinements
def initialize(base_uri:, token:, retry_proc:)
super()
@base_uri = base_uri
- @uri = api_v3_paths.join_uri_path(base_uri, '/ocs/v2.php/apps/dav/api/v1/direct')
+ @uri = Util.join_uri_path(base_uri, '/ocs/v2.php/apps/dav/api/v1/direct')
@token = token
@retry_proc = retry_proc
end
@@ -52,8 +50,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:disable Metrics/AbcSize
def outbound_response(file_link)
@retry_proc.call(@token) do |token|
- begin
- service_result = ServiceResult.success(
+ service_result = begin
+ ServiceResult.success(
result: RestClient.post(
@uri.to_s,
{ fileId: file_link.origin_id },
@@ -65,19 +63,19 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
)
)
rescue RestClient::Unauthorized => e
- service_result = error(:not_authorized, 'Outbound request not authorized!', e.response)
+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)
rescue RestClient::NotFound => e
- service_result = error(:not_found, 'Outbound request destination not found!', e.response)
+ Util.error(:not_found, 'Outbound request destination not found!', e.response)
rescue RestClient::ExceptionWithResponse => e
- service_result = error(:error, 'Outbound request failed!', e.response)
+ Util.error(:error, 'Outbound request failed!', e.response)
rescue StandardError
- service_result = error(:error, 'Outbound request failed!')
+ Util.error(:error, 'Outbound request failed!')
end
service_result.bind do |response|
# The nextcloud API returns a successful response with empty body if the authorization is missing or expired
if response.body.blank?
- error(:not_authorized, 'Outbound request not authorized!')
+ Util.error(:not_authorized, 'Outbound request not authorized!')
else
ServiceResult.success(result: response)
end
@@ -88,13 +86,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:enable Metrics/AbcSize
def download_link(token, origin_name)
- api_v3_paths.join_uri_path(@base_uri, 'index.php/apps/integration_openproject/direct', token, CGI.escape(origin_name))
+ Util.join_uri_path(@base_uri, 'index.php/apps/integration_openproject/direct', token, CGI.escape(origin_name))
end
def direct_download_token(body:)
token = parse_direct_download_token(body:)
if token.blank?
- return error(:error, "Received unexpected json response", body)
+ return Util.error(:error, "Received unexpected json response", body)
end
ServiceResult.success(result: token)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb
index 953a211eb4f..25f4a13efb9 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/file_query.rb
@@ -28,8 +28,6 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class FileQuery < Storages::Peripherals::StorageInteraction::StorageQuery
- include API::V3::Utilities::PathHelper
- include Errors
using Storages::Peripherals::ServiceResultRefinements
FILE_INFO_PATH = 'ocs/v1.php/apps/integration_openproject/fileinfo'.freeze
@@ -50,14 +48,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
private
- # rubocop:disable Metrics/AbcSize
def file_info(file_id)
@retry_proc.call(@token) do |token|
- begin
- service_result = ServiceResult.success(
+ service_result = begin
+ ServiceResult.success(
result: RestClient::Request.execute(
method: :get,
- url: api_v3_paths.join_uri_path(@base_uri, FILE_INFO_PATH, file_id),
+ url: Util.join_uri_path(@base_uri, FILE_INFO_PATH, file_id),
headers: {
'Authorization' => "Bearer #{token.access_token}",
'Accept' => 'application/json',
@@ -66,13 +63,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
)
)
rescue RestClient::Unauthorized => e
- service_result = error(:not_authorized, 'Outbound request not authorized!', e.response)
+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)
rescue RestClient::NotFound => e
- service_result = error(:not_found, 'Outbound request destination not found!', e.response)
+ Util.error(:not_found, 'Outbound request destination not found!', e.response)
rescue RestClient::ExceptionWithResponse => e
- service_result = error(:error, 'Outbound request failed!', e.response)
+ Util.error(:error, 'Outbound request failed!', e.response)
rescue StandardError
- service_result = error(:error, 'Outbound request failed!')
+ Util.error(:error, 'Outbound request failed!')
end
# rubocop:disable Style/OpenStructUse
@@ -81,12 +78,10 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
end
- # rubocop:enable Metrics/AbcSize
-
def handle_access_control(file_info_response)
data = file_info_response.ocs.data
if data.statuscode == 403
- error(:forbidden)
+ Util.error(:forbidden)
else
ServiceResult.success(result: data)
end
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 c8827fc98f9..5aefb8c1ffd 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
@@ -35,16 +35,17 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
@uri = base_uri
@token = token
@retry_proc = retry_proc
- @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(token.origin_user_id))
+ @base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", Util.escape_whitespace(token.origin_user_id))
end
- def query(parent)
+ # rubocop:disable Metrics/AbcSize
+ def query(folder)
http = Net::HTTP.new(@uri.host, @uri.port)
http.use_ssl = @uri.scheme == 'https'
result = @retry_proc.call(@token) do |token|
response = http.propfind(
- "#{@base_path}#{requested_folder(parent)}",
+ "#{@base_path}#{requested_folder(folder)}",
requested_properties,
{
'Depth' => '1',
@@ -52,22 +53,28 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
}
)
- response.is_a?(Net::HTTPSuccess) ? ServiceResult.success(result: response.body) : error(response)
+ case response
+ when Net::HTTPSuccess
+ ServiceResult.success(result: response.body)
+ when Net::HTTPNotFound
+ Util.error(:not_found)
+ when Net::HTTPUnauthorized
+ Util.error(:not_authorized)
+ else
+ Util.error(:error)
+ end
end
storage_files(result)
end
+ # rubocop:enable Metrics/AbcSize
private
- def escape_whitespace(value)
- value.gsub(' ', '%20')
- end
-
def requested_folder(folder)
return '' if folder.nil?
- escape_whitespace(folder)
+ Util.escape_whitespace(folder)
end
# rubocop:disable Metrics/AbcSize
@@ -91,24 +98,6 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:enable Metrics/AbcSize
- def error(response)
- case response
- when Net::HTTPNotFound
- error_result(:not_found)
- when Net::HTTPUnauthorized
- error_result(:not_authorized)
- else
- error_result(:error)
- end
- end
-
- def error_result(code, log_message = nil, data = nil)
- ServiceResult.failure(
- result: code, # This is needed to work with the ConnectionManager token refresh mechanism.
- errors: Storages::StorageError.new(code:, log_message:, data:)
- )
- end
-
def storage_files(response)
response.map do |xml|
a = Nokogiri::XML(xml)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/legacy_upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/legacy_upload_link_query.rb
index ac1ac9a4314..ee7444187a2 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/legacy_upload_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/legacy_upload_link_query.rb
@@ -99,8 +99,8 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def outbound_response(method:, relative_path:, payload:) # rubocop:disable Metrics/AbcSize
@retry_proc.call(@token) do |token|
- begin
- response = ServiceResult.success(
+ response = begin
+ ServiceResult.success(
result: RestClient::Request.execute(
method:,
url: @base_uri.merge(relative_path).to_s,
@@ -114,13 +114,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
)
)
rescue RestClient::Unauthorized => e
- response = error(:not_authorized, 'Outbound request not authorized!', e.response)
+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)
rescue RestClient::NotFound => e
- response = error(:not_found, 'Outbound request destination not found!', e.response)
+ Util.error(:not_found, 'Outbound request destination not found!', e.response)
rescue RestClient::ExceptionWithResponse => e
- response = error(:error, 'Outbound request failed!', e.response)
+ Util.error(:error, 'Outbound request failed!', e.response)
rescue StandardError
- response = error(:error, 'Outbound request failed!')
+ Util.error(:error, 'Outbound request failed!')
end
# rubocop:disable Style/OpenStructUse
@@ -129,7 +129,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
.bind do |r|
# The nextcloud API returns a successful response with empty body if the authorization is missing or expired
if r.body.blank?
- error(:not_authorized, 'Outbound request not authorized!')
+ Util.error(:not_authorized, 'Outbound request not authorized!')
else
ServiceResult.success(result: r)
end
@@ -139,12 +139,5 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:enable Style/OpenStructUse Style/MultilineBlockChain
end
end
-
- def error(code, log_message = nil, data = nil)
- ServiceResult.failure(
- result: code, # This is needed to work with the ConnectionManager token refresh mechanism.
- errors: Storages::StorageError.new(code:, log_message:, data:)
- )
- end
end
end
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 798b6d8d8ac..055561936c6 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
@@ -28,8 +28,6 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class SetPermissionsCommand < Storages::Peripherals::StorageInteraction::StorageCommand
- include API::V3::Utilities::PathHelper
- include Errors
using Storages::Peripherals::ServiceResultRefinements
PERMISSION_MAP = {
@@ -44,7 +42,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
super()
@uri = URI(storage.host).normalize
- @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(storage.username))
+ @base_path = Util.join_uri_path(@uri.path, "remote.php/dav/files", Util.escape_whitespace(storage.username))
@groupfolder = storage.groupfolder
@group = storage.group
@username = storage.username
@@ -67,24 +65,20 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
when Net::HTTPSuccess
ServiceResult.success
when Net::HTTPNotFound
- error(:not_found)
+ Util.error(:not_found)
when Net::HTTPUnauthorized
- error(:not_authorized)
+ Util.error(:not_authorized)
else
- error(:error)
+ Util.error(:error)
end
end
private
- def escape_whitespace(value)
- value.gsub(' ', '%20')
- end
-
def requested_folder(folder)
raise ArgumentError.new("Folder can't be nil or empty string!") if folder.blank?
- escape_whitespace(folder)
+ Util.escape_whitespace(folder)
end
def converted_permissions(permissions:)
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 f5e8522bb8c..5eea1c7bd73 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
@@ -28,8 +28,6 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class UploadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery
- include API::V3::Utilities::PathHelper
- include Errors
using Storages::Peripherals::ServiceResultRefinements # use '>>' (bind) operator for ServiceResult
URI_TOKEN_REQUEST = 'index.php/apps/integration_openproject/direct-upload-token'.freeze
@@ -53,7 +51,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def validated(data)
if data.nil? || data['parent'].nil?
- error(:error, 'Data is invalid', data)
+ Util.error(:error, 'Data is invalid', data)
else
ServiceResult.success(
result: Struct.new(:parent).new(data['parent'])
@@ -70,18 +68,18 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def build_upload_link(response)
- destination = URI.parse(api_v3_paths.join_uri_path(@base_uri, URI_UPLOAD_BASE_PATH, response.token))
+ destination = URI.parse(Util.join_uri_path(@base_uri, URI_UPLOAD_BASE_PATH, response.token))
ServiceResult.success(result: Storages::UploadLink.new(destination))
end
# rubocop:disable Metrics/AbcSize
def outbound_response(method:, relative_path:, payload:)
@retry_proc.call(@token) do |token|
- begin
- response = ServiceResult.success(
+ response = begin
+ ServiceResult.success(
result: RestClient::Request.execute(
method:,
- url: api_v3_paths.join_uri_path(@base_uri, relative_path),
+ url: Util.join_uri_path(@base_uri, relative_path),
payload: payload.to_json,
headers: {
'Authorization' => "Bearer #{token.access_token}",
@@ -91,13 +89,13 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
)
)
rescue RestClient::Unauthorized => e
- response = error(:not_authorized, 'Outbound request not authorized!', e.response)
+ Util.error(:not_authorized, 'Outbound request not authorized!', e.response)
rescue RestClient::NotFound => e
- response = error(:not_found, 'Outbound request destination not found!', e.response)
+ Util.error(:not_found, 'Outbound request destination not found!', e.response)
rescue RestClient::ExceptionWithResponse => e
- response = error(:error, 'Outbound request failed!', e.response)
+ Util.error(:error, 'Outbound request failed!', e.response)
rescue StandardError
- response = error(:error, 'Outbound request failed!')
+ Util.error(:error, 'Outbound request failed!')
end
# rubocop:disable Style/OpenStructUse
@@ -106,7 +104,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
.bind do |r|
# The nextcloud API returns a successful response with empty body if the authorization is missing or expired
if r.body.blank?
- error(:not_authorized, 'Outbound request not authorized!')
+ Util.error(:not_authorized, 'Outbound request not authorized!')
else
ServiceResult.success(result: r)
end
@@ -116,7 +114,6 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# rubocop:enable Style/OpenStructUse Style/MultilineBlockChain
end
end
-
# rubocop:enable Metrics/AbcSize
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/errors.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
similarity index 71%
rename from modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/errors.rb
rename to modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
index 8ed761faae3..dfd3c2a2fe9 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/errors.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/util.rb
@@ -26,13 +26,25 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module Storages::Peripherals::StorageInteraction::Nextcloud
- module Errors
+module Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ class << self
+ def escape_whitespace(value)
+ value.gsub(' ', '%20')
+ end
+
def error(code, log_message = nil, data = nil)
ServiceResult.failure(
result: code, # This is needed to work with the ConnectionManager token refresh mechanism.
errors: Storages::StorageError.new(code:, log_message:, data:)
)
end
+
+ def join_uri_path(uri, *parts)
+ # We use `File.join` to ensure single `/` in between every part. This API will break if executed on a
+ # Windows context, as it used `\` as file separators. But we anticipate that OpenProject
+ # Server is not run on a Windows context.
+ # URI::join cannot be used, as it behaves very different for the path parts depending on trailing slashes.
+ File.join(uri.to_s, *parts)
+ end
end
end