From 49754d33fa660043a658ae35b5ae5ee46d969ef8 Mon Sep 17 00:00:00 2001
From: shamoon <4887959+shamoon@users.noreply.github.com>
Date: Tue, 9 May 2023 21:51:31 -0700
Subject: [PATCH] Render frontend html as plain text

---
 .../document-detail.component.html               |  4 ++--
 .../document-detail/document-detail.component.ts | 16 +++++++++-------
 2 files changed, 11 insertions(+), 9 deletions(-)

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 7b21d4b50..0eff363f4 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
@@ -207,8 +207,8 @@
                 <object [data]="previewUrl | safeUrl" class="preview-sticky" width="100%"></object>
             </ng-template>
         </ng-container>
-        <ng-container *ngIf="getContentType() === 'text/plain'">
-            <div [innerHTML]="previewHtml | safeHtml" class="preview-sticky bg-light p-3" width="100%"></div>
+        <ng-container *ngIf="renderAsPlainText">
+            <div [innerText]="previewText" class="preview-sticky bg-light p-3" width="100%"></div>
         </ng-container>
         <div *ngIf="requiresPassword" class="password-prompt">
             <form>
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 599ae8523..c9e98f030 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
@@ -81,7 +81,7 @@ export class DocumentDetailComponent
   title: string
   titleSubject: Subject<string> = new Subject()
   previewUrl: string
-  _previewHtml: string
+  previewText: string
   downloadUrl: string
   downloadOriginalUrl: string
 
@@ -164,6 +164,12 @@ export class DocumentDetailComponent
       : this.metadata?.original_mime_type
   }
 
+  get renderAsPlainText(): boolean {
+    return ['text/plain', 'application/csv', 'text/csv'].includes(
+      this.getContentType()
+    )
+  }
+
   get isRTL() {
     if (!this.metadata || !this.metadata.lang) return false
     else {
@@ -220,10 +226,10 @@ export class DocumentDetailComponent
           this.previewUrl = this.documentsService.getPreviewUrl(this.documentId)
           this.http.get(this.previewUrl, { responseType: 'text' }).subscribe({
             next: (res) => {
-              this._previewHtml = res.toString()
+              this.previewText = res.toString()
             },
             error: (err) => {
-              this._previewHtml = $localize`An error occurred loading content: ${
+              this.previewText = $localize`An error occurred loading content: ${
                 err.message ?? err.toString()
               }`
             },
@@ -752,8 +758,4 @@ export class DocumentDetailComponent
       )
     )
   }
-
-  get previewHtml(): string {
-    return this._previewHtml
-  }
 }