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

[#42160] added download link to representer

Eric Schubert13eb9c894d02
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 2cb140988ef..dae3afee3bc 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -950,7 +950,6 @@ en:
password: "Password"
priority: "Priority"
project: "Project"
- public: "Public"
responsible: "Accountable"
role: "Role"
roles: "Roles"
diff --git a/docs/api/apiv3/components/schemas/file_link_read_model.yml b/docs/api/apiv3/components/schemas/file_link_read_model.yml
index dc17cd0f9a0..bcf06bb1d80 100644
--- a/docs/api/apiv3/components/schemas/file_link_read_model.yml
+++ b/docs/api/apiv3/components/schemas/file_link_read_model.yml
@@ -46,6 +46,7 @@ properties:
- staticOriginOpen
- originOpenLocation
- staticOriginOpenLocation
+ - staticOriginDownload
properties:
self:
allOf:
@@ -121,13 +122,6 @@ properties:
- description: |-
A static uri to open the location of the origin file on the storage. Responds with a redirect.
- **Resource**: N/A
- originDownload:
- allOf:
- - $ref: './link.yml'
- - description: |-
- The uri to download the origin file from the origin.
-
**Resource**: N/A
staticOriginDownload:
allOf:
@@ -213,7 +207,5 @@ example:
href: https://nextcloud.deathstar.rocks/index.php/f/5503?openfile=0
staticOriginOpenLocation:
href: /api/v3/file_links/1337/open?location=true
- originDownload:
- href: https://nextcloud.deathstar.rocks/index.php/f/5503?openfile=0
staticOriginDownload:
href: /api/v3/file_links/1337/download
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 385f5ccfd97..c40d0abd2c8 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
@@ -101,6 +101,12 @@ module API
}
end
+ link :staticOriginDownload do
+ {
+ href: api_v3_paths.file_link_download(represented.id)
+ }
+ end
+
associated_resource :storage
associated_resource :storageUrl,
diff --git a/modules/storages/lib/api/v3/file_links/file_links_download_api.rb b/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
index 5e6369e2c1b..fc7a0ca04be 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_download_api.rb
@@ -30,16 +30,30 @@ module API
module V3
module FileLinks
class FileLinksDownloadAPI < ::API::OpenProjectAPI
+ helpers do
+ def parse_response_body(body)
+ begin
+ json = JSON.parse body
+ rescue JSON::ParserError
+ return nil
+ end
+
+ json.dig('ocs', 'data', 'url')
+ end
+ end
+
resources :download do
get do
+ oauth_client_information = @file_link.storage.oauth_client
+
token_result = ::OAuthClients::ConnectionManager
- .new(user: User.current, oauth_client: @file_link.storage.oauth_client)
+ .new(user: User.current, oauth_client: oauth_client_information)
.get_access_token
raise API::Errors::InternalError.new(I18n.t('http.request.missing_authorization')) if token_result.failure?
response = API::V3::Storages::StorageRequestFactory
- .new(oauth_client:)
+ .new(oauth_client: oauth_client_information)
.download_command
.call(
access_token: token_result.result.access_token,
@@ -50,7 +64,7 @@ module API
# The nextcloud API returns a successful response with empty body if the authorization is missing or expired
raise API::Errors::InternalError.new(I18n.t('http.request.failed_authorization')) if response.result.body.blank?
- download_url = JSON.parse(response.result.body).dig('ocs', 'data', 'url')
+ download_url = parse_response_body response.result.body
if download_url.blank?
Rails.logger.error "Received unexpected json response: #{response.result.body}"
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 5b8a43a8a46..d9dd6a752e6 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
@@ -118,6 +118,13 @@ describe ::API::V3::FileLinks::FileLinkRepresenter, 'rendering' do
let(:href) { "/api/v3/file_links/#{file_link.id}/open?location=true" }
end
end
+
+ describe 'staticOriginDownload' do
+ it_behaves_like 'has an untitled link' do
+ let(:link) { 'staticOriginDownload' }
+ let(:href) { "/api/v3/file_links/#{file_link.id}/download" }
+ 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 b37898731c0..0bfe32a1bb2 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
@@ -459,4 +459,64 @@ describe 'API v3 file links resource', with_flag: { storages_module_active: true
it_behaves_like 'not found'
end
end
+
+ describe 'GET /api/v3/file_links/:file_link_id/download' do
+ let(:connection_manager) { instance_double(::OAuthClients::ConnectionManager) }
+ let(:request_factory) { instance_double(::API::V3::Storages::StorageRequestFactory) }
+ let(:access_token_result) { ServiceResult.success(result: create(:oauth_client_token)) }
+ let(:path) { api_v3_paths.file_link_download(file_link.id) }
+ let(:download_response) { instance_double(RestClient::Response) }
+ let(:download_command_result) { ServiceResult.success(result: download_response) }
+ let(:response_body) { { ocs: { data: { url: 'https://starkiller.nextcloud.com/direct/xyz' } } }.to_json }
+
+ before do
+ allow(connection_manager).to receive(:get_access_token).and_return(access_token_result)
+ allow(::OAuthClients::ConnectionManager).to receive(:new).and_return(connection_manager)
+ allow(request_factory).to receive(:download_command).and_return(->(*) { download_command_result })
+ allow(::API::V3::Storages::StorageRequestFactory).to receive(:new).and_return(request_factory)
+ allow(download_response).to receive(:body).and_return(response_body)
+
+ get path
+ end
+
+ it 'is successful' do
+ expect(subject.status).to be 303
+ end
+
+ context 'if storage origin returns unexpected response body' do
+ let(:response_body) { 'the force is not strong in this one ...' }
+
+ it_behaves_like 'error response',
+ 500,
+ 'InternalServerError',
+ I18n.t('http.response.unexpected')
+ end
+
+ context 'if storage origin returns empty response body' do
+ let(:response_body) { '' }
+
+ it_behaves_like 'error response',
+ 500,
+ 'InternalServerError',
+ I18n.t('http.request.failed_authorization')
+ end
+
+ context 'if no access token is available' do
+ let(:access_token_result) { ServiceResult.failure }
+
+ it_behaves_like 'error response',
+ 500,
+ 'InternalServerError',
+ I18n.t('http.request.missing_authorization')
+ end
+
+ context 'if outbound request returns with failure (token expired or revoked)' do
+ let(:download_command_result) { ServiceResult.failure(result: I18n.t('http.request.failed_authorization')) }
+
+ it_behaves_like 'error response',
+ 500,
+ 'InternalServerError',
+ I18n.t('http.request.failed_authorization')
+ end
+ end
end