load blobs based on cache identifier

This commit is contained in:
Dan Percic 2022-02-04 10:52:14 +02:00
parent f5c613974b
commit f68d342ee4
4 changed files with 23 additions and 27 deletions

View File

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

View File

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

View File

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

View File

@ -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<Blob | undefined>(undefined);
readonly #fileData$ = new BehaviorSubject<FileDataModel | undefined>(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<Blob> {
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<Blob> {
return this._fileManagementService.downloadOriginalFile(file.dossierId, file.fileId, 'body', true, file.cacheIdentifier);
#downloadOriginalFile(cacheIdentifier: string): Observable<Blob> {
return this._fileManagementService.downloadOriginalFile(this.dossierId, this.fileId, 'body', true, cacheIdentifier);
}
}