From 06a75b9261fd236c5ce02cc4c6fcbadba245f6ae Mon Sep 17 00:00:00 2001 From: Eric Schubert Date: Wed, 27 Apr 2022 16:31:17 +0200 Subject: [PATCH 1/3] [#42161] added new open file location link to api v3 --- .../api/v3/file_links/file_link_representer.rb | 12 ++++++++++++ .../lib/api/v3/file_links/file_links_open_api.rb | 2 +- .../lib/api/v3/file_links/storage_url_helper.rb | 6 ++++-- .../storages/lib/open_project/storages/engine.rb | 4 ++-- .../file_link_representer_rendering_spec.rb | 16 +++++++++++++++- .../api/v3/file_links/file_links_spec.rb | 10 ++++++++-- 6 files changed, 42 insertions(+), 8 deletions(-) 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 c7c07e2ae5..7a2b89e6ff 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 @@ -78,6 +78,18 @@ module API } end + link :originOpenLocation do + { + href: storage_url_open(represented, true) + } + end + + link :staticOriginOpenLocation do + { + href: api_v3_paths.file_link_open(represented.id, true) + } + end + associated_resource :storage associated_resource :storageUrl, 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 a6d1c7d9a4..68ed37fa95 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 @@ -34,7 +34,7 @@ module API resources :open do get do - url = storage_url_open(@file_link) + url = storage_url_open(@file_link, params[:location]) 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/lib/api/v3/file_links/storage_url_helper.rb b/modules/storages/lib/api/v3/file_links/storage_url_helper.rb index c001ffba47..b4f88e6d32 100644 --- a/modules/storages/lib/api/v3/file_links/storage_url_helper.rb +++ b/modules/storages/lib/api/v3/file_links/storage_url_helper.rb @@ -28,7 +28,9 @@ # 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}" + def storage_url_open(file_link, open_location: false) + location_flag = ActiveModel::Type::Boolean.new.cast(open_location) ? 0 : 1 + + "#{file_link.storage.host}/f/#{file_link.origin_id}?openfile=#{location_flag}" end end diff --git a/modules/storages/lib/open_project/storages/engine.rb b/modules/storages/lib/open_project/storages/engine.rb index 5089130e40..fcc2bdd4b2 100644 --- a/modules/storages/lib/open_project/storages/engine.rb +++ b/modules/storages/lib/open_project/storages/engine.rb @@ -120,8 +120,8 @@ module OpenProject::Storages "#{root}/file_links/#{file_link_id}/download" end - add_api_path :file_link_open do |file_link_id| - "#{root}/file_links/#{file_link_id}/open" + add_api_path :file_link_open do |file_link_id, location = false| + "#{root}/file_links/#{file_link_id}/open#{location ? '?location=true' : ''}" end # Add api endpoints specific to this module 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 4206f669c7..44fd6331d2 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 @@ -86,7 +86,7 @@ describe ::API::V3::FileLinks::FileLinkRepresenter, 'rendering' do describe 'originOpen' do it_behaves_like 'has an untitled link' do let(:link) { 'originOpen' } - let(:href) { "#{storage.host}/f/#{file_link.origin_id}" } + let(:href) { "#{storage.host}/f/#{file_link.origin_id}?openfile=1" } end end @@ -96,6 +96,20 @@ describe ::API::V3::FileLinks::FileLinkRepresenter, 'rendering' do let(:href) { "/api/v3/file_links/#{file_link.id}/open" } end end + + describe 'originOpenLocation' do + it_behaves_like 'has an untitled link' do + let(:link) { 'originOpenLocation' } + let(:href) { "#{storage.host}/f/#{file_link.origin_id}?openfile=0" } + end + end + + describe 'staticOriginOpenLocation' do + it_behaves_like 'has an untitled link' do + let(:link) { 'staticOriginOpenLocation' } + let(:href) { "/api/v3/file_links/#{file_link.id}/open?location=true" } + end + end end describe 'properties' do diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb index 4e2a1bc136..460c9749e7 100644 --- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb +++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb @@ -29,7 +29,6 @@ require 'spec_helper' require_module_spec_helper -# rubocop:disable RSpec/MultipleMemoizedHelpers describe 'API v3 file links resource', :enable_storages, type: :request do include API::V3::Utilities::PathHelper @@ -408,6 +407,14 @@ describe 'API v3 file links resource', :enable_storages, type: :request do expect(subject.status).to be 303 end + context 'with location flag' do + let(:path) { api_v3_paths.file_link_open(file_link.id, true) } + + it 'is successful' do + expect(subject.status).to be 303 + end + end + context 'if user has no view permissions' do let(:permissions) { [] } @@ -425,4 +432,3 @@ describe 'API v3 file links resource', :enable_storages, type: :request do end end end -# rubocop:enable RSpec/MultipleMemoizedHelpers From 1f5caed4a4a99b70a206054b2c33161a160d028a Mon Sep 17 00:00:00 2001 From: Eric Schubert Date: Thu, 28 Apr 2022 14:11:33 +0200 Subject: [PATCH 2/3] [#42161] fixed method call --- modules/storages/lib/api/v3/file_links/file_link_representer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7a2b89e6ff..8f46188e76 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 @@ -80,7 +80,7 @@ module API link :originOpenLocation do { - href: storage_url_open(represented, true) + href: storage_url_open(represented, open_location: true) } end From 97a3f691669e33b779bf380b6c91cc1ddda4c74a Mon Sep 17 00:00:00 2001 From: Eric Schubert Date: Thu, 28 Apr 2022 14:43:15 +0200 Subject: [PATCH 3/3] [#42161] fixed method call --- modules/storages/lib/api/v3/file_links/file_links_open_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 68ed37fa95..d3a1ef1177 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 @@ -34,7 +34,7 @@ module API resources :open do get do - url = storage_url_open(@file_link, params[:location]) + url = storage_url_open(@file_link, open_location: params[:location]) redirect url, body: "The requested resource can be viewed at #{url}" status 303 # The follow-up request to the resource must be GET end