diff --git a/src-ui/src/app/components/common/preview-popup/preview-popup.component.spec.ts b/src-ui/src/app/components/common/preview-popup/preview-popup.component.spec.ts index 4e67fd239..671541404 100644 --- a/src-ui/src/app/components/common/preview-popup/preview-popup.component.spec.ts +++ b/src-ui/src/app/components/common/preview-popup/preview-popup.component.spec.ts @@ -104,6 +104,11 @@ describe('PreviewPopupComponent', () => { }) it('should get text content from http if appropriate', () => { + component.document = { + ...doc, + original_file_name: 'sample.txt', + mime_type: 'text/plain', + } const httpSpy = jest.spyOn(http, 'get') httpSpy.mockReturnValueOnce( throwError(() => new Error('Error getting preview')) diff --git a/src-ui/src/app/components/common/preview-popup/preview-popup.component.ts b/src-ui/src/app/components/common/preview-popup/preview-popup.component.ts index 0302619aa..fa1d761c0 100644 --- a/src-ui/src/app/components/common/preview-popup/preview-popup.component.ts +++ b/src-ui/src/app/components/common/preview-popup/preview-popup.component.ts @@ -46,8 +46,8 @@ export class PreviewPopupComponent implements OnDestroy { get isPdf(): boolean { // We dont have time to retrieve metadata, make a best guess by file name return ( - this.document?.original_file_name?.endsWith('.pdf') || - this.document?.archived_file_name?.endsWith('.pdf') + this.document?.archived_file_name?.length > 0 || + this.document?.mime_type?.includes('pdf') ) } @@ -62,17 +62,19 @@ export class PreviewPopupComponent implements OnDestroy { } init() { - this.http - .get(this.previewURL, { responseType: 'text' }) - .pipe(first(), takeUntil(this.unsubscribeNotifier)) - .subscribe({ - next: (res) => { - this.previewText = res.toString() - }, - error: (err) => { - this.error = err - }, - }) + if (this.document.mime_type?.includes('text')) { + this.http + .get(this.previewURL, { responseType: 'text' }) + .pipe(first(), takeUntil(this.unsubscribeNotifier)) + .subscribe({ + next: (res) => { + this.previewText = res.toString() + }, + error: (err) => { + this.error = err + }, + }) + } } onError(event: any) { diff --git a/src-ui/src/app/components/document-detail/document-detail.component.html b/src-ui/src/app/components/document-detail/document-detail.component.html index 25a512463..f4f189a67 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.html +++ b/src-ui/src/app/components/document-detail/document-detail.component.html @@ -350,7 +350,7 @@ - @if (!metadata) { + @if (!document) {
diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 24ef2ffad..0a8c5f6a5 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -921,7 +921,7 @@ describe('DocumentDetailComponent', () => { it('should display built-in pdf viewer if not disabled', () => { initNormally() - component.metadata = { has_archive_version: true } + component.document.archived_file_name = 'file.pdf' jest.spyOn(settingsService, 'get').mockReturnValue(false) expect(component.useNativePdfViewer).toBeFalsy() fixture.detectChanges() @@ -930,7 +930,7 @@ describe('DocumentDetailComponent', () => { it('should display native pdf viewer if enabled', () => { initNormally() - component.metadata = { has_archive_version: true } + component.document.archived_file_name = 'file.pdf' jest.spyOn(settingsService, 'get').mockReturnValue(true) expect(component.useNativePdfViewer).toBeTruthy() fixture.detectChanges() @@ -1072,8 +1072,8 @@ describe('DocumentDetailComponent', () => { }) it('should change preview element by render type', () => { - component.metadata = { has_archive_version: true } initNormally() + component.document.archived_file_name = 'file.pdf' fixture.detectChanges() expect(component.archiveContentRenderType).toEqual( component.ContentRenderType.PDF @@ -1082,10 +1082,8 @@ describe('DocumentDetailComponent', () => { fixture.debugElement.query(By.css('pdf-viewer-container')) ).not.toBeUndefined() - component.metadata = { - has_archive_version: false, - original_mime_type: 'text/plain', - } + component.document.archived_file_name = undefined + component.document.mime_type = 'text/plain' fixture.detectChanges() expect(component.archiveContentRenderType).toEqual( component.ContentRenderType.Text @@ -1094,10 +1092,7 @@ describe('DocumentDetailComponent', () => { fixture.debugElement.query(By.css('div.preview-sticky')) ).not.toBeUndefined() - component.metadata = { - has_archive_version: false, - original_mime_type: 'image/jpg', - } + component.document.mime_type = 'image/jpeg' fixture.detectChanges() expect(component.archiveContentRenderType).toEqual( component.ContentRenderType.Image @@ -1105,13 +1100,9 @@ describe('DocumentDetailComponent', () => { expect( fixture.debugElement.query(By.css('.preview-sticky img')) ).not.toBeUndefined() - - component.metadata = { - has_archive_version: false, - original_mime_type: - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', - } - fixture.detectChanges() + ;(component.document.mime_type = + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'), + fixture.detectChanges() expect(component.archiveContentRenderType).toEqual( component.ContentRenderType.Other ) diff --git a/src-ui/src/app/components/document-detail/document-detail.component.ts b/src-ui/src/app/components/document-detail/document-detail.component.ts index 4fa535ea3..3ed6703c0 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.ts @@ -72,6 +72,7 @@ import { DeletePagesConfirmDialogComponent } from '../common/confirm-dialog/dele import { HotKeyService } from 'src/app/services/hot-key.service' import { PDFDocumentProxy } from 'ng2-pdf-viewer' import { DataType } from 'src/app/data/datatype' +import { log } from '@angular-devkit/build-angular/src/builders/ssr-dev-server' enum DocumentDetailNavIDs { Details = 1, @@ -221,15 +222,13 @@ export class DocumentDetailComponent } get archiveContentRenderType(): ContentRenderType { - return this.getRenderType( - this.metadata?.has_archive_version - ? 'application/pdf' - : this.metadata?.original_mime_type - ) + return this.document?.archived_file_name + ? this.getRenderType('application/pdf') + : this.getRenderType(this.document?.mime_type) } get originalContentRenderType(): ContentRenderType { - return this.getRenderType(this.metadata?.original_mime_type) + return this.getRenderType(this.document?.mime_type) } private getRenderType(mimeType: string): ContentRenderType { diff --git a/src-ui/src/app/data/document.ts b/src-ui/src/app/data/document.ts index 0b630b8cd..40a499ae4 100644 --- a/src-ui/src/app/data/document.ts +++ b/src-ui/src/app/data/document.ts @@ -150,6 +150,8 @@ export interface Document extends ObjectWithPermissions { added?: Date + mime_type?: string + deleted_at?: Date original_file_name?: string