mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
76150: Add a bitstream download page
This commit is contained in:
@@ -64,69 +64,3 @@ In order to start using one of these services, select it from the [Angulartics P
|
||||
|
||||
The Google Analytics script was added in [`main.browser.ts`](https://github.com/DSpace/dspace-angular/blob/ff04760f4af91ac3e7add5e7424a46cb2439e874/src/main.browser.ts#L33) instead of the `<head>` tag in `index.html` to ensure events get sent when the page is shown in a client's browser, and not when it's rendered on the universal server. Likely you'll want to do the same when adding a new service.
|
||||
|
||||
## SEO when hosting REST Api and UI on different servers
|
||||
|
||||
Indexers such as Google Scholar require that files are hosted on the same domain as the page that links them. In DSpace 7, Bitstreams are served from the REST server. So if you use different servers for the REST api and the UI you'll want to ensure that Bitstream downloads are proxied through the UI server.
|
||||
|
||||
In order to achieve this we'll need to do two things:
|
||||
- **Proxy the Bitstream downloads through the UI server.** You'll need to put a webserver such as httpd or nginx in front of the UI server in order to achieve this. [Below](#apache-http-server-config) you'll find a section explaining how to do it in httpd.
|
||||
- **Update the URLs for Bitstream downloads to match the UI server.** This can be done using a setting in the UI environment file.
|
||||
|
||||
### UI config
|
||||
If you set the property `rewriteDownloadUrls` to `true` in your `environment.prod.ts` file, the [origin](https://developer.mozilla.org/en-US/docs/Glossary/Origin) of any download URL will be replaced by the origin of the UI. This will also happen for the `citation_pdf_url` `<meta>` tag on Item pages.
|
||||
|
||||
The app will determine the UI origin currently in use, so the external UI URL doesn't need to be configured anywhere and rewrites will still work if you host the UI from multiple domains.
|
||||
|
||||
### Apache HTTP Server config
|
||||
|
||||
#### Basics
|
||||
In order to be able to host bitstreams from the UI Server you'll need to enable mod_proxy and add the following to the httpd config of your UI server:
|
||||
|
||||
```
|
||||
ProxyPassMatch "/server/api/core/bitstreams/([^/]+)/content" "http://rest.api/server/api/core/bitstreams/$1/content"
|
||||
ProxyPassReverse "/server/api/core/bitstreams/([^/]+)/content" "http://rest.api/server/api/core/bitstreams/$1/content"
|
||||
```
|
||||
|
||||
Replace http://rest.api in with the correct origin for your REST server.
|
||||
|
||||
The `ProxyPassMatch` line forwards all requests matching the regular expression for a bitstream download URL to the corresponding path on the REST server
|
||||
|
||||
The `ProxyPassReverse` ensures that if the REST server were to return redirect response, httpd would also swap out its hostname for the hostname of the UI before forwarding the response to the client.
|
||||
|
||||
#### Using HTTPS
|
||||
If your REST server uses https, you'll need to enable mod_ssl and ensure `SSLProxyEngine on` is part of your UI server's httpd config as well
|
||||
|
||||
If the UI hostname doesn't match the CN in the SSL certificate of the REST server (which is likely if they're on different domains), you'll also need to add the following lines
|
||||
|
||||
```
|
||||
SSLProxyCheckPeerCN off
|
||||
SSLProxyCheckPeerName off
|
||||
```
|
||||
These are two names for [the same directive](https://httpd.apache.org/docs/trunk/mod/mod_ssl.html#sslproxycheckpeername) that have been used for various versions of httpd, old versions need the former, then some in-between versions need both, and newer versions only need the latter. Keeping them both doesn't harm anything.
|
||||
|
||||
So the entire config becomes:
|
||||
|
||||
```
|
||||
SSLProxyEngine on
|
||||
SSLProxyCheckPeerCN off
|
||||
SSLProxyCheckPeerName off
|
||||
ProxyPassMatch "/server/api/core/bitstreams/([^/]+)/content" "https://rest.api/server/api/core/bitstreams/$1/content"
|
||||
ProxyPassReverse "/server/api/core/bitstreams/([^/]+)/content" "https://rest.api/server/api/core/bitstreams/$1/content"
|
||||
```
|
||||
|
||||
If you don't want httpd to verify the certificate of the REST server, you can also turn all checks off with the following config:
|
||||
|
||||
```
|
||||
SSLProxyEngine on
|
||||
SSLProxyVerify none
|
||||
SSLProxyCheckPeerCN off
|
||||
SSLProxyCheckPeerName off
|
||||
SSLProxyCheckPeerExpire off
|
||||
ProxyPassMatch "/server/api/core/bitstreams/([^/]+)/content" "https://rest.api/server/api/core/bitstreams/$1/content"
|
||||
ProxyPassReverse "/server/api/core/bitstreams/([^/]+)/content" "https://rest.api/server/api/core/bitstreams/$1/content"
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -3,6 +3,7 @@ import { RouterModule } from '@angular/router';
|
||||
import { EditBitstreamPageComponent } from './edit-bitstream-page/edit-bitstream-page.component';
|
||||
import { AuthenticatedGuard } from '../core/auth/authenticated.guard';
|
||||
import { BitstreamPageResolver } from './bitstream-page.resolver';
|
||||
import { BitstreamDownloadPageComponent } from '../shared/bitstream-download-page/bitstream-download-page.component';
|
||||
|
||||
const EDIT_BITSTREAM_PATH = ':id/edit';
|
||||
|
||||
@@ -12,6 +13,13 @@ const EDIT_BITSTREAM_PATH = ':id/edit';
|
||||
@NgModule({
|
||||
imports: [
|
||||
RouterModule.forChild([
|
||||
{
|
||||
path:':id/download',
|
||||
component: BitstreamDownloadPageComponent,
|
||||
resolve: {
|
||||
bitstream: BitstreamPageResolver
|
||||
},
|
||||
},
|
||||
{
|
||||
path: EDIT_BITSTREAM_PATH,
|
||||
component: EditBitstreamPageComponent,
|
||||
|
@@ -34,7 +34,7 @@
|
||||
</dl>
|
||||
</div>
|
||||
<div class="col-2">
|
||||
<ds-file-download-link [href]="file._links.content.href" [download]="file.name">
|
||||
<ds-file-download-link [bitstream]="file">
|
||||
{{"item.page.filesection.download" | translate}}
|
||||
</ds-file-download-link>
|
||||
</div>
|
||||
@@ -76,7 +76,7 @@
|
||||
</dl>
|
||||
</div>
|
||||
<div class="col-2">
|
||||
<ds-file-download-link [href]="file._links.content.href" [download]="file.name">
|
||||
<ds-file-download-link [bitstream]="file">
|
||||
{{"item.page.filesection.download" | translate}}
|
||||
</ds-file-download-link>
|
||||
</div>
|
||||
|
@@ -1,7 +1,7 @@
|
||||
<ng-container *ngVar="(bitstreams$ | async) as bitstreams">
|
||||
<ds-metadata-field-wrapper *ngIf="bitstreams?.length > 0" [label]="label | translate">
|
||||
<div class="file-section">
|
||||
<ds-file-download-link *ngFor="let file of bitstreams; let last=last;" [href]="file?._links.content.href" [download]="file?.name">
|
||||
<ds-file-download-link *ngFor="let file of bitstreams; let last=last;" [bitstream]="file">
|
||||
<span>{{file?.name}}</span>
|
||||
<span>({{(file?.sizeBytes) | dsFileSize }})</span>
|
||||
<span *ngIf="!last" innerHTML="{{separator}}"></span>
|
||||
|
@@ -12,4 +12,5 @@ export enum FeatureID {
|
||||
CanManageGroups = 'canManageGroups',
|
||||
IsCollectionAdmin = 'isCollectionAdmin',
|
||||
IsCommunityAdmin = 'isCommunityAdmin',
|
||||
CanDownload = 'canDownload',
|
||||
}
|
||||
|
@@ -182,10 +182,6 @@ describe('MetadataService', () => {
|
||||
Title,
|
||||
// tslint:disable-next-line:no-empty
|
||||
{ provide: ItemDataService, useValue: { findById: () => {} } },
|
||||
{
|
||||
provide: HardRedirectService,
|
||||
useValue: { rewriteDownloadURL: (a) => a, getRequestOrigin: () => environment.ui.baseUrl }
|
||||
},
|
||||
BrowseService,
|
||||
MetadataService
|
||||
],
|
||||
@@ -225,7 +221,7 @@ describe('MetadataService', () => {
|
||||
tick();
|
||||
expect(tagStore.get('citation_dissertation_name')[0].content).toEqual('Test PowerPoint Document');
|
||||
expect(tagStore.get('citation_dissertation_institution')[0].content).toEqual('Mock Publisher');
|
||||
expect(tagStore.get('citation_abstract_html_url')[0].content).toEqual(new URLCombiner(environment.ui.baseUrl, router.url).toString());
|
||||
expect(tagStore.get('citation_abstract_html_url')[0].content).toEqual([environment.ui.baseUrl, router.url].join(''));
|
||||
expect(tagStore.get('citation_pdf_url')[0].content).toEqual('https://dspace7.4science.it/dspace-spring-rest/api/core/bitstreams/99b00f3c-1cc6-4689-8158-91965bee6b28/content');
|
||||
}));
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Inject, Injectable } from '@angular/core';
|
||||
|
||||
import { Meta, MetaDefinition, Title } from '@angular/platform-browser';
|
||||
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
|
||||
@@ -20,8 +20,7 @@ import { Bitstream } from '../shared/bitstream.model';
|
||||
import { DSpaceObject } from '../shared/dspace-object.model';
|
||||
import { Item } from '../shared/item.model';
|
||||
import { getFirstSucceededRemoteDataPayload, getFirstSucceededRemoteListPayload } from '../shared/operators';
|
||||
import { HardRedirectService } from '../services/hard-redirect.service';
|
||||
import { URLCombiner } from '../url-combiner/url-combiner';
|
||||
import { environment } from '../../../environments/environment';
|
||||
import { RootDataService } from '../data/root-data.service';
|
||||
|
||||
@Injectable()
|
||||
@@ -262,7 +261,7 @@ export class MetadataService {
|
||||
*/
|
||||
private setCitationAbstractUrlTag(): void {
|
||||
if (this.currentObject.value instanceof Item) {
|
||||
const value = new URLCombiner(this.redirectService.getRequestOrigin(), this.router.url).toString();
|
||||
const value = [environment.ui.baseUrl, this.router.url].join('');
|
||||
this.addMetaTag('citation_abstract_html_url', value);
|
||||
}
|
||||
}
|
||||
@@ -287,8 +286,7 @@ export class MetadataService {
|
||||
getFirstSucceededRemoteDataPayload()
|
||||
).subscribe((format: BitstreamFormat) => {
|
||||
if (format.mimetype === 'application/pdf') {
|
||||
const rewrittenURL= this.redirectService.rewriteDownloadURL(bitstream._links.content.href);
|
||||
this.addMetaTag('citation_pdf_url', rewrittenURL);
|
||||
this.addMetaTag('citation_pdf_url', bitstream._links.content.href);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@
|
||||
|
||||
<div *ngVar="(filesRD$ | async)?.payload?.page as files">
|
||||
<ds-process-detail-field *ngIf="files && files?.length > 0" id="process-files" [title]="'process.detail.output-files'">
|
||||
<ds-file-download-link *ngFor="let file of files; let last=last;" [href]="file?._links?.content?.href" [download]="getFileName(file)">
|
||||
<ds-file-download-link *ngFor="let file of files; let last=last;" [bitstream]="file">
|
||||
<span>{{getFileName(file)}}</span>
|
||||
<span>({{(file?.sizeBytes) | dsFileSize }})</span>
|
||||
</ds-file-download-link>
|
||||
|
@@ -0,0 +1,3 @@
|
||||
<div class="container">
|
||||
<h3>{{'bitstream.download.page' | translate:{bitstream: (bitstream$ | async)?.name} }}</h3>
|
||||
</div>
|
@@ -0,0 +1,158 @@
|
||||
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { AuthService } from '../../core/auth/auth.service';
|
||||
import { FileService } from '../../core/shared/file.service';
|
||||
import { of as observableOf } from 'rxjs';
|
||||
import { Bitstream } from '../../core/shared/bitstream.model';
|
||||
import { BitstreamDownloadPageComponent } from './bitstream-download-page.component';
|
||||
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
|
||||
import { HardRedirectService } from '../../core/services/hard-redirect.service';
|
||||
import { createSuccessfulRemoteDataObject } from '../remote-data.utils';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { getForbiddenRoute } from '../../app-routing-paths';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
|
||||
describe('BitstreamDownloadPageComponent', () => {
|
||||
let component: BitstreamDownloadPageComponent;
|
||||
let fixture: ComponentFixture<BitstreamDownloadPageComponent>;
|
||||
|
||||
let authService: AuthService;
|
||||
let fileService: FileService;
|
||||
let authorizationService: AuthorizationDataService;
|
||||
let hardRedirectService: HardRedirectService;
|
||||
let activatedRoute;
|
||||
let router;
|
||||
|
||||
let bitstream: Bitstream;
|
||||
|
||||
function init() {
|
||||
authService = jasmine.createSpyObj('authService', {
|
||||
isAuthenticated: observableOf(true),
|
||||
setRedirectUrl: {}
|
||||
});
|
||||
authorizationService = jasmine.createSpyObj('authorizationSerivice', {
|
||||
isAuthorized: observableOf(true)
|
||||
});
|
||||
|
||||
fileService = jasmine.createSpyObj('fileService', {
|
||||
downloadFile: observableOf('content-url-with-headers')
|
||||
});
|
||||
|
||||
hardRedirectService = jasmine.createSpyObj('fileService', {
|
||||
redirect: {}
|
||||
});
|
||||
bitstream = Object.assign(new Bitstream(), {
|
||||
uuid: 'bitstreamUuid',
|
||||
_links: {
|
||||
content: {href: 'bitstream-content-link'},
|
||||
self: {href: 'bitstream-self-link'},
|
||||
}
|
||||
});
|
||||
|
||||
activatedRoute = {
|
||||
data: observableOf({
|
||||
bitstream: createSuccessfulRemoteDataObject(
|
||||
bitstream
|
||||
)
|
||||
})
|
||||
};
|
||||
|
||||
router = jasmine.createSpyObj('router', ['navigateByUrl']);
|
||||
}
|
||||
|
||||
function initTestbed() {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [CommonModule, TranslateModule.forRoot()],
|
||||
declarations: [BitstreamDownloadPageComponent],
|
||||
providers: [
|
||||
{provide: ActivatedRoute, useValue: activatedRoute},
|
||||
{provide: Router, useValue: router},
|
||||
{provide: AuthorizationDataService, useValue: authorizationService},
|
||||
{provide: AuthService, useValue: authService},
|
||||
{provide: FileService, useValue: fileService},
|
||||
{provide: HardRedirectService, useValue: hardRedirectService},
|
||||
]
|
||||
})
|
||||
.compileComponents();
|
||||
}
|
||||
|
||||
describe('init', () => {
|
||||
beforeEach(async(() => {
|
||||
init();
|
||||
initTestbed();
|
||||
}));
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(BitstreamDownloadPageComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
it('should init the comp', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('bitstream retrieval', () => {
|
||||
describe('when the user is authorized and not logged in', () => {
|
||||
beforeEach(async(() => {
|
||||
init();
|
||||
(authService.isAuthenticated as jasmine.Spy).and.returnValue(observableOf(false));
|
||||
|
||||
initTestbed();
|
||||
}));
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(BitstreamDownloadPageComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
it('should redirect to the content link', () => {
|
||||
expect(hardRedirectService.redirect).toHaveBeenCalledWith('bitstream-content-link');
|
||||
});
|
||||
});
|
||||
describe('when the user is authorized and logged in', () => {
|
||||
beforeEach(async(() => {
|
||||
init();
|
||||
initTestbed();
|
||||
}));
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(BitstreamDownloadPageComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
it('should redirect to an updated content link', () => {
|
||||
expect(hardRedirectService.redirect).toHaveBeenCalledWith('content-url-with-headers');
|
||||
});
|
||||
});
|
||||
describe('when the user is not authorized and logged in', () => {
|
||||
beforeEach(async(() => {
|
||||
init();
|
||||
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
|
||||
initTestbed();
|
||||
}));
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(BitstreamDownloadPageComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
it('should navigate to the forbidden route', () => {
|
||||
expect(router.navigateByUrl).toHaveBeenCalledWith(getForbiddenRoute(), {skipLocationChange: true});
|
||||
});
|
||||
});
|
||||
describe('when the user is not authorized and not logged in', () => {
|
||||
beforeEach(async(() => {
|
||||
init();
|
||||
(authService.isAuthenticated as jasmine.Spy).and.returnValue(observableOf(false));
|
||||
(authorizationService.isAuthorized as jasmine.Spy).and.returnValue(observableOf(false));
|
||||
initTestbed();
|
||||
}));
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(BitstreamDownloadPageComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
it('should navigate to the login page', () => {
|
||||
expect(authService.setRedirectUrl).toHaveBeenCalled();
|
||||
expect(router.navigateByUrl).toHaveBeenCalledWith('login');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@@ -0,0 +1,83 @@
|
||||
import { Component, OnDestroy, OnInit } from '@angular/core';
|
||||
import { map } 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 { 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',
|
||||
templateUrl: './bitstream-download-page.component.html'
|
||||
})
|
||||
/**
|
||||
* Page component for downloading a bitstream
|
||||
*/
|
||||
export class BitstreamDownloadPageComponent implements OnInit, OnDestroy {
|
||||
|
||||
bitstream$: Observable<Bitstream>;
|
||||
bitstreamRD$: Observable<RemoteData<Bitstream>>;
|
||||
|
||||
subs: Subscription[] = [];
|
||||
|
||||
constructor(
|
||||
private route: ActivatedRoute,
|
||||
protected router: Router,
|
||||
private authorizationService: AuthorizationDataService,
|
||||
private auth: AuthService,
|
||||
private fileService: FileService,
|
||||
private hardRedirectService: HardRedirectService,
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
|
||||
this.bitstreamRD$ = this.route.data.pipe(
|
||||
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) => {
|
||||
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]) => {
|
||||
if (isAuthorized && isLoggedIn) {
|
||||
const fileLink$ = this.fileService.downloadFile(bitstream._links.content.href);
|
||||
this.subs.push(fileLink$.subscribe((fileLink) => {
|
||||
this.hardRedirectService.redirect(fileLink);
|
||||
}));
|
||||
} else if (isAuthorized && !isLoggedIn) {
|
||||
this.hardRedirectService.redirect(bitstream._links.content.href);
|
||||
} else if (!isAuthorized && isLoggedIn) {
|
||||
this.router.navigateByUrl(getForbiddenRoute(), {skipLocationChange: true});
|
||||
} else if (!isAuthorized && !isLoggedIn) {
|
||||
this.auth.setRedirectUrl(this.router.url);
|
||||
this.router.navigateByUrl('login');
|
||||
}
|
||||
}));
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.subs
|
||||
.filter((subscription) => hasValue(subscription))
|
||||
.forEach((subscription) => subscription.unsubscribe());
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
<a *ngIf="!(isAuthenticated$ | async)" [href]="href" [download]="download"><ng-container *ngTemplateOutlet="content"></ng-container></a>
|
||||
<a *ngIf="(isAuthenticated$ | async)" [href]="href" [download]="download" (click)="downloadFile()"><ng-container *ngTemplateOutlet="content"></ng-container></a>
|
||||
<a [href]="bitstreamPath"><ng-container *ngTemplateOutlet="content"></ng-container></a>
|
||||
|
||||
|
||||
<ng-template #content>
|
||||
<ng-content></ng-content>
|
||||
|
@@ -3,7 +3,10 @@ import { FileDownloadLinkComponent } from './file-download-link.component';
|
||||
import { AuthService } from '../../core/auth/auth.service';
|
||||
import { FileService } from '../../core/shared/file.service';
|
||||
import { of as observableOf } from 'rxjs';
|
||||
import { HardRedirectService } from '../../core/services/hard-redirect.service';
|
||||
import { Bitstream } from '../../core/shared/bitstream.model';
|
||||
import { By } from '@angular/platform-browser';
|
||||
import { URLCombiner } from '../../core/url-combiner/url-combiner';
|
||||
import { getBitstreamModuleRoute } from '../../app-routing-paths';
|
||||
|
||||
describe('FileDownloadLinkComponent', () => {
|
||||
let component: FileDownloadLinkComponent;
|
||||
@@ -11,14 +14,16 @@ describe('FileDownloadLinkComponent', () => {
|
||||
|
||||
let authService: AuthService;
|
||||
let fileService: FileService;
|
||||
let href: string;
|
||||
let bitstream: Bitstream;
|
||||
|
||||
function init() {
|
||||
authService = jasmine.createSpyObj('authService', {
|
||||
isAuthenticated: observableOf(true)
|
||||
});
|
||||
fileService = jasmine.createSpyObj('fileService', ['downloadFile']);
|
||||
href = 'test-download-file-link';
|
||||
bitstream = Object.assign(new Bitstream(), {
|
||||
uuid: 'bitstreamUuid',
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(waitForAsync(() => {
|
||||
@@ -28,7 +33,6 @@ describe('FileDownloadLinkComponent', () => {
|
||||
providers: [
|
||||
{ provide: AuthService, useValue: authService },
|
||||
{ provide: FileService, useValue: fileService },
|
||||
{ provide: HardRedirectService, useValue: { rewriteDownloadURL: (a) => a } },
|
||||
]
|
||||
})
|
||||
.compileComponents();
|
||||
@@ -37,23 +41,22 @@ describe('FileDownloadLinkComponent', () => {
|
||||
beforeEach(() => {
|
||||
fixture = TestBed.createComponent(FileDownloadLinkComponent);
|
||||
component = fixture.componentInstance;
|
||||
component.href = href;
|
||||
component.bitstream = bitstream;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
describe('downloadFile', () => {
|
||||
let result;
|
||||
describe('init', () => {
|
||||
|
||||
beforeEach(() => {
|
||||
result = component.downloadFile();
|
||||
describe('getBitstreamPath', () => {
|
||||
it('should set the bitstreamPath based on the input bitstream', () => {
|
||||
expect(component.bitstreamPath).toEqual(new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString());
|
||||
});
|
||||
})
|
||||
|
||||
it('should call fileService.downloadFile with the provided href', () => {
|
||||
expect(fileService.downloadFile).toHaveBeenCalledWith(href);
|
||||
});
|
||||
it('should init the component', () => {
|
||||
const link = fixture.debugElement.query(By.css('a')).nativeElement;
|
||||
expect(link.href).toContain(new URLCombiner(getBitstreamModuleRoute(), bitstream.uuid, 'download').toString());
|
||||
})
|
||||
|
||||
it('should return false', () => {
|
||||
expect(result).toEqual(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -2,7 +2,9 @@ import { Component, Input, OnInit } from '@angular/core';
|
||||
import { FileService } from '../../core/shared/file.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { AuthService } from '../../core/auth/auth.service';
|
||||
import { HardRedirectService } from '../../core/services/hard-redirect.service';
|
||||
import { Bitstream } from '../../core/shared/bitstream.model';
|
||||
import { getBitstreamModuleRoute } from '../../app-routing-paths';
|
||||
import { URLCombiner } from '../../core/url-combiner/url-combiner';
|
||||
|
||||
@Component({
|
||||
selector: 'ds-file-download-link',
|
||||
@@ -15,15 +17,12 @@ import { HardRedirectService } from '../../core/services/hard-redirect.service';
|
||||
* ensuring the user is authorized to download the file.
|
||||
*/
|
||||
export class FileDownloadLinkComponent implements OnInit {
|
||||
/**
|
||||
* Href to link to
|
||||
*/
|
||||
@Input() href: string;
|
||||
|
||||
/**
|
||||
* Optional file name for the download
|
||||
* Optional bitstream instead of href and file name
|
||||
*/
|
||||
@Input() download: string;
|
||||
@Input() bitstream: Bitstream;
|
||||
bitstreamPath: string;
|
||||
|
||||
/**
|
||||
* Whether or not the current user is authenticated
|
||||
@@ -32,20 +31,15 @@ export class FileDownloadLinkComponent implements OnInit {
|
||||
|
||||
constructor(private fileService: FileService,
|
||||
private authService: AuthService,
|
||||
private redirectService: HardRedirectService) {
|
||||
) {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.isAuthenticated$ = this.authService.isAuthenticated();
|
||||
this.href = this.redirectService.rewriteDownloadURL(this.href);
|
||||
this.bitstreamPath = this.getBitstreamPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a download of the file
|
||||
* Return false to ensure the original href is displayed when the user hovers over the link
|
||||
*/
|
||||
downloadFile(): boolean {
|
||||
this.fileService.downloadFile(this.href);
|
||||
return false;
|
||||
getBitstreamPath() {
|
||||
return new URLCombiner(getBitstreamModuleRoute(), this.bitstream.uuid, 'download').toString();
|
||||
}
|
||||
}
|
||||
|
@@ -223,6 +223,7 @@ import { SearchObjects } from './search/search-objects.model';
|
||||
import { SearchResult } from './search/search-result.model';
|
||||
import { FacetConfigResponse } from './search/facet-config-response.model';
|
||||
import { FacetValues } from './search/facet-values.model';
|
||||
import { BitstreamDownloadPageComponent } from './bitstream-download-page/bitstream-download-page.component';
|
||||
import { GenericItemPageFieldComponent } from '../+item-page/simple/field-components/specific-field/generic/generic-item-page-field.component';
|
||||
import { MetadataRepresentationListComponent } from '../+item-page/simple/metadata-representation-list/metadata-representation-list.component';
|
||||
import { RelatedItemsComponent } from '../+item-page/simple/related-items/related-items-component';
|
||||
@@ -432,6 +433,7 @@ const COMPONENTS = [
|
||||
EpersonSearchBoxComponent,
|
||||
GroupSearchBoxComponent,
|
||||
FileDownloadLinkComponent,
|
||||
BitstreamDownloadPageComponent,
|
||||
CollectionDropdownComponent,
|
||||
ExportMetadataSelectorComponent,
|
||||
ConfirmationModalComponent,
|
||||
@@ -510,6 +512,14 @@ const ENTRY_COMPONENTS = [
|
||||
ClaimedTaskActionsRejectComponent,
|
||||
ClaimedTaskActionsReturnToPoolComponent,
|
||||
ClaimedTaskActionsEditMetadataComponent,
|
||||
CollectionDropdownComponent,
|
||||
FileDownloadLinkComponent,
|
||||
BitstreamDownloadPageComponent,
|
||||
CurationFormComponent,
|
||||
ExportMetadataSelectorComponent,
|
||||
ConfirmationModalComponent,
|
||||
VocabularyTreeviewComponent,
|
||||
SidebarSearchListElementComponent,
|
||||
PublicationSidebarSearchListElementComponent,
|
||||
CollectionSidebarSearchListElementComponent,
|
||||
CommunitySidebarSearchListElementComponent,
|
||||
|
@@ -526,6 +526,10 @@
|
||||
|
||||
|
||||
|
||||
"bitstream.download.page": "Now downloading {{bitstream}}..." ,
|
||||
|
||||
|
||||
|
||||
"bitstream.edit.bitstream": "Bitstream: ",
|
||||
|
||||
"bitstream.edit.form.description.hint": "Optionally, provide a brief description of the file, for example \"<i>Main article</i>\" or \"<i>Experiment data readings</i>\".",
|
||||
|
Reference in New Issue
Block a user