Allow frontend to use mime_type property

This commit is contained in:
shamoon 2024-10-15 11:27:39 -07:00
parent 346b0fab8e
commit 5ffc39feff
6 changed files with 37 additions and 38 deletions

View File

@ -104,6 +104,11 @@ describe('PreviewPopupComponent', () => {
}) })
it('should get text content from http if appropriate', () => { 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') const httpSpy = jest.spyOn(http, 'get')
httpSpy.mockReturnValueOnce( httpSpy.mockReturnValueOnce(
throwError(() => new Error('Error getting preview')) throwError(() => new Error('Error getting preview'))

View File

@ -46,8 +46,8 @@ export class PreviewPopupComponent implements OnDestroy {
get isPdf(): boolean { get isPdf(): boolean {
// We dont have time to retrieve metadata, make a best guess by file name // We dont have time to retrieve metadata, make a best guess by file name
return ( return (
this.document?.original_file_name?.endsWith('.pdf') || this.document?.archived_file_name?.length > 0 ||
this.document?.archived_file_name?.endsWith('.pdf') this.document?.mime_type?.includes('pdf')
) )
} }
@ -62,17 +62,19 @@ export class PreviewPopupComponent implements OnDestroy {
} }
init() { init() {
this.http if (this.document.mime_type?.includes('text')) {
.get(this.previewURL, { responseType: 'text' }) this.http
.pipe(first(), takeUntil(this.unsubscribeNotifier)) .get(this.previewURL, { responseType: 'text' })
.subscribe({ .pipe(first(), takeUntil(this.unsubscribeNotifier))
next: (res) => { .subscribe({
this.previewText = res.toString() next: (res) => {
}, this.previewText = res.toString()
error: (err) => { },
this.error = err error: (err) => {
}, this.error = err
}) },
})
}
} }
onError(event: any) { onError(event: any) {

View File

@ -350,7 +350,7 @@
</ng-template> </ng-template>
<ng-template #previewContent> <ng-template #previewContent>
@if (!metadata) { @if (!document) {
<div class="w-100 h-100 d-flex align-items-center justify-content-center"> <div class="w-100 h-100 d-flex align-items-center justify-content-center">
<div> <div>
<div class="spinner-border spinner-border-sm me-2" role="status"></div> <div class="spinner-border spinner-border-sm me-2" role="status"></div>

View File

@ -921,7 +921,7 @@ describe('DocumentDetailComponent', () => {
it('should display built-in pdf viewer if not disabled', () => { it('should display built-in pdf viewer if not disabled', () => {
initNormally() initNormally()
component.metadata = { has_archive_version: true } component.document.archived_file_name = 'file.pdf'
jest.spyOn(settingsService, 'get').mockReturnValue(false) jest.spyOn(settingsService, 'get').mockReturnValue(false)
expect(component.useNativePdfViewer).toBeFalsy() expect(component.useNativePdfViewer).toBeFalsy()
fixture.detectChanges() fixture.detectChanges()
@ -930,7 +930,7 @@ describe('DocumentDetailComponent', () => {
it('should display native pdf viewer if enabled', () => { it('should display native pdf viewer if enabled', () => {
initNormally() initNormally()
component.metadata = { has_archive_version: true } component.document.archived_file_name = 'file.pdf'
jest.spyOn(settingsService, 'get').mockReturnValue(true) jest.spyOn(settingsService, 'get').mockReturnValue(true)
expect(component.useNativePdfViewer).toBeTruthy() expect(component.useNativePdfViewer).toBeTruthy()
fixture.detectChanges() fixture.detectChanges()
@ -1072,8 +1072,8 @@ describe('DocumentDetailComponent', () => {
}) })
it('should change preview element by render type', () => { it('should change preview element by render type', () => {
component.metadata = { has_archive_version: true }
initNormally() initNormally()
component.document.archived_file_name = 'file.pdf'
fixture.detectChanges() fixture.detectChanges()
expect(component.archiveContentRenderType).toEqual( expect(component.archiveContentRenderType).toEqual(
component.ContentRenderType.PDF component.ContentRenderType.PDF
@ -1082,10 +1082,8 @@ describe('DocumentDetailComponent', () => {
fixture.debugElement.query(By.css('pdf-viewer-container')) fixture.debugElement.query(By.css('pdf-viewer-container'))
).not.toBeUndefined() ).not.toBeUndefined()
component.metadata = { component.document.archived_file_name = undefined
has_archive_version: false, component.document.mime_type = 'text/plain'
original_mime_type: 'text/plain',
}
fixture.detectChanges() fixture.detectChanges()
expect(component.archiveContentRenderType).toEqual( expect(component.archiveContentRenderType).toEqual(
component.ContentRenderType.Text component.ContentRenderType.Text
@ -1094,10 +1092,7 @@ describe('DocumentDetailComponent', () => {
fixture.debugElement.query(By.css('div.preview-sticky')) fixture.debugElement.query(By.css('div.preview-sticky'))
).not.toBeUndefined() ).not.toBeUndefined()
component.metadata = { component.document.mime_type = 'image/jpeg'
has_archive_version: false,
original_mime_type: 'image/jpg',
}
fixture.detectChanges() fixture.detectChanges()
expect(component.archiveContentRenderType).toEqual( expect(component.archiveContentRenderType).toEqual(
component.ContentRenderType.Image component.ContentRenderType.Image
@ -1105,13 +1100,9 @@ describe('DocumentDetailComponent', () => {
expect( expect(
fixture.debugElement.query(By.css('.preview-sticky img')) fixture.debugElement.query(By.css('.preview-sticky img'))
).not.toBeUndefined() ).not.toBeUndefined()
;(component.document.mime_type =
component.metadata = { 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'),
has_archive_version: false, fixture.detectChanges()
original_mime_type:
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
}
fixture.detectChanges()
expect(component.archiveContentRenderType).toEqual( expect(component.archiveContentRenderType).toEqual(
component.ContentRenderType.Other component.ContentRenderType.Other
) )

View File

@ -72,6 +72,7 @@ import { DeletePagesConfirmDialogComponent } from '../common/confirm-dialog/dele
import { HotKeyService } from 'src/app/services/hot-key.service' import { HotKeyService } from 'src/app/services/hot-key.service'
import { PDFDocumentProxy } from 'ng2-pdf-viewer' import { PDFDocumentProxy } from 'ng2-pdf-viewer'
import { DataType } from 'src/app/data/datatype' import { DataType } from 'src/app/data/datatype'
import { log } from '@angular-devkit/build-angular/src/builders/ssr-dev-server'
enum DocumentDetailNavIDs { enum DocumentDetailNavIDs {
Details = 1, Details = 1,
@ -221,15 +222,13 @@ export class DocumentDetailComponent
} }
get archiveContentRenderType(): ContentRenderType { get archiveContentRenderType(): ContentRenderType {
return this.getRenderType( return this.document?.archived_file_name
this.metadata?.has_archive_version ? this.getRenderType('application/pdf')
? 'application/pdf' : this.getRenderType(this.document?.mime_type)
: this.metadata?.original_mime_type
)
} }
get originalContentRenderType(): ContentRenderType { get originalContentRenderType(): ContentRenderType {
return this.getRenderType(this.metadata?.original_mime_type) return this.getRenderType(this.document?.mime_type)
} }
private getRenderType(mimeType: string): ContentRenderType { private getRenderType(mimeType: string): ContentRenderType {

View File

@ -150,6 +150,8 @@ export interface Document extends ObjectWithPermissions {
added?: Date added?: Date
mime_type?: string
deleted_at?: Date deleted_at?: Date
original_file_name?: string original_file_name?: string