Skip to content

Commit c6c5bbd

Browse files
Show Download all as ZIP button only when all conditions are met (DSpace#606)
* Show download all in zip button only when all conditions are met * Updated test because of new updates
1 parent 8d4f32d commit c6c5bbd

3 files changed

Lines changed: 83 additions & 22 deletions

File tree

src/app/item-page/clarin-files-section/clarin-files-section.component.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ <h6><i class="fa fa-paperclip">&nbsp;</i>{{'item.page.files.head' | translate}}<
1313
<pre class="command-pre">{{ command }}</pre>
1414
</div>
1515
<span>
16-
<a *ngIf="canDownloadAllFiles" class="btn btn-download" id="download-all-button" (click)="downloadFiles()"
16+
<a *ngIf="
17+
(totalFileSizes | async) > (downloadZipMinFileSize | async) &&
18+
(totalFileSizes | async) < (downloadZipMaxFileSize | async) &&
19+
(listOfFiles | async).length >= (downloadZipMinFileCount | async)"
20+
class="btn btn-download" id="download-all-button" (click)="downloadFiles()"
1721
style="visibility: visible">
1822
<i style="display: block" class="fa fa-download fa-3x">&nbsp;</i>
19-
{{'item.page.download.button.all.files.zip' | translate}} ({{ totalFileSizes | dsFileSize }})
23+
{{'item.page.download.button.all.files.zip' | translate}} ({{ (totalFileSizes | async) | dsFileSize }})
2024
</a>
2125
</span>
2226
</div>

src/app/item-page/clarin-files-section/clarin-files-section.component.spec.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { TranslateModule } from '@ngx-translate/core';
99
import { MetadataBitstream } from '../../core/metadata/metadata-bitstream.model';
1010
import { ResourceType } from '../../core/shared/resource-type';
1111
import { HALLink } from '../../core/shared/hal-link.model';
12-
import { BehaviorSubject , of} from 'rxjs';
12+
import { BehaviorSubject , of } from 'rxjs';
13+
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
14+
import { Item } from '../../core/shared/item.model';
15+
import { createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
16+
import { createPaginatedList } from '../../shared/testing/utils.test';
1317

1418
describe('ClarinFilesSectionComponent', () => {
1519
let component: ClarinFilesSectionComponent;
@@ -38,6 +42,22 @@ describe('ClarinFilesSectionComponent', () => {
3842
const metadataBitstreams: MetadataBitstream[] = [metadatabitstream];
3943
const bitstreamStream = new BehaviorSubject(metadataBitstreams);
4044

45+
const mockItem: Item = Object.assign(new Item(), {
46+
bundles: createSuccessfulRemoteDataObject$(createPaginatedList([])),
47+
metadata: {
48+
'local.files.size': [
49+
{
50+
language: 'en_US',
51+
value: '123'
52+
}
53+
]
54+
}
55+
});
56+
57+
const configurationServiceSpy = jasmine.createSpyObj('configurationService', {
58+
findByPropertyName: of('123456'),
59+
});
60+
4161
beforeEach(async () => {
4262
mockRegistryService = jasmine.createSpyObj('RegistryService', {
4363
'getMetadataBitstream': of(bitstreamStream)
@@ -53,13 +73,15 @@ describe('ClarinFilesSectionComponent', () => {
5373
providers: [
5474
{ provide: RegistryService, useValue: mockRegistryService },
5575
{ provide: Router, useValue: new RouterMock() },
56-
{ provide: HALEndpointService, useValue: halService }
76+
{ provide: HALEndpointService, useValue: halService },
77+
{ provide: ConfigurationDataService, useValue: configurationServiceSpy },
5778
],
5879
})
5980
.compileComponents();
6081

6182
fixture = TestBed.createComponent(ClarinFilesSectionComponent);
6283
component = fixture.componentInstance;
84+
component.item = mockItem;
6385
fixture.detectChanges();
6486
});
6587

src/app/item-page/clarin-files-section/clarin-files-section.component.ts

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { Component, Input, OnInit } from '@angular/core';
22
import { Item } from '../../core/shared/item.model';
3-
import { getAllSucceededRemoteListPayload } from '../../core/shared/operators';
3+
import { getAllSucceededRemoteListPayload, getFirstSucceededRemoteDataPayload } from '../../core/shared/operators';
44
import { getItemPageRoute } from '../item-page-routing-paths';
55
import { MetadataBitstream } from '../../core/metadata/metadata-bitstream.model';
66
import { RegistryService } from '../../core/registry/registry.service';
77
import { Router } from '@angular/router';
88
import { HALEndpointService } from '../../core/shared/hal-endpoint.service';
9+
import { ConfigurationDataService } from '../../core/data/configuration-data.service';
10+
import { BehaviorSubject } from 'rxjs';
911

1012
@Component({
1113
selector: 'ds-clarin-files-section',
@@ -44,28 +46,48 @@ export class ClarinFilesSectionComponent implements OnInit {
4446
/**
4547
* total size of list of files uploaded by users to this item
4648
*/
47-
totalFileSizes: number;
49+
totalFileSizes: BehaviorSubject<number> = new BehaviorSubject<number>(-1);
4850

4951
/**
5052
* list of files uploaded by users to this item
5153
*/
52-
listOfFiles: MetadataBitstream[];
54+
listOfFiles: BehaviorSubject<MetadataBitstream[]> = new BehaviorSubject<MetadataBitstream[]>([]);
55+
56+
/**
57+
* min file size to show `Download all ZIP` button, this value is loaded from the configuration property
58+
* `download.all.alert.min.file.size`
59+
*/
60+
downloadZipMinFileSize: BehaviorSubject<number> = new BehaviorSubject<number>(-1);
61+
62+
/**
63+
* max file size to show `Download all ZIP` button, this value is loaded from the configuration property
64+
* `download.all.limit.max.file.size`
65+
*/
66+
downloadZipMaxFileSize: BehaviorSubject<number> = new BehaviorSubject<number>(-1);
67+
68+
/**
69+
* min count of files to show `Download all ZIP` button, this value is loaded from the configuration property
70+
* `download.all.limit.min.file.count`
71+
*/
72+
downloadZipMinFileCount: BehaviorSubject<number> = new BehaviorSubject<number>(-1);
5373

5474

5575
constructor(protected registryService: RegistryService,
5676
protected router: Router,
57-
protected halService: HALEndpointService) {
77+
protected halService: HALEndpointService,
78+
protected configurationService: ConfigurationDataService) {
5879
}
5980

6081
ngOnInit(): void {
6182
this.registryService
6283
.getMetadataBitstream(this.itemHandle, 'ORIGINAL,TEXT,THUMBNAIL')
6384
.pipe(getAllSucceededRemoteListPayload())
6485
.subscribe((data: MetadataBitstream[]) => {
65-
this.listOfFiles = data;
86+
this.listOfFiles.next(data);
6687
this.generateCurlCommand();
67-
this.sumFileSizes();
6888
});
89+
this.totalFileSizes.next(Number(this.item.firstMetadataValue('local.files.size')));
90+
this.loadDownloadZipConfigProperties();
6991
}
7092

7193
setCommandline() {
@@ -77,12 +99,7 @@ export class ClarinFilesSectionComponent implements OnInit {
7799
}
78100

79101
generateCurlCommand() {
80-
const fileNames = this.listOfFiles.map((file: MetadataBitstream) => {
81-
// Show `Download All Files` only if there are more files.
82-
if (this.listOfFiles.length > 1) {
83-
this.canDownloadAllFiles = true;
84-
}
85-
102+
const fileNames = this.listOfFiles.value.map((file: MetadataBitstream) => {
86103
if (file.canPreview) {
87104
this.canShowCurlDownload = true;
88105
}
@@ -95,11 +112,29 @@ export class ClarinFilesSectionComponent implements OnInit {
95112
}/{${fileNames.join(',')}}`;
96113
}
97114

98-
sumFileSizes() {
99-
let totalBytes = 0;
100-
this.listOfFiles.forEach((file) => {
101-
totalBytes += file.fileSize;
102-
});
103-
this.totalFileSizes = totalBytes;
115+
loadDownloadZipConfigProperties() {
116+
this.configurationService.findByPropertyName('download.all.limit.min.file.count')
117+
.pipe(
118+
getFirstSucceededRemoteDataPayload()
119+
)
120+
.subscribe((config) => {
121+
this.downloadZipMinFileCount.next(Number(config.values[0]));
122+
});
123+
124+
this.configurationService.findByPropertyName('download.all.limit.max.file.size')
125+
.pipe(
126+
getFirstSucceededRemoteDataPayload()
127+
)
128+
.subscribe((config) => {
129+
this.downloadZipMaxFileSize.next(Number(config.values[0]));
130+
});
131+
132+
this.configurationService.findByPropertyName('download.all.alert.min.file.size')
133+
.pipe(
134+
getFirstSucceededRemoteDataPayload()
135+
)
136+
.subscribe((config) => {
137+
this.downloadZipMinFileSize.next(Number(config.values[0]));
138+
});
104139
}
105140
}

0 commit comments

Comments
 (0)