From f68d342ee440bfa659aee62a774e2e64178539b6 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Fri, 4 Feb 2022 10:52:14 +0200 Subject: [PATCH] load blobs based on cache identifier --- .../pdf-viewer/pdf-viewer.component.ts | 6 ++-- .../file-preview-screen.component.ts | 3 +- .../services/annotation-draw.service.ts | 10 +++--- .../services/file-preview-state.service.ts | 31 ++++++++----------- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/components/pdf-viewer/pdf-viewer.component.ts b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/components/pdf-viewer/pdf-viewer.component.ts index c95791eb1..ef8e78f10 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/components/pdf-viewer/pdf-viewer.component.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/components/pdf-viewer/pdf-viewer.component.ts @@ -154,7 +154,8 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha await pdfNet.initialize(environment.licenseKey ? atob(environment.licenseKey) : null); const compareDocument = await pdfNet.PDFDoc.createFromBuffer(fileReader.result as ArrayBuffer); - const currentDocument = await pdfNet.PDFDoc.createFromBuffer(await this.stateService.blob.arrayBuffer()); + const blob = await this.stateService.blob; + const currentDocument = await pdfNet.PDFDoc.createFromBuffer(await blob.arrayBuffer()); const loadCompareDocument = async () => { this._loadingService.start(); @@ -208,7 +209,8 @@ export class PdfViewerComponent extends AutoUnsubscribe implements OnInit, OnCha this.viewModeService.compareMode = false; const pdfNet = this.instance.Core.PDFNet; await pdfNet.initialize(environment.licenseKey ? atob(environment.licenseKey) : null); - const currentDocument = await pdfNet.PDFDoc.createFromBuffer(await this.stateService.blob.arrayBuffer()); + const blob = await this.stateService.blob; + const currentDocument = await pdfNet.PDFDoc.createFromBuffer(await blob.arrayBuffer()); const filename = (await this.stateService.file).filename ?? 'document.pdf'; this.instance.UI.loadDocument(currentDocument, { filename }); diff --git a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.ts b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.ts index 47343684d..109c1958d 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/file-preview-screen.component.ts @@ -561,7 +561,6 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni return; } - await this.stateService.loadBlob(file); this.stateService.fileData = await firstValueFrom(this._pdfViewerDataService.loadDataFor(file)); } @@ -694,7 +693,7 @@ export class FilePreviewScreenComponent extends AutoUnsubscribe implements OnIni this._instance.Core.annotationManager.showAnnotations(annotations); } - private async _setAnnotationsColor(annotations: Annotation[], customData: string) { + private _setAnnotationsColor(annotations: Annotation[], customData: string) { annotations.forEach(annotation => { annotation['StrokeColor'] = this._annotationDrawService.convertColor(this._instance, annotation.getCustomData(customData)); }); diff --git a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/services/annotation-draw.service.ts b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/services/annotation-draw.service.ts index 304972eb5..35ca9d421 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/services/annotation-draw.service.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/services/annotation-draw.service.ts @@ -45,7 +45,7 @@ export class AnnotationDrawService { } getColor(activeViewer: WebViewerInstance, dossierTemplateId: string, superType: string, dictionary?: string) { - let color; + let color: string; switch (superType) { case 'hint': case 'redaction': @@ -62,11 +62,11 @@ export class AnnotationDrawService { return color; } - public getAndConvertColor(activeViewer: WebViewerInstance, dossierTemplateId: string, superType: string, dictionary?: string) { + getAndConvertColor(activeViewer: WebViewerInstance, dossierTemplateId: string, superType: string, dictionary?: string) { return this.convertColor(activeViewer, this.getColor(activeViewer, dossierTemplateId, superType, dictionary)); } - public convertColor(activeViewer: WebViewerInstance, hexColor: string) { + convertColor(activeViewer: WebViewerInstance, hexColor: string) { const rgbColor = hexToRgb(hexColor); return new activeViewer.Core.Annotations.Color(rgbColor.r, rgbColor.g, rgbColor.b); } @@ -108,7 +108,7 @@ export class AnnotationDrawService { } private async _drawSections(activeViewer: WebViewerInstance, sectionGrid: ISectionGrid, dossierId: string) { - const sections = []; + const sections: Core.Annotations.RectangleAnnotation[] = []; for (const page of Object.keys(sectionGrid.rectanglesPerPage)) { const sectionRectangles = sectionGrid.rectanglesPerPage[page]; sectionRectangles.forEach(sectionRectangle => { @@ -154,7 +154,7 @@ export class AnnotationDrawService { const pageNumber = compareMode ? annotationWrapper.pageNumber * 2 - 1 : annotationWrapper.pageNumber; const dossierTemplateId = this._dossiersService.find(dossierId).dossierTemplateId; - let annotation; + let annotation: Core.Annotations.RectangleAnnotation | Core.Annotations.TextHighlightAnnotation; if (annotationWrapper.rectangle || annotationWrapper.isImage) { annotation = new activeViewer.Core.Annotations.RectangleAnnotation(); const pageHeight = activeViewer.Core.documentViewer.getPageHeight(pageNumber); diff --git a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/services/file-preview-state.service.ts b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/services/file-preview-state.service.ts index 14a3148e0..6d569c03b 100644 --- a/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/services/file-preview-state.service.ts +++ b/apps/red-ui/src/app/modules/dossier/screens/file-preview-screen/services/file-preview-state.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { BehaviorSubject, firstValueFrom, Observable, switchMap } from 'rxjs'; +import { BehaviorSubject, firstValueFrom, Observable, pairwise, switchMap } from 'rxjs'; import { FileDataModel } from '@models/file/file-data.model'; import { Dossier, File } from '@red/domain'; import { DossiersService } from '../../../../../services/entity-services/dossiers.service'; @@ -7,7 +7,7 @@ import { ActivatedRoute } from '@angular/router'; import { FilesMapService } from '../../../../../services/entity-services/files-map.service'; import { PermissionsService } from '../../../../../services/permissions.service'; import { boolFactory } from '@iqser/common-ui'; -import { filter, map, tap } from 'rxjs/operators'; +import { filter, startWith } from 'rxjs/operators'; import { FileManagementService } from '../../../../../services/entity-services/file-management.service'; @Injectable() @@ -23,7 +23,6 @@ export class FilePreviewStateService { readonly dossierTemplateId: string; readonly fileId: string; - readonly #blob$ = new BehaviorSubject(undefined); readonly #fileData$ = new BehaviorSubject(undefined); constructor( @@ -42,7 +41,7 @@ export class FilePreviewStateService { [this.isReadonly$, this.isWritable$] = boolFactory(this.file$, file => !permissionsService.canPerformAnnotationActions(file)); this.fileData$ = this.#fileData$.asObservable().pipe(filter(value => !!value)); - this.blob$ = this.#blob$.asObservable().pipe(filter(value => !!value)); + this.blob$ = this.#blob$; } get fileData(): FileDataModel { @@ -61,24 +60,20 @@ export class FilePreviewStateService { return firstValueFrom(this.dossier$); } - get blob(): Blob | undefined { - return this.#blob$.value; + get blob(): Promise { + return firstValueFrom(this.blob$); } - loadBlob(newFile: File) { - const newBlob$ = this.#downloadOriginalFile(newFile).pipe( - tap(blob => this.#blob$.next(blob)), - tap(() => console.log('new')), + get #blob$() { + return this.file$.pipe( + startWith(undefined), + pairwise(), + filter(([oldFile, newFile]) => oldFile?.cacheIdentifier !== newFile.cacheIdentifier), + switchMap(([, newFile]) => this.#downloadOriginalFile(newFile.cacheIdentifier)), ); - const blob$ = this.file$.pipe( - map(file => file.cacheIdentifier === newFile.cacheIdentifier && this.blob), - switchMap(isSame => (isSame ? this.blob$ : newBlob$)), - ); - - return firstValueFrom(blob$); } - #downloadOriginalFile(file: File): Observable { - return this._fileManagementService.downloadOriginalFile(file.dossierId, file.fileId, 'body', true, file.cacheIdentifier); + #downloadOriginalFile(cacheIdentifier: string): Observable { + return this._fileManagementService.downloadOriginalFile(this.dossierId, this.fileId, 'body', true, cacheIdentifier); } }