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 endpoint

Eric Schubert61de9b6993dc
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 20b7953f534..2cb140988ef 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -314,9 +314,9 @@ en:
that person access to the project's information but still want track tasks assigned to that person.
prioritiies:
edit:
- priority_color_text: |
- Click to assign or change the color of this priority.
- It can be used for highlighting work packages in the table.
+ priority_color_text: |
+ Click to assign or change the color of this priority.
+ It can be used for highlighting work packages in the table.
reportings:
index:
@@ -325,14 +325,14 @@ en:
statuses:
edit:
- status_readonly_html: |
- Check this option to mark work packages with this status as read-only.
- No attributes can be changed with the exception of the status.
- <br/>
- <strong>Note</strong>: Inherited values (e.g., from children or relations) will still apply.
- status_color_text: |
- Click to assign or change the color of this status.
- It is shown in the status button and can be used for highlighting work packages in the table.
+ status_readonly_html: |
+ Check this option to mark work packages with this status as read-only.
+ No attributes can be changed with the exception of the status.
+ <br/>
+ <strong>Note</strong>: Inherited values (e.g., from children or relations) will still apply.
+ status_color_text: |
+ Click to assign or change the color of this status.
+ It is shown in the status button and can be used for highlighting work packages in the table.
index:
no_results_title_text: There are currently no work package statuses.
no_results_content_text: Add a new status
@@ -1102,10 +1102,10 @@ en:
create_new_page: "Wiki page"
date:
- abbr_day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
- abbr_month_names: [~, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
+ abbr_day_names: [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]
+ abbr_month_names: [ ~, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ]
abbr_week: 'Wk'
- day_names: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
+ day_names: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ]
formats:
# Use the strftime parameters for formats.
# When no format has been given, it uses default.
@@ -1114,7 +1114,7 @@ en:
long: "%B %d, %Y"
short: "%b %d"
# Don't forget the nil at the beginning; there's no such thing as a 0th month
- month_names: [~, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
+ month_names: [ ~, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
# Used in date_select and datime_select.
order:
- :year
@@ -2800,7 +2800,7 @@ en:
delete_thing: "Delete"
duration: "Duration"
duration_days:
- one: "1 day"
+ one: "1 day"
other: "%{count} days"
edit_color: "Edit color"
edit_thing: "Edit"
@@ -2897,12 +2897,12 @@ en:
outline: "Reset Outline"
outlines:
aggregation: "Show aggregations only"
- level1: "Expand level 1"
- level2: "Expand level 2"
- level3: "Expand level 3"
- level4: "Expand level 4"
- level5: "Expand level 5"
- all: "Show all"
+ level1: "Expand level 1"
+ level2: "Expand level 2"
+ level3: "Expand level 3"
+ level4: "Expand level 4"
+ level5: "Expand level 5"
+ all: "Show all"
reporting_for_project:
show: "Status reported to project: %{title}"
edit_delete: "status report for project: %{title}"
@@ -3047,7 +3047,7 @@ en:
# TODO: merge with work_packages top level key
work_package:
updated_automatically_by_child_changes: |
- _Updated automatically by changing values within child work package %{child}_
+ _Updated automatically by changing values within child work package %{child}_
destroy:
info: "Deleting the work package is an irreversible action."
title: "Delete the work package"
@@ -3236,7 +3236,7 @@ en:
wrong_token_type_returned: "OAuth2 returned a wrong type of token, expecting AccessToken::Bearer"
oauth_issue_contact_admin: "OAuth2 reported an error. Please contact your system administrator."
oauth_client_not_found: "OAuth2 client not found in 'callback' endpoint (redirect_uri)."
- refresh_token_called_without_existing_token: >
+ refresh_token_called_without_existing_token: >
Internal error: Called refresh_token without a previously existing token.
refresh_token_updated_failed: "Error during update of OAuthClientToken"
oauth_client_not_found_explanation: >
@@ -3263,4 +3263,12 @@ en:
unauthorized_client: "The OAuth2 Authorization Server rejects the grant type (unauthorized_client)"
unsupported_grant_type: "The OAuth2 Authorization Server asks you to reauthorize (unsupported_grant_type)."
invalid_scope: "You are not allowed to access the requested resource (invalid_scope)."
+
+ http:
+ request:
+ failed_authorization: "The server side request failed authorizing itself."
+ missing_authorization: "The server side request failed due to missing authorization information."
+ response:
+ unexpected: "Unexpected response received."
+
you: you
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 8f2a5bb9439..5e6369e2c1b 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
@@ -32,8 +32,32 @@ module API
class FileLinksDownloadAPI < ::API::OpenProjectAPI
resources :download do
get do
- url = "https://example.org"
- redirect url, body: "The requested resource can be downloaded from #{url}"
+ token_result = ::OAuthClients::ConnectionManager
+ .new(user: User.current, oauth_client: @file_link.storage.oauth_client)
+ .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:)
+ .download_command
+ .call(
+ access_token: token_result.result.access_token,
+ file_id: @file_link.origin_id
+ )
+
+ raise API::Errors::InternalError.new(response.result) if response.failure?
+ # 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')
+
+ if download_url.blank?
+ Rails.logger.error "Received unexpected json response: #{response.result.body}"
+ raise API::Errors::InternalError.new(I18n.t('http.response.unexpected'))
+ end
+
+ redirect download_url, body: "The requested resource can be downloaded from #{download_url}"
status 303
end
end
diff --git a/modules/storages/lib/api/v3/storages/storage_request_factory.rb b/modules/storages/lib/api/v3/storages/storage_request_factory.rb
new file mode 100644
index 00000000000..c89ac883cd5
--- /dev/null
+++ b/modules/storages/lib/api/v3/storages/storage_request_factory.rb
@@ -0,0 +1,61 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module API
+ module V3
+ module Storages
+ class StorageRequestFactory
+ def initialize(oauth_client:)
+ @oauth_client = oauth_client
+ end
+
+ def download_command
+ ->(access_token:, file_id:) do
+ request_url = File.join(@oauth_client.integration.host, '/ocs/v2.php/apps/dav/api/v1/direct')
+ body = { fileId: file_id }
+ header = {
+ 'Authorization' => "Bearer #{access_token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json'
+ }
+
+ begin
+ response = RestClient.post request_url, body, header
+ rescue RestClient::Unauthorized
+ return ServiceResult.failure(result: I18n.t('http.request.failed_authorization'))
+ rescue StandardError => e
+ return ServiceResult.failure(result: e.message)
+ end
+
+ ServiceResult.success(result: response)
+ end
+ end
+ end
+ end
+ end
+end