Skip to content

Commit ce8cbfc

Browse files
UFAL/The editor cannot download the workflows bitstream (#924)
* Added `authorization-token` to the request, so the user could be identified in the BE context * Added notification error when downloading has failed
1 parent bbba912 commit ce8cbfc

3 files changed

Lines changed: 37 additions & 21 deletions

File tree

src/app/bitstream-page/clarin-license-agreement-page/clarin-license-agreement-page.component.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, Input, OnInit } from '@angular/core';
22
import { 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';
55
import { followLink } from '../../shared/utils/follow-link-config.model';
66
import { ClarinUserRegistration } from '../../core/shared/clarin/clarin-user-registration.model';
77
import { ClarinUserMetadata } from '../../core/shared/clarin/clarin-user-metadata.model';
@@ -35,13 +35,13 @@ import { RemoteDataBuildService } from '../../core/cache/builders/remote-data-bu
3535
import { HttpOptions } from '../../core/dspace-rest/dspace-rest.service';
3636
import { Router } from '@angular/router';
3737
import { getItemPageRoute } from '../../item-page/item-page-routing-paths';
38-
import { getBitstreamContentRoute } from '../../app-routing-paths';
3938
import { hasFailed } from 'src/app/core/data/request-entry-state.model';
4039
import { FindListOptions } from '../../core/data/find-list-options.model';
4140
import isEqual from 'lodash/isEqual';
4241
import cloneDeep from 'lodash/cloneDeep';
4342
import { ClarinUserMetadataDataService } from '../../core/data/clarin/clarin-user-metadata.service';
4443
import { 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) {

src/assets/i18n/cs.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8433,4 +8433,6 @@
84338433
"change.submitter.page.items.handle.message": "Handle záznamu: ",
84348434
// "change.submitter.page.items.name.message": "Item's name: ",
84358435
"change.submitter.page.items.name.message": "Název záznamu: ",
8436+
// "clarin-license-agreement-page.download-error": "An error occurred while downloading the file. Please try again later.",
8437+
"clarin-license-agreement-page.download-error": "Při stahování souboru došlo k chybě. Zkuste to prosím znovu později.",
84368438
}

src/assets/i18n/en.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5982,4 +5982,6 @@
59825982
"change.submitter.page.items.handle.message": "Item's handle: ",
59835983

59845984
"change.submitter.page.items.name.message": "Item's name: ",
5985+
5986+
"clarin-license-agreement-page.download-error": "An error occurred while downloading the file. Please try again later.",
59855987
}

0 commit comments

Comments
 (0)