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 helper instead of a service to get open url for file_links

Christophe Bliard9e968c4752b2
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index 7c31574c018..0ba53c49c40 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -33,6 +33,7 @@ module API
include API::Decorators::LinkedResource
include API::Decorators::DateProperty
include ::API::Caching::CachedRepresenter
+ include API::V3::FileLinks::StorageUrlHelper
property :id
@@ -67,7 +68,7 @@ module API
link :originOpen do
{
- href: ::Storages::StorageUrlService.new(represented).call('open').result
+ href: storage_url_open(represented)
}
end
diff --git a/modules/storages/lib/api/v3/file_links/file_links_open_api.rb b/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
index 158b51b07fa..a6d1c7d9a45 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_open_api.rb
@@ -30,13 +30,11 @@ module API
module V3
module FileLinks
class FileLinksOpenAPI < ::API::OpenProjectAPI
+ helpers API::V3::FileLinks::StorageUrlHelper
+
resources :open do
get do
- service_call = ::Storages::StorageUrlService.new(@file_link).call('open')
-
- raise StandardError, 'Cannot create opening url for linked file.' if service_call.failure?
-
- url = service_call.result
+ url = storage_url_open(@file_link)
redirect url, body: "The requested resource can be viewed at #{url}"
status 303 # The follow-up request to the resource must be GET
end
diff --git a/modules/storages/app/services/storages/storage_url_service.rb b/modules/storages/lib/api/v3/file_links/storage_url_helper.rb
similarity index 74%
rename from modules/storages/app/services/storages/storage_url_service.rb
rename to modules/storages/lib/api/v3/file_links/storage_url_helper.rb
index 500ffce1f90..c001ffba47d 100644
--- a/modules/storages/app/services/storages/storage_url_service.rb
+++ b/modules/storages/lib/api/v3/file_links/storage_url_helper.rb
@@ -26,20 +26,9 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module Storages
- class StorageUrlService
- attr_reader :file_link
-
- ACTION_TYPES = %w[open].freeze
-
- def initialize(file_link)
- @file_link = file_link
- end
-
- def call(action)
- return ServiceResult.new(success: false, message: 'invalid action type') unless ACTION_TYPES.include?(action)
-
- ServiceResult.new(success: true, result: "#{@file_link.storage.host}/f/#{@file_link.origin_id}")
- end
+# Helper for open and download links for a file link object.
+module API::V3::FileLinks::StorageUrlHelper
+ def storage_url_open(file_link)
+ "#{file_link.storage.host}/f/#{file_link.origin_id}"
end
end
diff --git a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb
index 6ae4fc934cc..4206f669c70 100644
--- a/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb
+++ b/modules/storages/spec/lib/api/v3/file_links/file_link_representer_rendering_spec.rb
@@ -40,10 +40,6 @@ describe ::API::V3::FileLinks::FileLinkRepresenter, 'rendering' do
subject(:generated) { representer.to_json }
- before do
- allow(::Storages::StorageUrlService).to receive(:find).and_return creator
- end
-
describe '_links' do
describe 'self' do
it_behaves_like 'has a titled link' do