71504: FileDownloadLinkComponent

This commit is contained in:
Kristof De Langhe
2020-06-22 11:40:39 +02:00
parent bbaaaed4b5
commit b578aa408b
7 changed files with 88 additions and 6 deletions

View File

@@ -21,9 +21,9 @@
</dl> </dl>
</div> </div>
<div class="col-2"> <div class="col-2">
<a [href]="file._links.content.href" [download]="file.name"> <ds-file-download-link [href]="file._links.content.href" [download]="file.name">
{{"item.page.filesection.download" | translate}} {{"item.page.filesection.download" | translate}}
</a> </ds-file-download-link>
</div> </div>
</div> </div>
</ds-metadata-field-wrapper> </ds-metadata-field-wrapper>

View File

@@ -1,11 +1,11 @@
<ng-container *ngVar="(bitstreams$ | async) as bitstreams"> <ng-container *ngVar="(bitstreams$ | async) as bitstreams">
<ds-metadata-field-wrapper *ngIf="bitstreams?.length > 0" [label]="label | translate"> <ds-metadata-field-wrapper *ngIf="bitstreams?.length > 0" [label]="label | translate">
<div class="file-section"> <div class="file-section">
<a *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;" [href]="file?._links.content.href" [download]="file?.name">
<span>{{file?.name}}</span> <span>{{file?.name}}</span>
<span>({{(file?.sizeBytes) | dsFileSize }})</span> <span>({{(file?.sizeBytes) | dsFileSize }})</span>
<span *ngIf="!last" innerHTML="{{separator}}"></span> <span *ngIf="!last" innerHTML="{{separator}}"></span>
</a> </ds-file-download-link>
</div> </div>
</ds-metadata-field-wrapper> </ds-metadata-field-wrapper>
</ng-container> </ng-container>

View File

@@ -0,0 +1,6 @@
<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>
<ng-template #content>
<ng-content></ng-content>
</ng-template>

View File

@@ -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<FileDownloadLinkComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ FileDownloadLinkComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FileDownloadLinkComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -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<boolean>;
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;
}
}

View File

@@ -202,6 +202,7 @@ import { ResourcePolicyTargetResolver } from './resource-policies/resolvers/reso
import { ResourcePolicyResolver } from './resource-policies/resolvers/resource-policy.resolver'; 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 { 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 { 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 = [ const MODULES = [
// Do NOT include UniversalModule, HttpModule, or JsonpModule here // Do NOT include UniversalModule, HttpModule, or JsonpModule here
@@ -386,7 +387,8 @@ const COMPONENTS = [
ResourcePolicyFormComponent, ResourcePolicyFormComponent,
EpersonGroupListComponent, EpersonGroupListComponent,
EpersonSearchBoxComponent, EpersonSearchBoxComponent,
GroupSearchBoxComponent GroupSearchBoxComponent,
FileDownloadLinkComponent,
]; ];
const ENTRY_COMPONENTS = [ const ENTRY_COMPONENTS = [
@@ -459,7 +461,8 @@ const ENTRY_COMPONENTS = [
ClaimedTaskActionsApproveComponent, ClaimedTaskActionsApproveComponent,
ClaimedTaskActionsRejectComponent, ClaimedTaskActionsRejectComponent,
ClaimedTaskActionsReturnToPoolComponent, ClaimedTaskActionsReturnToPoolComponent,
ClaimedTaskActionsEditMetadataComponent ClaimedTaskActionsEditMetadataComponent,
FileDownloadLinkComponent,
]; ];
const SHARED_ITEM_PAGE_COMPONENTS = [ const SHARED_ITEM_PAGE_COMPONENTS = [