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 backend code

Eric Schubertb87bcbc87ddb
diff --git a/frontend/src/app/shared/components/file-links/file-link-list-item/file-link-list-item.component.ts b/frontend/src/app/shared/components/file-links/file-link-list-item/file-link-list-item.component.ts
index a9cbea98fa4..e7455a68acc 100644
--- a/frontend/src/app/shared/components/file-links/file-link-list-item/file-link-list-item.component.ts
+++ b/frontend/src/app/shared/components/file-links/file-link-list-item/file-link-list-item.component.ts
@@ -37,15 +37,15 @@ import {
Output,
ViewChild,
} from '@angular/core';
+
+import { I18nService } from 'core-app/core/i18n/i18n.service';
+import { TimezoneService } from 'core-app/core/datetime/timezone.service';
+import { IFileIcon } from 'core-app/shared/components/file-links/file-link-icons/icon-mappings';
import { IFileLink, IFileLinkOriginData } from 'core-app/core/state/file-links/file-link.model';
+import { PrincipalRendererService } from 'core-app/shared/components/principal/principal-renderer.service';
import {
getIconForMimeType,
} from 'core-app/shared/components/file-links/file-link-icons/file-link-list-item-icon.factory';
-import { TimezoneService } from 'core-app/core/datetime/timezone.service';
-import { I18nService } from 'core-app/core/i18n/i18n.service';
-import { PrincipalRendererService } from 'core-app/shared/components/principal/principal-renderer.service';
-import { IFileIcon } from 'core-app/shared/components/file-links/file-link-icons/icon-mappings';
-import { HttpClient } from '@angular/common/http';
@Component({
// eslint-disable-next-line @angular-eslint/component-selector
@@ -80,7 +80,6 @@ export class FileLinkListItemComponent implements OnInit, AfterViewInit {
constructor(
private readonly i18n:I18nService,
private readonly timezoneService:TimezoneService,
- private readonly http:HttpClient,
private readonly principalRendererService:PrincipalRendererService,
) {}
@@ -108,15 +107,4 @@ export class FileLinkListItemComponent implements OnInit, AfterViewInit {
);
}
}
-
- public downloadFileLink():void {
- this.http
- .get('http://host.docker.internal/remote.php/direct/Q24lfGHNVY0NFysEVtSW6hyYFy4tAPjB8up3Wcuc0mT5FuM81fwfiNHY3bEm')
- .subscribe((value) => {
- console.log(value);
- }, (error) => {
- console.log('ERROR');
- console.log(error);
- });
- }
}
diff --git a/frontend/src/app/shared/components/file-links/file-link-list-item/file-link-list-item.html b/frontend/src/app/shared/components/file-links/file-link-list-item/file-link-list-item.html
index d633d7d2ac9..54aaac30dd4 100644
--- a/frontend/src/app/shared/components/file-links/file-link-list-item/file-link-list-item.html
+++ b/frontend/src/app/shared/components/file-links/file-link-list-item/file-link-list-item.html
@@ -26,21 +26,13 @@
></div>
</a>
<div class="spot-list--item-floating-actions op-files-tab--file-list-item-floating-actions">
-<!-- <a-->
-<!-- class="spot-link"-->
-<!-- [title]="text.title.downloadFileLink"-->
-<!-- [href]="'https://nextcloud.ripley.minifox.fr/remote.php/direct/s3EbjrmfAS4wFTmPhksdbMN2ZwBv3Ehu2drrUbUvBryLdZQAr1eCwjMlaLdJ'"-->
-<!-- [download]="fileLink.originData.name"-->
-<!-- >-->
-<!-- <span class="spot-icon spot-icon_download"></span>-->
-<!-- </a>-->
- <button
+ <a
class="spot-link"
[title]="text.title.downloadFileLink"
- (click)="downloadFileLink()"
+ [href]="fileLink._links.staticOriginDownload.href"
>
<span class="spot-icon spot-icon_download"></span>
- </button>
+ </a>
<a
class="spot-link"
[title]="text.title.openFileLocation"
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 2e5cbb4b190..6f000e7a96b 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,49 +30,15 @@ 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
+ helpers API::V3::FileLinks::StorageUrlHelper
resources :download do
get do
- oauth_client_information = @file_link.storage.oauth_client
-
- token_result = ::OAuthClients::ConnectionManager
- .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: oauth_client_information)
- .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 = parse_response_body response.result.body
- # download_url = 'https://nextcloud.ripley.minifox.fr/remote.php/direct/s3EbjrmfAS4wFTmPhksdbMN2ZwBv3Ehu2drrUbUvBryLdZQAr1eCwjMlaLdJ'
+ download_url = make_download_url file_link: @file_link, user: User.current
- 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
+ raise API::Errors::InternalError.new(download_url.result) if download_url.failure?
- redirect download_url, body: "The requested resource can be downloaded from #{download_url}"
+ redirect download_url.result, body: "The requested resource can be downloaded from #{download_url.result}"
status 303
end
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 b4f88e6d328..4536563facf 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
@@ -33,4 +33,63 @@ module API::V3::FileLinks::StorageUrlHelper
"#{file_link.storage.host}/f/#{file_link.origin_id}?openfile=#{location_flag}"
end
+
+ # rubocop:disable Metrics/AbcSize
+ def make_download_url(file_link:, user:)
+ storage = file_link.storage
+ oauth_client = storage.oauth_client
+
+ client_token = ::OAuthClients::ConnectionManager
+ .new(user:, oauth_client:)
+ .get_access_token
+
+ return ServiceResult.failure(result: I18n.t('http.request.missing_authorization')) if client_token.failure?
+
+ response = make_direct_download oauth_client:,
+ access_token: client_token.result.access_token,
+ file_id: file_link.origin_id
+
+ return response if response.failure?
+
+ token = parse_direct_download_token body: response.result
+ if token.blank?
+ Rails.logger.error "Received unexpected json response: #{response.result.body}"
+ return ServiceResult.failure(result: I18n.t('http.response.unexpected'))
+ end
+
+ "#{storage.host}/apps/integration_openproject/direct?token=#{token}&fileName=#{file_link.origin_name}"
+ end
+ # rubocop:enable Metrics/AbcSize
+
+ private
+
+ def make_direct_download(oauth_client:, access_token:, file_id:)
+ response = API::V3::Storages::StorageRequestFactory
+ .new(oauth_client:)
+ .download_command
+ .call(access_token:, file_id:)
+
+ return response if response.failure?
+
+ # The nextcloud API returns a successful response with empty body if the authorization is missing or expired
+ return ServiceResult.failure(result: I18n.t('http.request.failed_authorization')) if response.result.body.blank?
+
+ ServiceResult.success(result: response.result.body)
+ end
+
+ def parse_direct_download_token(body:)
+ begin
+ json = JSON.parse body
+ rescue JSON::ParserError
+ return nil
+ end
+
+ direct_download_url = json.dig('ocs', 'data', 'url')
+ return nil if direct_download_url.blank?
+
+ path = URI.parse(direct_download_url).path
+ return nil if path.blank?
+
+ path.split('/').last
+ end
end