mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
76150: Implement feedback
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import { Inject, Injectable } from '@angular/core';
|
||||
import { RawRestResponse } from '../dspace-rest/raw-rest-response.model';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { take } from 'rxjs/operators';
|
||||
import { map, take } from 'rxjs/operators';
|
||||
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
|
||||
import { URLCombiner } from '../url-combiner/url-combiner';
|
||||
import { hasValue } from '../../shared/empty.util';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
|
||||
/**
|
||||
* Provides utility methods to save files on the client-side.
|
||||
@@ -17,17 +18,16 @@ export class FileService {
|
||||
) { }
|
||||
|
||||
/**
|
||||
* Combines an URL with a short-lived token and sets the current URL to the newly created one
|
||||
* Combines an URL with a short-lived token and sets the current URL to the newly created one and returns it
|
||||
*
|
||||
* @param url
|
||||
* file url
|
||||
*/
|
||||
downloadFile(url: string) {
|
||||
this.authService.getShortlivedToken().pipe(take(1)).subscribe((token) => {
|
||||
this._window.nativeWindow.location.href = hasValue(token) ? new URLCombiner(url, `?authentication-token=${token}`).toString() : url;
|
||||
});
|
||||
retrieveFileDownloadLink(url: string): Observable<string> {
|
||||
return this.authService.getShortlivedToken().pipe(take(1), map((token) =>
|
||||
hasValue(token) ? new URLCombiner(url, `?authentication-token=${token}`).toString() : url
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Derives file name from the http response
|
||||
* by looking inside content-disposition
|
||||
|
@@ -35,7 +35,7 @@ describe('BitstreamDownloadPageComponent', () => {
|
||||
});
|
||||
|
||||
fileService = jasmine.createSpyObj('fileService', {
|
||||
downloadFile: observableOf('content-url-with-headers')
|
||||
retrieveFileDownloadLink: observableOf('content-url-with-headers')
|
||||
});
|
||||
|
||||
hardRedirectService = jasmine.createSpyObj('fileService', {
|
||||
|
@@ -1,19 +1,17 @@
|
||||
import { Component, OnDestroy, OnInit } from '@angular/core';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { filter, map, switchMap, take } from 'rxjs/operators';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { Subscription } from 'rxjs/internal/Subscription';
|
||||
import { hasValue, isNotEmpty } from '../empty.util';
|
||||
import { getRemoteDataPayload, redirectOn4xx } from '../../core/shared/operators';
|
||||
import { Bitstream } from '../../core/shared/bitstream.model';
|
||||
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
|
||||
import { FeatureID } from '../../core/data/feature-authorization/feature-id';
|
||||
import { AuthService } from '../../core/auth/auth.service';
|
||||
import { combineLatest as observableCombineLatest, Observable } from 'rxjs';
|
||||
import { combineLatest as observableCombineLatest, Observable, of as observableOf } from 'rxjs';
|
||||
import { FileService } from '../../core/shared/file.service';
|
||||
import { HardRedirectService } from '../../core/services/hard-redirect.service';
|
||||
import { getForbiddenRoute } from '../../app-routing-paths';
|
||||
import { RemoteData } from '../../core/data/remote-data';
|
||||
import { tap } from 'rxjs/internal/operators/tap';
|
||||
|
||||
@Component({
|
||||
selector: 'ds-bitstream-download-page',
|
||||
@@ -22,12 +20,11 @@ import { tap } from 'rxjs/internal/operators/tap';
|
||||
/**
|
||||
* Page component for downloading a bitstream
|
||||
*/
|
||||
export class BitstreamDownloadPageComponent implements OnInit, OnDestroy {
|
||||
export class BitstreamDownloadPageComponent implements OnInit {
|
||||
|
||||
bitstream$: Observable<Bitstream>;
|
||||
bitstreamRD$: Observable<RemoteData<Bitstream>>;
|
||||
|
||||
subs: Subscription[] = [];
|
||||
|
||||
constructor(
|
||||
private route: ActivatedRoute,
|
||||
@@ -46,22 +43,34 @@ export class BitstreamDownloadPageComponent implements OnInit, OnDestroy {
|
||||
map((data) => data.bitstream));
|
||||
|
||||
this.bitstream$ = this.bitstreamRD$.pipe(
|
||||
tap((v) => console.log('dfdf', v)),
|
||||
redirectOn4xx(this.router, this.auth),
|
||||
getRemoteDataPayload()
|
||||
);
|
||||
|
||||
this.subs.push(this.bitstream$.subscribe((bitstream) => {
|
||||
this.bitstream$.pipe(
|
||||
switchMap((bitstream: Bitstream) => {
|
||||
const isAuthorized$ = this.authorizationService.isAuthorized(FeatureID.CanDownload, isNotEmpty(bitstream) ? bitstream.self : undefined);
|
||||
const isLoggedIn$ = this.auth.isAuthenticated();
|
||||
|
||||
this.subs.push(observableCombineLatest(isAuthorized$, isLoggedIn$)
|
||||
.subscribe(([isAuthorized, isLoggedIn]) => {
|
||||
return observableCombineLatest([isAuthorized$, isLoggedIn$, observableOf(bitstream)]);
|
||||
}),
|
||||
filter(([isAuthorized, isLoggedIn, bitstream]: [boolean, boolean, Bitstream]) => hasValue(isAuthorized) && hasValue(isLoggedIn)),
|
||||
take(1),
|
||||
switchMap(([isAuthorized, isLoggedIn, bitstream]: [boolean, boolean, Bitstream]) => {
|
||||
if (isAuthorized && isLoggedIn) {
|
||||
const fileLink$ = this.fileService.downloadFile(bitstream._links.content.href);
|
||||
this.subs.push(fileLink$.subscribe((fileLink) => {
|
||||
this.hardRedirectService.redirect(fileLink);
|
||||
return this.fileService.retrieveFileDownloadLink(bitstream._links.content.href).pipe(
|
||||
filter((fileLink) => hasValue(fileLink)),
|
||||
take(1),
|
||||
map((fileLink) => {
|
||||
return [isAuthorized, isLoggedIn, bitstream, fileLink];
|
||||
// return [isAuthorized, isLoggedIn, bitstream];
|
||||
}));
|
||||
} else {
|
||||
return [[isAuthorized, isLoggedIn, bitstream, '']];
|
||||
}
|
||||
})
|
||||
).subscribe(([isAuthorized, isLoggedIn, bitstream, fileLink]: [boolean, boolean, Bitstream, string]) => {
|
||||
if (isAuthorized && isLoggedIn && isNotEmpty(fileLink)) {
|
||||
this.hardRedirectService.redirect(fileLink);
|
||||
} else if (isAuthorized && !isLoggedIn) {
|
||||
this.hardRedirectService.redirect(bitstream._links.content.href);
|
||||
} else if (!isAuthorized && isLoggedIn) {
|
||||
@@ -70,14 +79,6 @@ export class BitstreamDownloadPageComponent implements OnInit, OnDestroy {
|
||||
this.auth.setRedirectUrl(this.router.url);
|
||||
this.router.navigateByUrl('login');
|
||||
}
|
||||
}));
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.subs
|
||||
.filter((subscription) => hasValue(subscription))
|
||||
.forEach((subscription) => subscription.unsubscribe());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -82,7 +82,7 @@ export class ItemDetailPreviewComponent {
|
||||
first())
|
||||
.subscribe((url) => {
|
||||
const fileUrl = `${url}/${uuid}/content`;
|
||||
this.fileService.downloadFile(fileUrl);
|
||||
this.fileService.retrieveFileDownloadLink(fileUrl);
|
||||
});
|
||||
}
|
||||
|
||||
|
@@ -41,7 +41,7 @@ import { FormBuilderService } from '../../../../shared/form/builder/form-builder
|
||||
|
||||
function getMockFileService(): FileService {
|
||||
return jasmine.createSpyObj('FileService', {
|
||||
downloadFile: jasmine.createSpy('downloadFile'),
|
||||
retrieveFileDownloadLink: jasmine.createSpy('retrieveFileDownloadLink'),
|
||||
getFileNameFromResponseContentDisposition: jasmine.createSpy('getFileNameFromResponseContentDisposition')
|
||||
});
|
||||
}
|
||||
@@ -232,7 +232,7 @@ describe('SubmissionSectionUploadFileComponent test suite', () => {
|
||||
|
||||
tick();
|
||||
|
||||
expect(fileService.downloadFile).toHaveBeenCalled();
|
||||
expect(fileService.retrieveFileDownloadLink).toHaveBeenCalled();
|
||||
}));
|
||||
|
||||
it('should save Bitstream File data properly when form is valid', fakeAsync(() => {
|
||||
|
@@ -224,7 +224,7 @@ export class SubmissionSectionUploadFileComponent implements OnChanges, OnInit {
|
||||
first())
|
||||
.subscribe((url) => {
|
||||
const fileUrl = `${url}/${this.fileData.uuid}/content`;
|
||||
this.fileService.downloadFile(fileUrl);
|
||||
this.fileService.retrieveFileDownloadLink(fileUrl);
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user