remove multiple calls to stampPdf

This commit is contained in:
Dan Percic 2022-03-11 12:44:12 +02:00
parent 6b38de9892
commit 5938fb19f7
12 changed files with 57 additions and 58 deletions

View File

@ -13,7 +13,7 @@ import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '@environments/environment';
import { AuthModule } from './modules/auth/auth.module';
import { AuthErrorComponent } from '@components/auth-error/auth-error.component';
import { HttpCacheInterceptor } from '@redaction/red-cache';
import { HttpCacheInterceptor } from '@red/cache';
import { NotificationsComponent } from '@components/notifications/notifications.component';
import { DownloadsListScreenComponent } from '@components/downloads-list-screen/downloads-list-screen.component';
import { AppRoutingModule } from './app-routing.module';

View File

@ -66,7 +66,7 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha
@Output() readonly manualAnnotationRequested = new EventEmitter<ManualRedactionEntryWrapper>();
@Output() readonly pageChanged = new EventEmitter<number>();
@Output() readonly keyUp = new EventEmitter<KeyboardEvent>();
@Output() readonly viewerReady = new EventEmitter<void>();
@Output() readonly viewerReady = new EventEmitter<WebViewerInstance>();
@Output() readonly annotationsChanged = new EventEmitter<AnnotationWrapper>();
@ViewChild('viewer', { static: true }) viewer: ElementRef;
@ViewChild('compareFileInput', { static: true }) compareFileInput: ElementRef;
@ -691,8 +691,7 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha
private _setReadyAndInitialState(): void {
this._ngZone.run(() => {
this.pdf.ready = true;
this.viewerReady.emit();
this.viewerReady.emit(this.instance);
const routePageNumber: number = this._activatedRoute.snapshot.queryParams.page;
this.pageChanged.emit(routePageNumber || 1);
this._setInitialDisplayMode();

View File

@ -397,10 +397,11 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni
this._changeDetectorRef.markForCheck();
}
@Debounce()
async viewerReady() {
this.ready = true;
this._pdf.ready = true;
await this._stampPDF();
await this._reloadAnnotations();
this._setExcludedPageStyles();
@ -477,17 +478,18 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni
}
private async _stampPDF() {
if (!this._pdf.ready) {
const pdfDoc = await this._pdf.documentViewer.getDocument().getPDFDoc();
const file = await this.stateService.file;
const allPages = [...Array(file.numberOfPages).keys()].map(page => page + 1);
if (!pdfDoc || !this._pdf.ready) {
return;
}
const pdfDoc = await this._pdf.documentViewer.getDocument().getPDFDoc();
const file = this._filesMapService.get(this.dossierId, this.fileId);
const allPages = [...Array(file.numberOfPages).keys()].map(page => page + 1);
await clearStamps(pdfDoc, this._pdf.PDFNet, allPages);
if (this._viewModeService.isRedacted) {
const dossier = this._dossiersService.find(this.dossierId);
const dossier = await this.stateService.dossier;
if (dossier.watermarkPreviewEnabled) {
await this._stampPreview(pdfDoc, dossier.dossierTemplateId);
}
@ -533,7 +535,6 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni
private async _fileUpdated(file: File): Promise<void> {
await this._loadFileData(file);
await this._reloadAnnotations();
await this._stampPDF();
}
private _subscribeToFileUpdates(): void {
@ -573,7 +574,8 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni
private async _loadFileData(file: File): Promise<void | boolean> {
if (!file || file.isError) {
return this._router.navigate([this._dossiersService.find(this.dossierId).routerLink]);
const dossier = await this.stateService.dossier;
return this._router.navigate([dossier.routerLink]);
}
if (file.isUnprocessed) {
@ -638,6 +640,7 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni
if (currentFilters) {
this._handleDeltaAnnotationFilters(currentFilters, this.visibleAnnotations);
}
await this._annotationDrawService.drawAnnotations(newAnnotations);
console.log(`[REDACTION] Annotations redraw time: ${new Date().getTime() - startTime} ms for ${newAnnotations.length} annotations`);
}

View File

@ -33,19 +33,13 @@ export class AnnotationDrawService {
private readonly _viewModeService: ViewModeService,
) {}
async drawAnnotations(annotationWrappers: AnnotationWrapper[]) {
drawAnnotations(annotationWrappers: AnnotationWrapper[]) {
if (!this._pdf.instance || !this._pdf.ready) {
return;
}
const pdfNet = this._pdf.instance.Core.PDFNet;
await pdfNet.runWithCleanup(
async () => {
await this._drawAnnotations(annotationWrappers);
},
environment.licenseKey ? atob(environment.licenseKey) : null,
);
const licenceKey = environment.licenseKey ? atob(environment.licenseKey) : null;
return this._pdf.PDFNet.runWithCleanup(() => this._drawAnnotations(annotationWrappers), licenceKey);
}
getColor(superType: string, dictionary?: string) {
@ -92,10 +86,13 @@ export class AnnotationDrawService {
}
private async _drawAnnotations(annotationWrappers: AnnotationWrapper[]) {
if (!this._pdf.ready) {
return;
}
const annotations = annotationWrappers.map(annotation => this._computeAnnotation(annotation)).filter(a => !!a);
const annotationManager = this._pdf.annotationManager;
annotationManager.addAnnotations(annotations, { imported: true });
await annotationManager.drawAnnotationsFromList(annotations);
this._pdf.instance.Core.annotationManager.addAnnotations(annotations, { imported: true });
await this._pdf.instance.Core.annotationManager.drawAnnotationsFromList(annotations);
if (this._userPreferenceService.areDevFeaturesEnabled) {
const { dossierId, fileId } = this._state;

View File

@ -11,7 +11,7 @@ import { FileManagementService } from '@services/entity-services/file-management
import { DOSSIER_ID, FILE_ID } from '@utils/constants';
import { DossiersService } from '../../../../../services/dossiers/dossiers.service';
import { dossiersServiceResolver } from '@services/entity-services/dossiers.service.provider';
import { wipeFilesCache } from '../../../../../../../../../libs/red-cache/src';
import { wipeFilesCache } from '@red/cache';
@Injectable()
export class FilePreviewStateService {

View File

@ -60,7 +60,7 @@ export class PdfViewer {
return this.instance.Core.Annotations;
}
get PDFNet() {
get PDFNet(): typeof Core.PDFNet {
return this.instance.Core.PDFNet;
}

View File

@ -3,7 +3,7 @@ import { HttpClient } from '@angular/common/http';
import { Title } from '@angular/platform-browser';
import packageInfo from '../../../../../package.json';
import config from '../../assets/config/config.json';
import { CacheApiService, wipeCaches } from '@redaction/red-cache';
import { CacheApiService, wipeCaches } from '@red/cache';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
@ -13,8 +13,6 @@ const version = packageInfo.version;
providedIn: 'root',
})
export class ConfigService {
private _values = { ...config, FRONTEND_APP_VERSION: version } as const;
constructor(
private readonly _httpClient: HttpClient,
private readonly _cacheApiService: CacheApiService,
@ -23,6 +21,12 @@ export class ConfigService {
this._checkFrontendVersion();
}
private _values = { ...config, FRONTEND_APP_VERSION: version } as const;
get values() {
return this._values;
}
loadAppConfig(): Observable<any> {
return this._httpClient.get<any>('/assets/config/config.json').pipe(
tap(envConfig => {
@ -32,6 +36,11 @@ export class ConfigService {
);
}
updateDisplayName(name: string): void {
this._values = { ...this._values, APP_NAME: name } as const;
this._titleService.setTitle(this._values.APP_NAME || 'RedactManager');
}
private _checkFrontendVersion(): void {
this._cacheApiService.getCachedValue('FRONTEND_APP_VERSION').then(async lastVersion => {
console.log('[REDACTION] Last app version: ', lastVersion, ' current version ', version);
@ -42,13 +51,4 @@ export class ConfigService {
await this._cacheApiService.cacheValue('FRONTEND_APP_VERSION', version);
});
}
get values() {
return this._values;
}
updateDisplayName(name: string): void {
this._values = { ...this._values, APP_NAME: name } as const;
this._titleService.setTitle(this._values.APP_NAME || 'RedactManager');
}
}

View File

@ -2,7 +2,7 @@ import { Inject, Injectable, Injector } from '@angular/core';
import { KeycloakService } from 'keycloak-angular';
import jwt_decode from 'jwt-decode';
import { ICreateUserRequest, IMyProfileUpdateRequest, IProfileUpdateRequest, IResetPasswordRequest, IUser, User } from '@red/domain';
import { wipeCaches } from '@redaction/red-cache';
import { wipeCaches } from '@red/cache';
import { BASE_HREF } from '../tokens';
import { BehaviorSubject, firstValueFrom, Observable } from 'rxjs';
import { EntitiesService, List, mapEach, QueryParam, RequiredParam, Validate } from '@iqser/common-ui';

View File

@ -3,7 +3,7 @@ import { environment } from '@environments/environment';
import { Core } from '@pdftron/webviewer';
import PDFDoc = Core.PDFNet.PDFDoc;
async function createPageSet(pdfNet: any, pages: number[]) {
async function createPageSet(pdfNet: typeof Core.PDFNet, pages: number[]) {
const pageSet = await pdfNet.PageSet.create();
for (const page of pages) {
if (page > 0) {
@ -25,7 +25,7 @@ function convertFont(type: string): number {
return 4;
}
export async function clearStamps(document: PDFDoc, pdfNet: any, pages: number[]) {
export async function clearStamps(document: PDFDoc, pdfNet: typeof Core.PDFNet, pages: number[]) {
await pdfNet.runWithCleanup(
async () => {
await document.lock();

View File

@ -5,7 +5,7 @@ import { from, Observable, throwError } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
providedIn: 'root',
})
export class CacheApiService {
constructor() {
@ -49,8 +49,8 @@ export class CacheApiService {
return this._toHttpResponse(response);
}
}
return throwError(new Error('Request not Cached'));
})
return throwError(() => new Error('Request not Cached'));
}),
);
}
@ -61,8 +61,8 @@ export class CacheApiService {
const expires = new Date().getTime() + ttl * 1000;
const response = new Response(string, {
headers: {
_expires: `${expires}`
}
_expires: `${expires}`,
},
});
const request = new Request(name);
// console.log('should cache', valueReference, string, response);
@ -110,7 +110,7 @@ export class CacheApiService {
} catch (e) {}
}
return undefined;
})
}),
);
} else {
return Promise.resolve(undefined);
@ -118,13 +118,13 @@ export class CacheApiService {
}
_buildUrl(request: HttpRequest<any>) {
let url;
let url: string;
if (request.method === 'GET') {
url = request.urlWithParams;
}
if (request.method === 'POST') {
const body = request.body;
let hash;
let hash: string;
if (Array.isArray(body)) {
hash = JSON.stringify(body.sort());
} else {
@ -202,9 +202,9 @@ export class CacheApiService {
status: response.status,
statusText: response.statusText,
url: response.url,
headers: this._toHttpHeaders(response.headers)
})
)
headers: this._toHttpHeaders(response.headers),
}),
),
);
}
@ -215,8 +215,8 @@ export class CacheApiService {
status: httpResponse.status,
statusText: httpResponse.statusText,
headers: {
_expires: undefined
}
_expires: undefined,
},
};
httpResponse.headers.keys().forEach(key => {

View File

@ -23,10 +23,10 @@ export async function wipeCaches(logoutDependant: boolean = false) {
await caches.delete(APP_LEVEL_CACHE);
for (const cache of DYNAMIC_CACHES) {
if (!logoutDependant) {
caches.delete(cache.name);
await caches.delete(cache.name);
}
if (logoutDependant && cache.clearOnLogout) {
caches.delete(cache.name);
await caches.delete(cache.name);
}
}
}
@ -36,7 +36,7 @@ export function wipeFilesCache() {
}
export async function wipeCacheEntry(cacheName: string, entry: string) {
caches.open(cacheName).then(cache => {
return caches.open(cacheName).then(cache => {
cache.delete(entry, { ignoreSearch: false });
});
}

View File

@ -24,7 +24,7 @@
"@iqser/common-ui": ["libs/common-ui/src/index.ts"],
"@models/*": ["apps/red-ui/src/app/models/*"],
"@red/domain": ["libs/red-domain/src/index.ts"],
"@redaction/red-cache": ["libs/red-cache/src/index.ts"],
"@red/cache": ["libs/red-cache/src/index.ts"],
"@services/*": ["apps/red-ui/src/app/services/*"],
"@shared/*": ["apps/red-ui/src/app/modules/shared/*"],
"@upload-download/*": ["apps/red-ui/src/app/modules/upload-download/*"],