file-download-link.component.ts (dspace-angular-dspace-7.0) | : | file-download-link.component.ts (dspace-angular-dspace-7.1) | ||
---|---|---|---|---|
import { Component, Input, OnInit } from '@angular/core'; | import { Component, Input, OnInit } from '@angular/core'; | |||
import { Bitstream } from '../../core/shared/bitstream.model'; | import { Bitstream } from '../../core/shared/bitstream.model'; | |||
import { getBitstreamDownloadRoute } from '../../app-routing-paths'; | import { getBitstreamDownloadRoute, getBitstreamRequestACopyRoute } from '../../ | |||
app-routing-paths'; | ||||
import { AuthorizationDataService } from '../../core/data/feature-authorization/ | ||||
authorization-data.service'; | ||||
import { FeatureID } from '../../core/data/feature-authorization/feature-id'; | ||||
import { hasValue, isNotEmpty } from '../empty.util'; | ||||
import { map } from 'rxjs/operators'; | ||||
import { of as observableOf, combineLatest as observableCombineLatest, Observabl | ||||
e } from 'rxjs'; | ||||
import { Item } from '../../core/shared/item.model'; | ||||
@Component({ | @Component({ | |||
selector: 'ds-file-download-link', | selector: 'ds-file-download-link', | |||
templateUrl: './file-download-link.component.html', | templateUrl: './file-download-link.component.html', | |||
styleUrls: ['./file-download-link.component.scss'] | styleUrls: ['./file-download-link.component.scss'] | |||
}) | }) | |||
/** | /** | |||
* Component displaying a download link | * Component displaying a download link | |||
* When the user is authenticated, a short-lived token retrieved from the REST A PI is added to the download link, | * When the user is authenticated, a short-lived token retrieved from the REST A PI is added to the download link, | |||
* ensuring the user is authorized to download the file. | * ensuring the user is authorized to download the file. | |||
*/ | */ | |||
export class FileDownloadLinkComponent implements OnInit { | export class FileDownloadLinkComponent implements OnInit { | |||
/** | /** | |||
* Optional bitstream instead of href and file name | * Optional bitstream instead of href and file name | |||
*/ | */ | |||
@Input() bitstream: Bitstream; | @Input() bitstream: Bitstream; | |||
@Input() item: Item; | ||||
/** | /** | |||
* Additional css classes to apply to link | * Additional css classes to apply to link | |||
*/ | */ | |||
@Input() cssClasses = ''; | @Input() cssClasses = ''; | |||
/** | /** | |||
* A boolean representing if link is shown in same tab or in a new one. | * A boolean representing if link is shown in same tab or in a new one. | |||
*/ | */ | |||
@Input() isBlank = false; | @Input() isBlank = false; | |||
bitstreamPath: string; | @Input() enableRequestACopy = true; | |||
bitstreamPath$: Observable<{ | ||||
routerLink: string, | ||||
queryParams: any, | ||||
}>; | ||||
canDownload$: Observable<boolean>; | ||||
constructor( | ||||
private authorizationService: AuthorizationDataService, | ||||
) { | ||||
} | ||||
ngOnInit() { | ngOnInit() { | |||
this.bitstreamPath = this.getBitstreamPath(); | if (this.enableRequestACopy) { | |||
this.canDownload$ = this.authorizationService.isAuthorized(FeatureID.CanDo | ||||
wnload, isNotEmpty(this.bitstream) ? this.bitstream.self : undefined); | ||||
const canRequestACopy$ = this.authorizationService.isAuthorized(FeatureID. | ||||
CanRequestACopy, isNotEmpty(this.bitstream) ? this.bitstream.self : undefined); | ||||
this.bitstreamPath$ = observableCombineLatest([this.canDownload$, canReque | ||||
stACopy$]).pipe( | ||||
map(([canDownload, canRequestACopy]) => this.getBitstreamPath(canDownloa | ||||
d, canRequestACopy)) | ||||
); | ||||
} else { | ||||
this.bitstreamPath$ = observableOf(this.getBitstreamDownloadPath()); | ||||
this.canDownload$ = observableOf(true); | ||||
} | ||||
} | ||||
getBitstreamPath(canDownload: boolean, canRequestACopy: boolean) { | ||||
if (!canDownload && canRequestACopy && hasValue(this.item)) { | ||||
return getBitstreamRequestACopyRoute(this.item, this.bitstream); | ||||
} | ||||
return this.getBitstreamDownloadPath(); | ||||
} | } | |||
getBitstreamPath() { | getBitstreamDownloadPath() { | |||
return getBitstreamDownloadRoute(this.bitstream); | return { | |||
routerLink: getBitstreamDownloadRoute(this.bitstream), | ||||
queryParams: {} | ||||
}; | ||||
} | } | |||
} | } | |||
End of changes. 5 change blocks. | ||||
5 lines changed or deleted | 51 lines changed or added |