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

[#47113] Direct download of a storage file fails

Eric Schubertae34a2096862
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index d43ec759d45..ff72e0dbb94 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -586,6 +586,14 @@ 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/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
index 53a88873969..b2eff83b29a 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,13 +28,14 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class DownloadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery
+ include API::V3::Utilities::PathHelper
using Storages::Peripherals::ServiceResultRefinements
def initialize(base_uri:, token:, retry_proc:)
super()
@base_uri = base_uri
- @uri = URI::join(base_uri, '/ocs/v2.php/apps/dav/api/v1/direct')
+ @uri = api_v3_paths.join_uri_path(base_uri, '/ocs/v2.php/apps/dav/api/v1/direct')
@token = token
@retry_proc = retry_proc
end
@@ -47,6 +48,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
private
+ # rubocop:disable Metrics/AbcSize
def outbound_response(file_link)
@retry_proc.call(@token) do |token|
begin
@@ -71,18 +73,19 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
response = error(:error, 'Outbound request failed!')
end
- response
- .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!')
- else
- ServiceResult.success(result: r)
- end
+ response.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!')
+ else
+ ServiceResult.success(result: r)
end
+ end
end
end
+ # rubocop:enable Metrics/AbcSize
+
def error(code, log_message = nil, data = nil)
ServiceResult.failure(
result: code, # This is needed to work with the ConnectionManager token refresh mechanism.
@@ -91,7 +94,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def download_link(token, origin_name)
- URI::join(@base_uri, "/index.php/apps/integration_openproject/direct/#{token}/#{CGI.escape(origin_name)}")
+ api_v3_paths.join_uri_path(@base_uri, 'index.php/apps/integration_openproject/direct', token, CGI.escape(origin_name))
end
def direct_download_token(body:)
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 88cea3ac198..d95e5367f59 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
@@ -28,12 +28,14 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class FilesQuery < Storages::Peripherals::StorageInteraction::StorageQuery
+ include API::V3::Utilities::PathHelper
+
def initialize(base_uri:, token:, retry_proc:)
super()
@uri = base_uri
@token = token
@retry_proc = retry_proc
- @base_path = File.join(@uri.path, "remote.php/dav/files", escape_whitespace(token.origin_user_id))
+ @base_path = api_v3_paths.join_uri_path(@uri.path, "remote.php/dav/files", escape_whitespace(token.origin_user_id))
end
def query(parent)
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 3e1f0cdfccd..dbfb0e80168 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,6 +28,7 @@
module Storages::Peripherals::StorageInteraction::Nextcloud
class UploadLinkQuery < Storages::Peripherals::StorageInteraction::StorageQuery
+ include API::V3::Utilities::PathHelper
using Storages::Peripherals::ServiceResultRefinements # use '>>' (bind) operator for ServiceResult
URI_TOKEN_REQUEST = 'apps/integration_openproject/direct-upload-token'.freeze
@@ -68,7 +69,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
end
def build_upload_link(response)
- destination = URI.parse(File.join(@base_uri.to_s, URI_UPLOAD_BASE_PATH, response.token))
+ destination = URI.parse(api_v3_paths.join_uri_path(@base_uri, URI_UPLOAD_BASE_PATH, response.token))
ServiceResult.success(result: Storages::UploadLink.new(destination))
end
@@ -79,7 +80,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
response = ServiceResult.success(
result: RestClient::Request.execute(
method:,
- url: File.join(@base_uri.to_s, relative_path),
+ url: api_v3_paths.join_uri_path(@base_uri, relative_path),
payload: payload.to_json,
headers: {
'Authorization' => "Bearer #{token.access_token}",
diff --git a/modules/storages/app/views/storages/admin/storages/new_oauth_client.html.erb b/modules/storages/app/views/storages/admin/storages/new_oauth_client.html.erb
index 72a7fd4a94c..1268e41e730 100644
--- a/modules/storages/app/views/storages/admin/storages/new_oauth_client.html.erb
+++ b/modules/storages/app/views/storages/admin/storages/new_oauth_client.html.erb
@@ -17,7 +17,7 @@
<span class="form--field-instructions">
<%= "#{t("storages.instructions.copy_from")}: " %>
<%= link_to "#{t("storages.instructions.#{@storage.provider_type}.integration")}",
- URI::join(@storage.host, "settings/admin/openproject").to_s,
+ File.join(@storage.host, "settings/admin/openproject").to_s,
target: "_blank" %>
</span>
</div>
@@ -30,7 +30,7 @@
<span class="form--field-instructions">
<%= "#{t("storages.instructions.copy_from")}: " %>
<%= link_to "#{t("storages.instructions.#{@storage.provider_type}.integration")}",
- URI::join(@storage.host, "settings/admin/openproject").to_s,
+ File.join(@storage.host, "settings/admin/openproject").to_s,
target: "blank" %>
</span>
</div>
diff --git a/modules/storages/spec/common/peripherals/storage_requests_spec.rb b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
index 71024a547ad..7a454bff6f6 100644
--- a/modules/storages/spec/common/peripherals/storage_requests_spec.rb
+++ b/modules/storages/spec/common/peripherals/storage_requests_spec.rb
@@ -74,7 +74,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
end
let(:download_token) { "8dM3dC9iy1N74F5AJ0ClnjSF4dWTxfymVy1HTXBh8rbZVM81CpcBJaIYZvmR" }
let(:uri) do
- URI::join(url, "/index.php/apps/integration_openproject/direct/#{download_token}/#{CGI.escape(file_link.origin_name)}")
+ "#{url}/index.php/apps/integration_openproject/direct/#{download_token}/#{CGI.escape(file_link.origin_name)}"
end
let(:json) do
{
@@ -112,6 +112,25 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
end
)
end
+
+ context 'if Nextcloud is running on a sub path' do
+ let(:url) { 'https://example.com/html' }
+
+ it 'must return a download link URL' do
+ subject
+ .download_link_query(user:)
+ .match(
+ on_success: ->(query) do
+ result = query.call(file_link)
+ expect(result).to be_success
+ expect(result.result).to be_eql(uri)
+ end,
+ on_failure: ->(error) do
+ raise "Files query could not be created: #{error}"
+ end
+ )
+ end
+ end
end
describe 'with not supported storage type selected' do
@@ -513,7 +532,7 @@ describe Storages::Peripherals::StorageRequests, webmock: true do
end
let(:uri) do
- URI::join(url, "/public.php/webdav/#{query_payload[:fileName]}")
+ "#{url}/public.php/webdav/#{query_payload[:fileName]}"
end
let(:share_id) { 37 }