11import { Component , Input , OnInit } from '@angular/core' ;
22import { Bitstream } from '../../core/shared/bitstream.model' ;
3- import { BehaviorSubject , Observable , of as observableOf } from 'rxjs' ;
4- import { finalize , switchMap , take } from 'rxjs/operators' ;
3+ import { BehaviorSubject , firstValueFrom , Observable , of as observableOf } from 'rxjs' ;
4+ import { filter , finalize , switchMap , take } from 'rxjs/operators' ;
55import { followLink } from '../../shared/utils/follow-link-config.model' ;
66import { ClarinUserRegistration } from '../../core/shared/clarin/clarin-user-registration.model' ;
77import { ClarinUserMetadata } from '../../core/shared/clarin/clarin-user-metadata.model' ;
@@ -35,13 +35,13 @@ import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-bu
3535import { HttpOptions } from '../../core/dspace-rest/dspace-rest.service' ;
3636import { Router } from '@angular/router' ;
3737import { getItemPageRoute } from '../../item-page/item-page-routing-paths' ;
38- import { getBitstreamContentRoute } from '../../app-routing-paths' ;
3938import { hasFailed } from 'src/app/core/data/request-entry-state.model' ;
4039import { FindListOptions } from '../../core/data/find-list-options.model' ;
4140import isEqual from 'lodash/isEqual' ;
4241import cloneDeep from 'lodash/cloneDeep' ;
4342import { ClarinUserMetadataDataService } from '../../core/data/clarin/clarin-user-metadata.service' ;
4443import { HtmlContentService } from '../../shared/html-content.service' ;
44+ import { FileService } from '../../core/shared/file.service' ;
4545
4646/**
4747 * The component shows the user's filled in user metadata and the user can fill in other required user metadata.
@@ -143,7 +143,9 @@ export class ClarinLicenseAgreementPageComponent implements OnInit {
143143 private hardRedirectService : HardRedirectService ,
144144 private requestService : RequestService ,
145145 private clarinUserMetadataDataService : ClarinUserMetadataDataService ,
146- private htmlContentService : HtmlContentService ) { }
146+ private htmlContentService : HtmlContentService ,
147+ protected fileService : FileService ,
148+ protected notificationsService : NotificationsService ) { }
147149
148150 ngOnInit ( ) : void {
149151 // Load CurrentItem by bitstreamID to show itemHandle
@@ -242,7 +244,7 @@ export class ClarinLicenseAgreementPageComponent implements OnInit {
242244 } else {
243245 // Or just download the bitstream by download token
244246 const downloadToken = Object . values ( responseRD$ ?. payload ) . join ( '' ) ;
245- this . redirectToDownload ( downloadToken ) ;
247+ void this . redirectToDownload ( downloadToken ) ;
246248 }
247249 } ) ;
248250 }
@@ -255,23 +257,33 @@ export class ClarinLicenseAgreementPageComponent implements OnInit {
255257 return this . router . routerState . snapshot . url . endsWith ( '/zip' ) ;
256258 }
257259
258- private redirectToDownload ( downloadToken = null ) {
259- // 1. Get bitstream
260- // 2. Get bitstream download link
261- // 3. Get bitstream content download link and check if there is `authorization-token` in to query params
262- let bitstream = null ;
263- this . bitstream$
264- . pipe ( take ( 1 ) )
265- . subscribe ( bitstream$ => {
266- bitstream = bitstream$ ;
267- } ) ;
268- let bitstreamDownloadPath = getBitstreamContentRoute ( bitstream ) ;
269- if ( isNotEmpty ( downloadToken ) ) {
270- bitstreamDownloadPath = this . halService . getRootHref ( ) + '/core' + bitstreamDownloadPath +
271- '?dtoken=' + downloadToken ;
260+ /**
261+ * Redirects to the download link of the bitstream.
262+ * If a download token is provided, it appends it as a query parameter.
263+ *
264+ * @param downloadToken
265+ * @private
266+ */
267+ private async redirectToDownload ( downloadToken ?: string ) : Promise < void > {
268+ try {
269+ const bitstream = await firstValueFrom ( this . bitstream$ . pipe ( take ( 1 ) ) ) ;
270+
271+ const fileLink = await firstValueFrom (
272+ this . fileService . retrieveFileDownloadLink ( bitstream . _links . content . href ) . pipe (
273+ filter ( hasValue ) ,
274+ take ( 1 )
275+ )
276+ ) ;
277+
278+ // Determine whether the URL already contains query parameters
279+ const hasQueryParams = fileLink . includes ( '?' ) ;
280+ const tokenParam = downloadToken ? `${ hasQueryParams ? '&' : '?' } dtoken=${ downloadToken } ` : '' ;
281+
282+ const redirectUrl = `${ fileLink } ${ tokenParam } ` ;
283+ this . hardRedirectService . redirect ( redirectUrl ) ;
284+ } catch ( error ) {
285+ this . notificationsService . error ( this . translateService . instant ( 'clarin-license-agreement-page.download-error' ) ) ;
272286 }
273-
274- this . hardRedirectService . redirect ( bitstreamDownloadPath ) ;
275287 }
276288
277289 public getMetadataValueByKey ( metadataKey : string ) {
0 commit comments