From b01af490a36d7294e441fd0b18b99111f7bbe818 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Mon, 5 Aug 2024 12:13:58 -0400 Subject: [PATCH] rework auto download service (#3990) --- .../search-results-row.component.ts | 13 +++--- .../toolbar/view-node/view-node.component.ts | 12 ++--- .../document-base-page.component.ts | 10 ++-- .../aca-file-auto-download.service.spec.ts | 46 ++++++------------- .../src/lib/services/app-settings.service.ts | 14 ++++++ ...ad.service.ts => auto-download.service.ts} | 30 +++++++----- projects/aca-shared/src/public-api.ts | 2 +- 7 files changed, 66 insertions(+), 61 deletions(-) rename projects/aca-shared/src/lib/services/{aca-file-auto-download.service.ts => auto-download.service.ts} (64%) diff --git a/projects/aca-content/src/lib/components/search/search-results-row/search-results-row.component.ts b/projects/aca-content/src/lib/components/search/search-results-row/search-results-row.component.ts index 01006ec508..7dffa6b21c 100644 --- a/projects/aca-content/src/lib/components/search/search-results-row/search-results-row.component.ts +++ b/projects/aca-content/src/lib/components/search/search-results-row/search-results-row.component.ts @@ -22,7 +22,7 @@ * from Hyland Software. If not, see . */ -import { Component, Input, OnInit, ViewEncapsulation, ChangeDetectionStrategy, OnDestroy } from '@angular/core'; +import { Component, Input, OnInit, ViewEncapsulation, ChangeDetectionStrategy, OnDestroy, inject } from '@angular/core'; import { NodeEntry, SearchEntryHighlight } from '@alfresco/js-api'; import { ViewNodeAction, NavigateToFolder } from '@alfresco/aca-shared/store'; import { Store } from '@ngrx/store'; @@ -30,7 +30,7 @@ import { BehaviorSubject, Subject } from 'rxjs'; import { NodesApiService } from '@alfresco/adf-content-services'; import { takeUntil } from 'rxjs/operators'; import { Router } from '@angular/router'; -import { AcaFileAutoDownloadService } from '@alfresco/aca-shared'; +import { AutoDownloadService, AppSettingsService } from '@alfresco/aca-shared'; import { CommonModule } from '@angular/common'; import { LocationLinkComponent } from '../../common/location-link/location-link.component'; import { MatDialogModule } from '@angular/material/dialog'; @@ -46,6 +46,8 @@ import { MatDialogModule } from '@angular/material/dialog'; host: { class: 'aca-search-results-row' } }) export class SearchResultsRowComponent implements OnInit, OnDestroy { + private settings = inject(AppSettingsService); + private readonly highlightPrefix = ""; private readonly highlightPostfix = ''; @@ -70,7 +72,7 @@ export class SearchResultsRowComponent implements OnInit, OnDestroy { private store: Store, private nodesApiService: NodesApiService, private router: Router, - private fileAutoDownloadService: AcaFileAutoDownloadService + private autoDownloadService: AutoDownloadService ) {} ngOnInit() { @@ -141,9 +143,8 @@ export class SearchResultsRowComponent implements OnInit, OnDestroy { showPreview(event: Event) { event.stopPropagation(); - if (this.fileAutoDownloadService.shouldFileAutoDownload(this.node.entry.content.sizeInBytes)) { - this.fileAutoDownloadService.autoDownloadFile(this.node); - } else { + + if (!this.settings.autoDownloadEnabled || !this.autoDownloadService.tryDownload(this.node, this.settings.authDownloadThreshold)) { this.store.dispatch(new ViewNodeAction(this.node.entry.id, { location: this.router.url })); } } diff --git a/projects/aca-content/src/lib/components/toolbar/view-node/view-node.component.ts b/projects/aca-content/src/lib/components/toolbar/view-node/view-node.component.ts index 10998441f0..7c0ad5a2ae 100644 --- a/projects/aca-content/src/lib/components/toolbar/view-node/view-node.component.ts +++ b/projects/aca-content/src/lib/components/toolbar/view-node/view-node.component.ts @@ -22,13 +22,13 @@ * from Hyland Software. If not, see . */ -import { Component, ViewEncapsulation, Input } from '@angular/core'; +import { Component, ViewEncapsulation, Input, inject } from '@angular/core'; import { Store } from '@ngrx/store'; import { AppStore, ViewNodeAction, getAppSelection } from '@alfresco/aca-shared/store'; import { ActivatedRoute, Router } from '@angular/router'; import { take } from 'rxjs/operators'; import { SharedLinkEntry } from '@alfresco/js-api'; -import { AcaFileAutoDownloadService } from '@alfresco/aca-shared'; +import { AutoDownloadService, AppSettingsService } from '@alfresco/aca-shared'; import { CommonModule } from '@angular/common'; import { TranslateModule } from '@ngx-translate/core'; import { MatButtonModule } from '@angular/material/button'; @@ -60,12 +60,14 @@ import { MatDialogModule } from '@angular/material/dialog'; host: { class: 'app-view-node' } }) export class ViewNodeComponent { + private settings = inject(AppSettingsService); + @Input() data: { title?: string; menuButton?: boolean; iconButton?: boolean }; constructor( private store: Store, private router: Router, - private fileAutoDownloadService: AcaFileAutoDownloadService, + private autoDownloadService: AutoDownloadService, private activatedRoute: ActivatedRoute ) {} @@ -74,9 +76,7 @@ export class ViewNodeComponent { .select(getAppSelection) .pipe(take(1)) .subscribe((selection) => { - if (this.fileAutoDownloadService.shouldFileAutoDownload(selection.file.entry?.content?.sizeInBytes)) { - this.fileAutoDownloadService.autoDownloadFile(selection.file); - } else { + if (!this.settings.autoDownloadEnabled || !this.autoDownloadService.tryDownload(selection.file, this.settings.authDownloadThreshold)) { let id: string; if (selection.file.entry.nodeType === 'app:filelink') { diff --git a/projects/aca-shared/src/lib/components/document-base-page/document-base-page.component.ts b/projects/aca-shared/src/lib/components/document-base-page/document-base-page.component.ts index e068149abf..dacb45ee77 100644 --- a/projects/aca-shared/src/lib/components/document-base-page/document-base-page.component.ts +++ b/projects/aca-shared/src/lib/components/document-base-page/document-base-page.component.ts @@ -43,9 +43,10 @@ import { } from '@alfresco/aca-shared/store'; import { AppExtensionService } from '../../services/app.extension.service'; import { isLibrary, isLocked } from '../../utils/node.utils'; -import { AcaFileAutoDownloadService } from '../../services/aca-file-auto-download.service'; +import { AutoDownloadService } from '../../services/auto-download.service'; import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'; import { Router } from '@angular/router'; +import { AppSettingsService } from '../../services/app-settings.service'; /* eslint-disable @angular-eslint/directive-class-suffix */ @Directive() @@ -70,13 +71,14 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges { isSmallScreen = false; selectedRowItemsCount = 0; + protected settings = inject(AppSettingsService); protected extensions = inject(AppExtensionService); protected content = inject(DocumentBasePageService); protected store = inject>(Store); protected breakpointObserver = inject(BreakpointObserver); protected uploadService = inject(UploadService); protected router = inject(Router); - private fileAutoDownloadService = inject(AcaFileAutoDownloadService, { optional: true }); + private autoDownloadService = inject(AutoDownloadService, { optional: true }); protected subscriptions: Subscription[] = []; @@ -144,9 +146,7 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges { showPreview(node: NodeEntry, extras?: ViewNodeExtras) { if (node?.entry) { - if (this.fileAutoDownloadService?.shouldFileAutoDownload(node.entry?.content?.sizeInBytes)) { - this.fileAutoDownloadService.autoDownloadFile(node); - } else { + if (!this.settings.autoDownloadEnabled || !this.autoDownloadService.tryDownload(node, this.settings.authDownloadThreshold)) { let id: string; if (node.entry.nodeType === 'app:filelink') { diff --git a/projects/aca-shared/src/lib/services/aca-file-auto-download.service.spec.ts b/projects/aca-shared/src/lib/services/aca-file-auto-download.service.spec.ts index f7e9b11654..399667e0e0 100644 --- a/projects/aca-shared/src/lib/services/aca-file-auto-download.service.spec.ts +++ b/projects/aca-shared/src/lib/services/aca-file-auto-download.service.spec.ts @@ -23,15 +23,13 @@ */ import { TestBed } from '@angular/core/testing'; -import { AcaFileAutoDownloadService, initialState, LibTestingModule } from '@alfresco/aca-shared'; +import { AutoDownloadService, initialState, LibTestingModule } from '@alfresco/aca-shared'; import { MatDialog } from '@angular/material/dialog'; -import { AppConfigService } from '@alfresco/adf-core'; import { FileAutoDownloadComponent } from '@alfresco/adf-content-services'; import { provideMockStore } from '@ngrx/store/testing'; describe('AcaFileAutoDownloadService', () => { - let service: AcaFileAutoDownloadService; - let appConfig: AppConfigService; + let service: AutoDownloadService; const mockDialogRef = { open: jasmine.createSpy('open') @@ -43,40 +41,24 @@ describe('AcaFileAutoDownloadService', () => { providers: [provideMockStore({ initialState }), { provide: MatDialog, useValue: mockDialogRef }] }); - service = TestBed.inject(AcaFileAutoDownloadService); - appConfig = TestBed.inject(AppConfigService); + service = TestBed.inject(AutoDownloadService); }); - it('shouldFileAutoDownload should return true if fileSize exceeds configured threshold and file auto download is enabled', () => { - appConfig.config.viewer = { - enableFileAutoDownload: true, - fileAutoDownloadSizeThresholdInMB: 10 - }; - const shouldAutDownloadFlag = service.shouldFileAutoDownload(11000000); - expect(shouldAutDownloadFlag).toBe(true); + it('tryDownload should return true if fileSize exceeds configured threshold and file auto tryDownload is enabled', () => { + const node = { entry: { content: { sizeInBytes: 11000000 } } } as any; + const result = service.tryDownload(node, 10); + expect(result).toBe(true); }); - it('shouldFileAutoDownload should return false if fileSize does not exceeds configured threshold and file auto download is enabled', () => { - appConfig.config.viewer = { - enableFileAutoDownload: true, - fileAutoDownloadSizeThresholdInMB: 10 - }; - const shouldAutDownloadFlag = service.shouldFileAutoDownload(500000); - expect(shouldAutDownloadFlag).toBe(false); + it('tryDownload should return false if fileSize does not exceeds configured threshold and file auto tryDownload is enabled', () => { + const node = { entry: { content: { sizeInBytes: 500000 } } } as any; + const result = service.tryDownload(node, 10); + expect(result).toBe(false); }); - it('shouldFileAutoDownload should return false if fileSize exceeds configured threshold but file auto download is disabled', () => { - appConfig.config.viewer = { - enableFileAutoDownload: false, - fileAutoDownloadSizeThresholdInMB: 10 - }; - const shouldAutDownloadFlag = service.shouldFileAutoDownload(11000000); - expect(shouldAutDownloadFlag).toBe(false); - }); - - it('autoDownloadFile should open FileAutoDownload dialog when called', () => { - const nodeEntity: any = { entry: { isFile: true } }; - service.autoDownloadFile(nodeEntity); + it('tryDownload should open the dialog when called', () => { + const nodeEntity: any = { entry: { isFile: true, content: { sizeInBytes: 11000000 } } }; + service.tryDownload(nodeEntity, 10); expect(mockDialogRef.open).toHaveBeenCalledWith(FileAutoDownloadComponent, { disableClose: true, data: nodeEntity }); }); }); diff --git a/projects/aca-shared/src/lib/services/app-settings.service.ts b/projects/aca-shared/src/lib/services/app-settings.service.ts index 4a37168d8d..5145a3b24e 100644 --- a/projects/aca-shared/src/lib/services/app-settings.service.ts +++ b/projects/aca-shared/src/lib/services/app-settings.service.ts @@ -97,4 +97,18 @@ export class AppSettingsService { } return result; } + + /** + * Gets the enablement of the file auto tryDownload feature from the app settings. + */ + get autoDownloadEnabled(): boolean { + return this.appConfig.get('viewer.enableFileAutoDownload', true); + } + + /** + * Gets the file auto tryDownload size threshold in MB from the app settings. + */ + get authDownloadThreshold(): number { + return this.appConfig.get('viewer.fileAutoDownloadSizeThresholdInMB', 15); + } } diff --git a/projects/aca-shared/src/lib/services/aca-file-auto-download.service.ts b/projects/aca-shared/src/lib/services/auto-download.service.ts similarity index 64% rename from projects/aca-shared/src/lib/services/aca-file-auto-download.service.ts rename to projects/aca-shared/src/lib/services/auto-download.service.ts index 1e10d6adf4..dbd7165dea 100644 --- a/projects/aca-shared/src/lib/services/aca-file-auto-download.service.ts +++ b/projects/aca-shared/src/lib/services/auto-download.service.ts @@ -22,9 +22,8 @@ * from Hyland Software. If not, see . */ -import { Injectable } from '@angular/core'; +import { inject, Injectable } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; -import { AppConfigService } from '@alfresco/adf-core'; import { NodeEntry } from '@alfresco/js-api'; import { FileAutoDownloadComponent } from '@alfresco/adf-content-services'; @@ -33,19 +32,28 @@ const BYTES_TO_MB_CONVERSION_VALUE = 1048576; @Injectable({ providedIn: 'root' }) -export class AcaFileAutoDownloadService { - constructor(private dialog: MatDialog, private appConfig: AppConfigService) {} +export class AutoDownloadService { + private dialog = inject(MatDialog); - public shouldFileAutoDownload(fileSizeInBytes: number): boolean { + private shouldDownload(node: NodeEntry, threshold: number): boolean { + const fileSizeInBytes = node?.entry?.content?.sizeInBytes || 0; const sizeInMB = fileSizeInBytes / BYTES_TO_MB_CONVERSION_VALUE; - const fileAutoDownloadFlag: boolean = this.appConfig.get('viewer.enableFileAutoDownload', true); - const sizeThreshold: number = this.appConfig.get('viewer.fileAutoDownloadSizeThresholdInMB', 15); - - return fileAutoDownloadFlag && sizeInMB && sizeInMB > sizeThreshold; + return sizeInMB && sizeInMB > threshold; } - public autoDownloadFile(node: NodeEntry) { - this.dialog.open(FileAutoDownloadComponent, { disableClose: true, data: node }); + /** + * Opens the dialog to download the node content. + * Determines whether node content should be auto downloaded based on the file size and the configured threshold. + * @param node node entry + * @param threshold file size threshold in MB + */ + tryDownload(node: NodeEntry, threshold: number): boolean { + if (this.shouldDownload(node, threshold)) { + this.dialog.open(FileAutoDownloadComponent, { disableClose: true, data: node }); + return true; + } + + return false; } } diff --git a/projects/aca-shared/src/public-api.ts b/projects/aca-shared/src/public-api.ts index 6ac2a49269..6f8d4aa336 100644 --- a/projects/aca-shared/src/public-api.ts +++ b/projects/aca-shared/src/public-api.ts @@ -55,7 +55,7 @@ export * from './lib/services/node-permission.service'; export * from './lib/services/app.extension.service'; export * from './lib/services/router.extension.service'; export * from './lib/services/app-hook.service'; -export * from './lib/services/aca-file-auto-download.service'; +export * from './lib/services/auto-download.service'; export * from './lib/services/app-settings.service'; export * from './lib/services/user-profile.service';