From b578aa408bd5737d33342f8f393d8d2330ae203b Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Mon, 22 Jun 2020 11:40:39 +0200 Subject: [PATCH] 71504: FileDownloadLinkComponent --- .../full-file-section.component.html | 4 +- .../file-section/file-section.component.html | 4 +- .../file-download-link.component.html | 6 +++ .../file-download-link.component.scss | 0 .../file-download-link.component.spec.ts | 25 ++++++++++ .../file-download-link.component.ts | 48 +++++++++++++++++++ src/app/shared/shared.module.ts | 7 ++- 7 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 src/app/shared/file-download-link/file-download-link.component.html create mode 100644 src/app/shared/file-download-link/file-download-link.component.scss create mode 100644 src/app/shared/file-download-link/file-download-link.component.spec.ts create mode 100644 src/app/shared/file-download-link/file-download-link.component.ts diff --git a/src/app/+item-page/full/field-components/file-section/full-file-section.component.html b/src/app/+item-page/full/field-components/file-section/full-file-section.component.html index b8ab9bdb41..7c1719eb82 100644 --- a/src/app/+item-page/full/field-components/file-section/full-file-section.component.html +++ b/src/app/+item-page/full/field-components/file-section/full-file-section.component.html @@ -21,9 +21,9 @@
- + {{"item.page.filesection.download" | translate}} - +
diff --git a/src/app/+item-page/simple/field-components/file-section/file-section.component.html b/src/app/+item-page/simple/field-components/file-section/file-section.component.html index 6533322e03..17e4a795e7 100644 --- a/src/app/+item-page/simple/field-components/file-section/file-section.component.html +++ b/src/app/+item-page/simple/field-components/file-section/file-section.component.html @@ -1,11 +1,11 @@ diff --git a/src/app/shared/file-download-link/file-download-link.component.html b/src/app/shared/file-download-link/file-download-link.component.html new file mode 100644 index 0000000000..06624c8b40 --- /dev/null +++ b/src/app/shared/file-download-link/file-download-link.component.html @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/app/shared/file-download-link/file-download-link.component.scss b/src/app/shared/file-download-link/file-download-link.component.scss new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/app/shared/file-download-link/file-download-link.component.spec.ts b/src/app/shared/file-download-link/file-download-link.component.spec.ts new file mode 100644 index 0000000000..7e1999816b --- /dev/null +++ b/src/app/shared/file-download-link/file-download-link.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { FileDownloadLinkComponent } from './file-download-link.component'; + +describe('FileDownloadLinkComponent', () => { + let component: FileDownloadLinkComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ FileDownloadLinkComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(FileDownloadLinkComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/shared/file-download-link/file-download-link.component.ts b/src/app/shared/file-download-link/file-download-link.component.ts new file mode 100644 index 0000000000..9df7c191ff --- /dev/null +++ b/src/app/shared/file-download-link/file-download-link.component.ts @@ -0,0 +1,48 @@ +import { Component, Input, OnInit } from '@angular/core'; +import { FileService } from '../../core/shared/file.service'; +import { Observable } from 'rxjs/internal/Observable'; +import { AuthService } from '../../core/auth/auth.service'; + +@Component({ + selector: 'ds-file-download-link', + templateUrl: './file-download-link.component.html', + styleUrls: ['./file-download-link.component.scss'] +}) +/** + * Component displaying a download link + * When the user is authenticated, a short-lived token retrieved from the REST API is added to the download link, + * 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 + */ + @Input() download: string; + + /** + * Whether or not the current user is authenticated + */ + isAuthenticated$: Observable; + + constructor(private fileService: FileService, + private authService: AuthService) { } + + ngOnInit() { + this.isAuthenticated$ = this.authService.isAuthenticated(); + } + + /** + * 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; + } + +} diff --git a/src/app/shared/shared.module.ts b/src/app/shared/shared.module.ts index 67d7db5c5d..09d090406a 100644 --- a/src/app/shared/shared.module.ts +++ b/src/app/shared/shared.module.ts @@ -202,6 +202,7 @@ import { ResourcePolicyTargetResolver } from './resource-policies/resolvers/reso import { ResourcePolicyResolver } from './resource-policies/resolvers/resource-policy.resolver'; import { EpersonSearchBoxComponent } from './resource-policies/form/eperson-group-list/eperson-search-box/eperson-search-box.component'; import { GroupSearchBoxComponent } from './resource-policies/form/eperson-group-list/group-search-box/group-search-box.component'; +import { FileDownloadLinkComponent } from './file-download-link/file-download-link.component'; const MODULES = [ // Do NOT include UniversalModule, HttpModule, or JsonpModule here @@ -386,7 +387,8 @@ const COMPONENTS = [ ResourcePolicyFormComponent, EpersonGroupListComponent, EpersonSearchBoxComponent, - GroupSearchBoxComponent + GroupSearchBoxComponent, + FileDownloadLinkComponent, ]; const ENTRY_COMPONENTS = [ @@ -459,7 +461,8 @@ const ENTRY_COMPONENTS = [ ClaimedTaskActionsApproveComponent, ClaimedTaskActionsRejectComponent, ClaimedTaskActionsReturnToPoolComponent, - ClaimedTaskActionsEditMetadataComponent + ClaimedTaskActionsEditMetadataComponent, + FileDownloadLinkComponent, ]; const SHARED_ITEM_PAGE_COMPONENTS = [