@@ -36,7 +36,10 @@ import {
3636 Observable ,
3737 of ,
3838} from 'rxjs' ;
39- import { map } from 'rxjs/operators' ;
39+ import {
40+ map ,
41+ switchMap ,
42+ } from 'rxjs/operators' ;
4043
4144import { ThemedAccessStatusBadgeComponent } from '../object-collection/shared/badges/access-status-badge/themed-access-status-badge.component' ;
4245
@@ -112,13 +115,23 @@ export class FileDownloadLinkComponent implements OnInit {
112115 if ( this . enableRequestACopy ) {
113116 // Obtain item request data from the route snapshot
114117 this . itemRequest = this . route . snapshot . data . itemRequest ;
115- // Set up observables to test access rights to a normal bitstream download, a valid token download, and the request-a-copy feature
118+ // Set up observable to evaluate access rights for a normal download
116119 this . canDownload$ = this . authorizationService . isAuthorized ( FeatureID . CanDownload , isNotEmpty ( this . bitstream ) ? this . bitstream . self : undefined ) ;
117- this . canDownloadWithToken$ = of ( ( this . itemRequest && this . itemRequest . acceptRequest && ! this . itemRequest . accessExpired ) ? ( this . itemRequest . allfiles !== false || this . itemRequest . bitstreamId === this . bitstream . uuid ) : false ) ;
118- this . canRequestACopy$ = this . authorizationService . isAuthorized ( FeatureID . CanRequestACopy , isNotEmpty ( this . bitstream ) ? this . bitstream . self : undefined ) ;
119- // Set up observable to determine the path to the bitstream based on the user's access rights and features as above
120- this . bitstreamPath$ = observableCombineLatest ( [ this . canDownload$ , this . canDownloadWithToken$ , this . canRequestACopy$ ] ) . pipe (
121- map ( ( [ canDownload , canDownloadWithToken , canRequestACopy ] ) => this . getBitstreamPath ( canDownload , canDownloadWithToken , canRequestACopy ) ) ,
120+ // Only set up and execute other observables if canDownload emits false
121+ this . bitstreamPath$ = this . canDownload$ . pipe (
122+ switchMap ( canDownload => {
123+ if ( canDownload ) {
124+ return of ( this . getBitstreamDownloadPath ( ) ) ;
125+ }
126+ // Set up and combine observables to evaluate access rights to a valid token download and the request-a-copy feature
127+ this . canDownloadWithToken$ = of ( ( this . itemRequest && this . itemRequest . acceptRequest && ! this . itemRequest . accessExpired ) ? ( this . itemRequest . allfiles !== false || this . itemRequest . bitstreamId === this . bitstream . uuid ) : false ) ;
128+ this . canRequestACopy$ = this . authorizationService . isAuthorized ( FeatureID . CanRequestACopy , isNotEmpty ( this . bitstream ) ? this . bitstream . self : undefined ) ;
129+ // Set up canDownload observable so the template can read the state
130+ this . canDownload$ = of ( false ) ;
131+ return observableCombineLatest ( [ this . canDownloadWithToken$ , this . canRequestACopy$ ] ) . pipe (
132+ map ( ( [ canDownloadWithToken , canRequestACopy ] ) => this . getBitstreamPathForRequestACopy ( canDownloadWithToken , canRequestACopy ) ) ,
133+ ) ;
134+ } ) ,
122135 ) ;
123136 } else {
124137 this . bitstreamPath$ = of ( this . getBitstreamDownloadPath ( ) ) ;
@@ -130,21 +143,16 @@ export class FileDownloadLinkComponent implements OnInit {
130143 * Return a path to the bitstream based on what kind of access and authorization the user has, and whether
131144 * they may request a copy
132145 *
133- * @param canDownload user can download normally
134146 * @param canDownloadWithToken user can download using a token granted by a request approver
135147 * @param canRequestACopy user can request approval to access a copy
136148 */
137- getBitstreamPath ( canDownload : boolean , canDownloadWithToken , canRequestACopy : boolean ) {
138- // No matter what, if the user can download with their own authZ, allow it
139- if ( canDownload ) {
140- return this . getBitstreamDownloadPath ( ) ;
141- }
142- // Otherwise, if they access token is valid, use this
149+ getBitstreamPathForRequestACopy ( canDownloadWithToken : boolean , canRequestACopy : boolean ) {
150+ // if the access token is valid, use this
143151 if ( canDownloadWithToken ) {
144152 return this . getAccessByTokenBitstreamPath ( this . itemRequest ) ;
145153 }
146154 // If the user can't download, but can request a copy, show the request a copy link
147- if ( ! canDownload && canRequestACopy && hasValue ( this . item ) ) {
155+ if ( canRequestACopy && hasValue ( this . item ) ) {
148156 return getBitstreamRequestACopyRoute ( this . item , this . bitstream ) ;
149157 }
150158 // By default, return the plain path
0 commit comments