rework auto download service (#3990)

This commit is contained in:
Denys Vuika
2024-08-05 12:13:58 -04:00
committed by GitHub
parent 56912cbd35
commit b01af490a3
7 changed files with 66 additions and 61 deletions

View File

@@ -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<AppStore>>(Store<AppStore>);
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') {

View File

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

View File

@@ -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<boolean>('viewer.enableFileAutoDownload', true);
}
/**
* Gets the file auto tryDownload size threshold in MB from the app settings.
*/
get authDownloadThreshold(): number {
return this.appConfig.get<number>('viewer.fileAutoDownloadSizeThresholdInMB', 15);
}
}

View File

@@ -22,9 +22,8 @@
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@@ -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';