mirror of
https://github.com/Alfresco/alfresco-content-app.git
synced 2025-07-24 17:31:52 +00:00
rework auto download service (#3990)
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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 { NodeEntry, SearchEntryHighlight } from '@alfresco/js-api';
|
||||||
import { ViewNodeAction, NavigateToFolder } from '@alfresco/aca-shared/store';
|
import { ViewNodeAction, NavigateToFolder } from '@alfresco/aca-shared/store';
|
||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
@@ -30,7 +30,7 @@ import { BehaviorSubject, Subject } from 'rxjs';
|
|||||||
import { NodesApiService } from '@alfresco/adf-content-services';
|
import { NodesApiService } from '@alfresco/adf-content-services';
|
||||||
import { takeUntil } from 'rxjs/operators';
|
import { takeUntil } from 'rxjs/operators';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { AcaFileAutoDownloadService } from '@alfresco/aca-shared';
|
import { AutoDownloadService, AppSettingsService } from '@alfresco/aca-shared';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
import { LocationLinkComponent } from '../../common/location-link/location-link.component';
|
import { LocationLinkComponent } from '../../common/location-link/location-link.component';
|
||||||
import { MatDialogModule } from '@angular/material/dialog';
|
import { MatDialogModule } from '@angular/material/dialog';
|
||||||
@@ -46,6 +46,8 @@ import { MatDialogModule } from '@angular/material/dialog';
|
|||||||
host: { class: 'aca-search-results-row' }
|
host: { class: 'aca-search-results-row' }
|
||||||
})
|
})
|
||||||
export class SearchResultsRowComponent implements OnInit, OnDestroy {
|
export class SearchResultsRowComponent implements OnInit, OnDestroy {
|
||||||
|
private settings = inject(AppSettingsService);
|
||||||
|
|
||||||
private readonly highlightPrefix = "<span class='aca-highlight'>";
|
private readonly highlightPrefix = "<span class='aca-highlight'>";
|
||||||
private readonly highlightPostfix = '</span>';
|
private readonly highlightPostfix = '</span>';
|
||||||
|
|
||||||
@@ -70,7 +72,7 @@ export class SearchResultsRowComponent implements OnInit, OnDestroy {
|
|||||||
private store: Store<any>,
|
private store: Store<any>,
|
||||||
private nodesApiService: NodesApiService,
|
private nodesApiService: NodesApiService,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private fileAutoDownloadService: AcaFileAutoDownloadService
|
private autoDownloadService: AutoDownloadService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
@@ -141,9 +143,8 @@ export class SearchResultsRowComponent implements OnInit, OnDestroy {
|
|||||||
|
|
||||||
showPreview(event: Event) {
|
showPreview(event: Event) {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
if (this.fileAutoDownloadService.shouldFileAutoDownload(this.node.entry.content.sizeInBytes)) {
|
|
||||||
this.fileAutoDownloadService.autoDownloadFile(this.node);
|
if (!this.settings.autoDownloadEnabled || !this.autoDownloadService.tryDownload(this.node, this.settings.authDownloadThreshold)) {
|
||||||
} else {
|
|
||||||
this.store.dispatch(new ViewNodeAction(this.node.entry.id, { location: this.router.url }));
|
this.store.dispatch(new ViewNodeAction(this.node.entry.id, { location: this.router.url }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,13 +22,13 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Component, ViewEncapsulation, Input } from '@angular/core';
|
import { Component, ViewEncapsulation, Input, inject } from '@angular/core';
|
||||||
import { Store } from '@ngrx/store';
|
import { Store } from '@ngrx/store';
|
||||||
import { AppStore, ViewNodeAction, getAppSelection } from '@alfresco/aca-shared/store';
|
import { AppStore, ViewNodeAction, getAppSelection } from '@alfresco/aca-shared/store';
|
||||||
import { ActivatedRoute, Router } from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
import { take } from 'rxjs/operators';
|
import { take } from 'rxjs/operators';
|
||||||
import { SharedLinkEntry } from '@alfresco/js-api';
|
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 { CommonModule } from '@angular/common';
|
||||||
import { TranslateModule } from '@ngx-translate/core';
|
import { TranslateModule } from '@ngx-translate/core';
|
||||||
import { MatButtonModule } from '@angular/material/button';
|
import { MatButtonModule } from '@angular/material/button';
|
||||||
@@ -60,12 +60,14 @@ import { MatDialogModule } from '@angular/material/dialog';
|
|||||||
host: { class: 'app-view-node' }
|
host: { class: 'app-view-node' }
|
||||||
})
|
})
|
||||||
export class ViewNodeComponent {
|
export class ViewNodeComponent {
|
||||||
|
private settings = inject(AppSettingsService);
|
||||||
|
|
||||||
@Input() data: { title?: string; menuButton?: boolean; iconButton?: boolean };
|
@Input() data: { title?: string; menuButton?: boolean; iconButton?: boolean };
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private store: Store<AppStore>,
|
private store: Store<AppStore>,
|
||||||
private router: Router,
|
private router: Router,
|
||||||
private fileAutoDownloadService: AcaFileAutoDownloadService,
|
private autoDownloadService: AutoDownloadService,
|
||||||
private activatedRoute: ActivatedRoute
|
private activatedRoute: ActivatedRoute
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@@ -74,9 +76,7 @@ export class ViewNodeComponent {
|
|||||||
.select(getAppSelection)
|
.select(getAppSelection)
|
||||||
.pipe(take(1))
|
.pipe(take(1))
|
||||||
.subscribe((selection) => {
|
.subscribe((selection) => {
|
||||||
if (this.fileAutoDownloadService.shouldFileAutoDownload(selection.file.entry?.content?.sizeInBytes)) {
|
if (!this.settings.autoDownloadEnabled || !this.autoDownloadService.tryDownload(selection.file, this.settings.authDownloadThreshold)) {
|
||||||
this.fileAutoDownloadService.autoDownloadFile(selection.file);
|
|
||||||
} else {
|
|
||||||
let id: string;
|
let id: string;
|
||||||
|
|
||||||
if (selection.file.entry.nodeType === 'app:filelink') {
|
if (selection.file.entry.nodeType === 'app:filelink') {
|
||||||
|
@@ -43,9 +43,10 @@ import {
|
|||||||
} from '@alfresco/aca-shared/store';
|
} from '@alfresco/aca-shared/store';
|
||||||
import { AppExtensionService } from '../../services/app.extension.service';
|
import { AppExtensionService } from '../../services/app.extension.service';
|
||||||
import { isLibrary, isLocked } from '../../utils/node.utils';
|
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 { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
|
import { AppSettingsService } from '../../services/app-settings.service';
|
||||||
|
|
||||||
/* eslint-disable @angular-eslint/directive-class-suffix */
|
/* eslint-disable @angular-eslint/directive-class-suffix */
|
||||||
@Directive()
|
@Directive()
|
||||||
@@ -70,13 +71,14 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges {
|
|||||||
isSmallScreen = false;
|
isSmallScreen = false;
|
||||||
selectedRowItemsCount = 0;
|
selectedRowItemsCount = 0;
|
||||||
|
|
||||||
|
protected settings = inject(AppSettingsService);
|
||||||
protected extensions = inject(AppExtensionService);
|
protected extensions = inject(AppExtensionService);
|
||||||
protected content = inject(DocumentBasePageService);
|
protected content = inject(DocumentBasePageService);
|
||||||
protected store = inject<Store<AppStore>>(Store<AppStore>);
|
protected store = inject<Store<AppStore>>(Store<AppStore>);
|
||||||
protected breakpointObserver = inject(BreakpointObserver);
|
protected breakpointObserver = inject(BreakpointObserver);
|
||||||
protected uploadService = inject(UploadService);
|
protected uploadService = inject(UploadService);
|
||||||
protected router = inject(Router);
|
protected router = inject(Router);
|
||||||
private fileAutoDownloadService = inject(AcaFileAutoDownloadService, { optional: true });
|
private autoDownloadService = inject(AutoDownloadService, { optional: true });
|
||||||
|
|
||||||
protected subscriptions: Subscription[] = [];
|
protected subscriptions: Subscription[] = [];
|
||||||
|
|
||||||
@@ -144,9 +146,7 @@ export abstract class PageComponent implements OnInit, OnDestroy, OnChanges {
|
|||||||
|
|
||||||
showPreview(node: NodeEntry, extras?: ViewNodeExtras) {
|
showPreview(node: NodeEntry, extras?: ViewNodeExtras) {
|
||||||
if (node?.entry) {
|
if (node?.entry) {
|
||||||
if (this.fileAutoDownloadService?.shouldFileAutoDownload(node.entry?.content?.sizeInBytes)) {
|
if (!this.settings.autoDownloadEnabled || !this.autoDownloadService.tryDownload(node, this.settings.authDownloadThreshold)) {
|
||||||
this.fileAutoDownloadService.autoDownloadFile(node);
|
|
||||||
} else {
|
|
||||||
let id: string;
|
let id: string;
|
||||||
|
|
||||||
if (node.entry.nodeType === 'app:filelink') {
|
if (node.entry.nodeType === 'app:filelink') {
|
||||||
|
@@ -23,15 +23,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { TestBed } from '@angular/core/testing';
|
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 { MatDialog } from '@angular/material/dialog';
|
||||||
import { AppConfigService } from '@alfresco/adf-core';
|
|
||||||
import { FileAutoDownloadComponent } from '@alfresco/adf-content-services';
|
import { FileAutoDownloadComponent } from '@alfresco/adf-content-services';
|
||||||
import { provideMockStore } from '@ngrx/store/testing';
|
import { provideMockStore } from '@ngrx/store/testing';
|
||||||
|
|
||||||
describe('AcaFileAutoDownloadService', () => {
|
describe('AcaFileAutoDownloadService', () => {
|
||||||
let service: AcaFileAutoDownloadService;
|
let service: AutoDownloadService;
|
||||||
let appConfig: AppConfigService;
|
|
||||||
|
|
||||||
const mockDialogRef = {
|
const mockDialogRef = {
|
||||||
open: jasmine.createSpy('open')
|
open: jasmine.createSpy('open')
|
||||||
@@ -43,40 +41,24 @@ describe('AcaFileAutoDownloadService', () => {
|
|||||||
providers: [provideMockStore({ initialState }), { provide: MatDialog, useValue: mockDialogRef }]
|
providers: [provideMockStore({ initialState }), { provide: MatDialog, useValue: mockDialogRef }]
|
||||||
});
|
});
|
||||||
|
|
||||||
service = TestBed.inject(AcaFileAutoDownloadService);
|
service = TestBed.inject(AutoDownloadService);
|
||||||
appConfig = TestBed.inject(AppConfigService);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('shouldFileAutoDownload should return true if fileSize exceeds configured threshold and file auto download is enabled', () => {
|
it('tryDownload should return true if fileSize exceeds configured threshold and file auto tryDownload is enabled', () => {
|
||||||
appConfig.config.viewer = {
|
const node = { entry: { content: { sizeInBytes: 11000000 } } } as any;
|
||||||
enableFileAutoDownload: true,
|
const result = service.tryDownload(node, 10);
|
||||||
fileAutoDownloadSizeThresholdInMB: 10
|
expect(result).toBe(true);
|
||||||
};
|
|
||||||
const shouldAutDownloadFlag = service.shouldFileAutoDownload(11000000);
|
|
||||||
expect(shouldAutDownloadFlag).toBe(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('shouldFileAutoDownload should return false if fileSize does not exceeds configured threshold and file auto download is enabled', () => {
|
it('tryDownload should return false if fileSize does not exceeds configured threshold and file auto tryDownload is enabled', () => {
|
||||||
appConfig.config.viewer = {
|
const node = { entry: { content: { sizeInBytes: 500000 } } } as any;
|
||||||
enableFileAutoDownload: true,
|
const result = service.tryDownload(node, 10);
|
||||||
fileAutoDownloadSizeThresholdInMB: 10
|
expect(result).toBe(false);
|
||||||
};
|
|
||||||
const shouldAutDownloadFlag = service.shouldFileAutoDownload(500000);
|
|
||||||
expect(shouldAutDownloadFlag).toBe(false);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('shouldFileAutoDownload should return false if fileSize exceeds configured threshold but file auto download is disabled', () => {
|
it('tryDownload should open the dialog when called', () => {
|
||||||
appConfig.config.viewer = {
|
const nodeEntity: any = { entry: { isFile: true, content: { sizeInBytes: 11000000 } } };
|
||||||
enableFileAutoDownload: false,
|
service.tryDownload(nodeEntity, 10);
|
||||||
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);
|
|
||||||
expect(mockDialogRef.open).toHaveBeenCalledWith(FileAutoDownloadComponent, { disableClose: true, data: nodeEntity });
|
expect(mockDialogRef.open).toHaveBeenCalledWith(FileAutoDownloadComponent, { disableClose: true, data: nodeEntity });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -97,4 +97,18 @@ export class AppSettingsService {
|
|||||||
}
|
}
|
||||||
return result;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -22,9 +22,8 @@
|
|||||||
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
|
* 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 { MatDialog } from '@angular/material/dialog';
|
||||||
import { AppConfigService } from '@alfresco/adf-core';
|
|
||||||
import { NodeEntry } from '@alfresco/js-api';
|
import { NodeEntry } from '@alfresco/js-api';
|
||||||
import { FileAutoDownloadComponent } from '@alfresco/adf-content-services';
|
import { FileAutoDownloadComponent } from '@alfresco/adf-content-services';
|
||||||
|
|
||||||
@@ -33,19 +32,28 @@ const BYTES_TO_MB_CONVERSION_VALUE = 1048576;
|
|||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root'
|
providedIn: 'root'
|
||||||
})
|
})
|
||||||
export class AcaFileAutoDownloadService {
|
export class AutoDownloadService {
|
||||||
constructor(private dialog: MatDialog, private appConfig: AppConfigService) {}
|
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 sizeInMB = fileSizeInBytes / BYTES_TO_MB_CONVERSION_VALUE;
|
||||||
|
|
||||||
const fileAutoDownloadFlag: boolean = this.appConfig.get('viewer.enableFileAutoDownload', true);
|
return sizeInMB && sizeInMB > threshold;
|
||||||
const sizeThreshold: number = this.appConfig.get('viewer.fileAutoDownloadSizeThresholdInMB', 15);
|
|
||||||
|
|
||||||
return fileAutoDownloadFlag && sizeInMB && sizeInMB > sizeThreshold;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public autoDownloadFile(node: NodeEntry) {
|
/**
|
||||||
|
* 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 });
|
this.dialog.open(FileAutoDownloadComponent, { disableClose: true, data: node });
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -55,7 +55,7 @@ export * from './lib/services/node-permission.service';
|
|||||||
export * from './lib/services/app.extension.service';
|
export * from './lib/services/app.extension.service';
|
||||||
export * from './lib/services/router.extension.service';
|
export * from './lib/services/router.extension.service';
|
||||||
export * from './lib/services/app-hook.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/app-settings.service';
|
||||||
export * from './lib/services/user-profile.service';
|
export * from './lib/services/user-profile.service';
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user